Calculate count of multiple columns

Hi experts,

I want to get the 4 column count
 

create table test (id number,col1 number, col2 number,col3 number,col4 number );

insert into test (id,col1,col2,col3,col4) values (1,0,0,0,0);
insert into test (id,col1,col2,col3,col4) values (2,0,0,0,5555);
insert into test (id,col1,col2,col3,col4) values (3,22,0,0,5555);
insert into test (id,col1,col2,col3,col4) values (4,0,0,0,5555);
insert into test (id,col1,col2,col3,col4) values (5,22,111,33333,5555);
insert into test (id,col1,col2,col3,col4) values (6,22,0,33333,5555);

My output looks like:

id count_col
1     0
2      1
3      2
4      1
5      4
6      3
Thanks in advance
select  id,
        abs(sign(nvl(col1,0))) + abs(sign(nvl(col2,0))) + abs(sign(nvl(col3,0))) + abs(sign(nvl(col4,0))) count_col
  from  test
  order by id
/

        ID  COUNT_COL
---------- ----------
         1          0
         2          1
         3          2
         4          1
         5          4
         6          3

6 rows selected.

SQL> 

SY.

Tags: Database

Similar Questions

  • Calculate totals for multiple columns

    I want to add totals for the two columns in a table and a total end with these two subtotals. I can not know how to do this and how to reference the rows and cells. Can someone please? Thank you. Deepa

    Add Totals to multiple columns.jpg

    Give a name for each line of the footer... (For ex: FotterRow1, FooterRow2)...

    Then your code will work without a lot of complications.

    Sum (FooterRow1.TotalA1 + FooterRow2.TotalA2)

    Thank you

    Srini

  • How do you count multiple columns of a field in a table

    How do you count multiple columns of a field in a table

    Select count (*)

    of user_tab_columns

    where table_name = 'YOUR_TABLE '.

  • CSS3 multiple columns and Internet Exploder

    I have developed a web page that needs a two column layout and has decided to use CSS3 to describe the columns: very simple:

    {.twocol}

    border-style: solid;

    border width: 7px;

    border-color: #0476ac;

    -webkit-border-radius: 20px;

    -moz-border-radius: 20px;

    border-radius: 20px;

    -moz-column-count: 2;

    -moz-column-gap: 20px;

    -webkit-column-count: 2;

    -webkit-column-gap: 20px;

    number of columns: 2;

    column interval: 20px;

    padding: 10px;

    height: 300px;

    }

    CSS bold described the two-column layout that I want.

    Looked in Internet Exploiter version 9 and it failed. And, since the text was missing I did the 'old skool' method:

    {.colLeft}

    Width: 410px;

    }

    {.colRight}

    Width: 410px;

    float: right;

    }

    Then in HTML:

    < section class = "twocol" >

    < div class = "colRight" >

    < p > right column of things comes here < /p >

    < / div >

    < div class = "colLeft" >

    Insert here the stuff left column < p > < /p >

    < / div >

    < / section >

    I have commented things in "BOLD" in my style sheet.

    Here's my question:

    Since I'm on the HTML5 Shiv, shouldn't that have created the support for multiple columns in CSS3 declarations or is it simply too complex?

    HTML5 Shiv, as its name suggests, is strictly for the support of HTML5 tags in pre - IE9.  It will not help you with CSS level 3.

    Nancy O.

  • Gears - error when you try to insert values into a table with multiple columns

    Hello

    I started playing with the gears and SQlLite today and I get an error when I try to insert values into a table with multiple columns.

    I have:

    var db = google.gears.factory.create('beta.database');
        db.open('developerSet');
        db.execute('create table if not exists Developers (DeveloperName text, DeveloperAge int)');
    
        var devName = "Davy"
        var devAge = 32;
    
        try {
            db.execute('insert into Developers values (?, ?)', [devName, devAge]);
            alert('success');
        }
        catch (e) {
            alert(e);
        }
    

    I get the error:

    net.rim.device.api.database.DatabaseException; insert into developers values (?,?): SQL logic error or missing database.

    I use this reference: http://code.google.com/apis/gears/api_database.html

    Everything works if I have only one field as:

    var db = google.gears.factory.create('beta.database');
        db.open('developerSet');
        db.execute('create table if not exists Developers (DeveloperName text)');
    
        var devName = "Davy"
        var devAge = 32;
    
        try {
            db.execute('insert into Developers values (?)', [devName]);
            alert('success');
        }
        catch (e) {
            alert(e);
        }
    

    I use the plug-in Visual Studio 2.0 for 2008 that are running Windows XP SP and Simulator 2.13.0.56

    Thank you

    Davy

    Yes, a SQLite database will persist between battery pulls.  The database is registered either to internal MEM or removable media (not the device memory), depending on which is available on your device.

    In general, its not considered a best practice to remove your table as soon as it is empty and re - create it again when you want to add data.  This adds extra overhead fresh for the final, delete and insert first for a given table.  Instead, define and finalize your drawing before you create your table.  Once created, review the static schema.

    That being said, for development purposes, it may be easier to provide an easy way to drop your tables while you develop your schema.

    See you soon,.

    Adam

  • Update multiple columns from multiple tables in a single UPDATE request

    Hello

    I'm trying to figure if I'm heading in the right direction.

    I want to update multiple columns from multiple tables in a single UPDATE request. Also, I would like to update multiple columns in a table from the tables.

    Scenario 1

    UPDATE Table2, Table 3
    SET T2.Column1 = T1.Column1 
    ,T2.Column2 = T1.Column2
    ,T3.Column2 = T1.Column2
    FROM Table1 T1, Table2 T2, Table3 T3
    WHERE T1.id = T2.id
    and T1.id = T3.id
    
    

    Scenario 2

    UPDATE Table3
    SET T3.Column1 = T1.Column1 
    T3.Column2 = T1.Column2
    ,T3.Column3 = T2.Column3
    ,T3.Column4 = T2.Column4
    FROM Table1 T1, Table2 T2, Table3 T3
    WHERE T3.id = T1.id
    and T3.id = T2.id
    
    

    Hello

    For scenario 1, you must write separate instructions UPDATE table2 and table3.

    To guard against someone else change one of these tables while you act so you can copy all relevant data in a global temporary table and update this global temporary table table3.

    ENGAGE only when two tables have been changed.

    You can write a procedure or an INSTEAD OF trigger to do all this.

    For scenario 2, you can reference many tables that you need when new table3.  It might be more efficient and simpler to use the MERGER rather than UPDATED.  For example:

    MERGE INTO table3 dst

    WITH THE HELP OF)

    SELECT t1.id

    t1.column1

    t1.column2

    t2.column3

    t2.column4

    FROM table1 t1

    JOIN table2 t2 ON t1.id = t2.id

    )             src

    WE (dst.id = src_id

    WHEN MATCHED THEN UPDATE

    SET dst.column1 = src.column1

    dst.column2 = src.column2,

    dst.column3 = src.column3,

    dst.column4 = src.column4,

    ;

  • Make multiple columns while placing text in InDesign CC 2015

    I used to be able to drag all by placing a text file and press the right arrow key before releasing the mouse to create multiple columns within a text. Which seems more to work in InDesign CC 2015.

    Is this a bug or Adobe changed the way it works?

    I just tested in CC 2015 and CS6.

    What works is when you slide on a block of empty text with the text tool, by pressing the right arrow key divides the image into two columns. (This works also when creating other images).

    In both versions of InDesign, when you place a text file or Word, this shortcut does not work.

    We must therefore create first part of two columns of text, then place the text in the frame.

  • How can I create sequential shooting (1 for each 'row') numbers in a script in multiple column?

    How can I create sequential shooting (1 for each 'row') numbers in a script in multiple column?

    Sorry, but for now there no such function to put sequential numbers in the column number drawn.

  • Concatenate multiple columns in a single string

    Hello

    I use Oracle 11.2, how can I concatenate values from multiple columns in a string with an SQL:

    create table testTb (number (5) classId, class varchar2 (32));

    Insert into the testTb value (101, 'room101');
    Insert into the testTb value (101, 'room201');
    Insert into the testTb value (101, 'room301');
    Insert into the testTb value (202, 'room444');
    Insert into the testTb value (202, 'room555');

    I would like to generate the result as following:

    Class 101 is room101, room201, room301
    202 class is located in room444, room555

    Thank you

    Please post sample data.
    11.2 you can use LISTAGG:

    SQL> select 'Class '||classid||' is in '||listagg(classroom, ', ') within group (order by classroom)
     str
      2  from   testtb
      3  group by classid;
    
    STR
    --------------------------------------------------------------------------------
    Class 101 is in room101, room201, room301
    Class 202 is in room444, room555
    
    2 rows selected.
    
    SQL> 
    
  • to display the separted by commas in multiple columns

    Hello


    I want to display the value in multiple columns like below

    data like this

    col1

    res_menaHome:MenaHome
    res_menaHomeEmp:MenaHome used
    res_MDSpecialSer:MD Special Services
    res_Smart: Smart

    now, I want to display like

    col1 col2
    res_menaHome MenaHome
    res_menaHomeEmp employee MenaHome
    res_MDSpecialSer® Special Services
    chip res_Smart


    Thanks in advance.

    You mean like this?

    with q as (select 'res_menaHome:MenaHome' myString from dual)
    select substr(myString, 1, instr(myString, ':') - 1) col1,
    substr(myString, instr(myString, ':') + 1) col2
     from q
    
    COL1,COL2
    res_menaHome,MenaHome
    
  • Call a select stmt with multiple columns inside another column

    Hi all

    I have a question about the appeal of a select statement, which is to have multiple columns inside another select statement.

    I know that we can use inline views to retrieve data from another table within a query as shown below.

    SELECT (SELECT dname FROM dept WHERE deptno = e.deptno), deptno, sal FROM emp e;

    Now, I'm going to pull the loc also column within the same view of inline. But oracle is not allowing me to do the same thing.

    Is there a way we can achieve the same thing because I don't want to hit the area two times table each time for each emp record.

    Appreciate your valuable suggestions.

    Thank you
    Madhu K.

    Maybe just a simple outer join?

    select ut.subscriberid
          ,ut.unitid
          ,ut.install_date
          ,nvl(tuh.hardwaretype, 'NO_HW')
    from   tt_unit ut
    left   join tt_unit_hwtype tuh
    on     tuh.unitid = ut.unitid
    where  trunc(ut.install_date) >= V_CONST_PROG_START_DT;
    
  • GROUP BY with multiple columns.

    I have a little query on the GROUP BY clause.

    Sometimes, we will give several names of columns in GROUP BY. If it behaves in the same way as ORDER BY (multi-column) I wish I had an example where I can use GROUP BY multiple columns. in any case regardless of his behaviour, it will be really appreciated if someone can explain a scenario where GROUP BY with multiple columns can be used.

    I know about the use of GROUP BY aggregate functions, but using a single column.

    Thank you.. !!!

    No, not like that

    I think that Group by does not follow any order...

    If we execute like this

    SELECT registrationid, dateofbirth, sum (age) FROM prawin62 WHERE registrationid = 22 GROUP BY dateofbirth, registrationid.

    22     1/23/1975     54
    22     5/18/2011     330
    

    It will give the same result based on date of birth...

    ~ Praveen

  • Sort order incorrect in datagrid sort multiple columns

    Hello

    I'm a little confused about sort multiple columns in a datagrid. The following code sorts, but not the sort, I expect or what you are doing the thing about a coming O alphabetically before or after P. In my opinion, the code must sort stadt, vname by clicking on the city column, and name, the VNAME by clicking on the column name, but obviously he disobeys correctly? You see an error in the code or is this a bug?

    <? XML version = "1.0" encoding = "utf-8"? >
    "" < mx:Application xmlns:mx = ' http://www.adobe.com/2006/mxml ' layout = "vertical" >
    < mx:Script >
    <! [CDATA]
    Import mx.collections.SortField;
    Import mx.collections.Sort;
    Import mx.collections.ArrayCollection;
    Import mx.events.DataGridEvent;
    var myData: ArrayCollection collection = new ArrayCollection (new Array () collection
    {name: "Meier", vname: "Peter", City: "Bonn"},
    {name: "Meier", vname: "Max", City: "Bonn"},
    {name: "Meier", vname: "Olga", City: "Bonn"},
    {name: "Meier", vname: "Peter", City: "Bochum"},
    {name: "Meier", vname: "Susi", City: "Bochum"}
    ));
    function mySort(event:DataGridEvent) {}
    var mySorter: sort = new Sort();
    Switch (event.columnIndex) {}
    case 1:
    mySorter.fields =]
    new SortField ("name", true),
    new SortField ("vname", true)
    ];
    break;
    case 3:
    mySorter.fields =]
    new SortField ("stadt", true),
    new SortField ("name", true),
    new SortField ("vname", true)
    ];
    break;
    }
    myData.sort = mySorter;
    myData.refresh ();
    }
    []] >
    < / mx:Script >
    < mx:DataGrid dataProvider = '{myData}' headerRelease = "mySort (event)" >
    < mx:columns >
    < mx:DataGridColumn dataField = "name" headerText = "Name" / >
    < mx:DataGridColumn dataField = "VNAME" headerText = "Vorname" sortable = "false" / >
    < mx:DataGridColumn dataField = "stadt" headerText = "Stadt" / >
    < / mx:columns >
    < / mx:DataGrid >
    < / mx:Application >

    Allright! So the column index begins with zero don't step one! It was the first.

    Secondly, once you have defined your own sorter, you don't want the default behavior of the event to pass by so you will add

    Event.preventDefault ();

    just after myData.refresh (); line.

    Give it a shot. should work.

    HTH.

    ATTA

  • How to load a multiple column table in the coherence of caches?

    How to load a multiple column table in the coherence of caches?
    I want to load a multi-column (about 20 columns) table in cache coherence. How to change the following code (spatially how to change the SQL SELECT statement)?
    Is the following select statement enough: Select the key, the value of EMPLOYEES ?
    public static void bulkLoad(NamedCache cache, Connection conn)
        {
        Statement s;
        ResultSet rs;
        
        try
            {
            s = conn.createStatement();
            rs = s.executeQuery("select key, value from table");
            while (rs.next())
                {
                Integer key   = new Integer(rs.getInt(1));
                String  value = rs.getString(2);
                cache.put(key, value);
                }
            ...
            }
        catch (SQLException e)
            {...}
        }

    First of all, you need a class to hold your 20 fields:

    public class Data {
    
        private String field1;
        private String field2;
        private String field3;
        private String field4;
        private String field5;
        private String field6;
        private String field7;
        private String field8;
        private String field9;
        private String field10;
        private String field11;
        private String field12;
        private String field13;
        private String field14;
        private String field15;
        private String field16;
        private String field17;
        private String field18;
        private String field19;
        private String field20;
    
        public Data() {
        }
    
        public String getField1() {
            return field1;
        }
    
        public void setField1(String field1) {
            this.field1 = field1;
        }
    
        public String getField2() {
            return field2;
        }
    
        public void setField2(String field2) {
            this.field2 = field2;
        }
    
        public String getField3() {
            return field3;
        }
    
        public void setField3(String field3) {
            this.field3 = field3;
        }
    
        public String getField4() {
            return field4;
        }
    
        public void setField4(String field4) {
            this.field4 = field4;
        }
    
        public String getField5() {
            return field5;
        }
    
        public void setField5(String field5) {
            this.field5 = field5;
        }
    
        public String getField6() {
            return field6;
        }
    
        public void setField6(String field6) {
            this.field6 = field6;
        }
    
        public String getField7() {
            return field7;
        }
    
        public void setField7(String field7) {
            this.field7 = field7;
        }
    
        public String getField8() {
            return field8;
        }
    
        public void setField8(String field8) {
            this.field8 = field8;
        }
    
        public String getField9() {
            return field9;
        }
    
        public void setField9(String field9) {
            this.field9 = field9;
        }
    
        public String getField10() {
            return field10;
        }
    
        public void setField10(String field10) {
            this.field10 = field10;
        }
    
        public String getField11() {
            return field11;
        }
    
        public void setField11(String field11) {
            this.field11 = field11;
        }
    
        public String getField12() {
            return field12;
        }
    
        public void setField12(String field12) {
            this.field12 = field12;
        }
    
        public String getField13() {
            return field13;
        }
    
        public void setField13(String field13) {
            this.field13 = field13;
        }
    
        public String getField14() {
            return field14;
        }
    
        public void setField14(String field14) {
            this.field14 = field14;
        }
    
        public String getField15() {
            return field15;
        }
    
        public void setField15(String field15) {
            this.field15 = field15;
        }
    
        public String getField16() {
            return field16;
        }
    
        public void setField16(String field16) {
            this.field16 = field16;
        }
    
        public String getField17() {
            return field17;
        }
    
        public void setField17(String field17) {
            this.field17 = field17;
        }
    
        public String getField18() {
            return field18;
        }
    
        public void setField18(String field18) {
            this.field18 = field18;
        }
    
        public String getField19() {
            return field19;
        }
    
        public void setField19(String field19) {
            this.field19 = field19;
        }
    
        public String getField20() {
            return field20;
        }
    
        public void setField20(String field20) {
            this.field20 = field20;
        }
    }
    

    Then you can use it to store data in you original code

    public static void bulkLoad(NamedCache cache, Connection conn)
        {
        Statement s;
        ResultSet rs;
    
        try
            {
            s = conn.createStatement();
            String sql = "select key, value, value2, " +
                    "value3, value4, value5, value6, " +
                    "value7, value8, value9, value10 " +
                    "value11, value12, value13, value14, " +
                    "value15, value16, value17, value18, " +
                    "value19, value120 from table";
    
            rs = s.executeQuery(sql);
            while (rs.next())
                {
                Integer key   = new Integer(rs.getInt(1));
                Data data = new Data();
                data.setField1(rs.getString(2));
                data.setField2(rs.getString(3));
                data.setField3(rs.getString(4));
                data.setField4(rs.getString(5));
                data.setField5(rs.getString(6));
                data.setField6(rs.getString(7));
                data.setField7(rs.getString(8));
                data.setField8(rs.getString(9));
                data.setField9(rs.getString(10));
                data.setField10(rs.getString(11));
                data.setField11(rs.getString(12));
                data.setField12(rs.getString(13));
                data.setField13(rs.getString(14));
                data.setField14(rs.getString(15));
                data.setField15(rs.getString(16));
                data.setField16(rs.getString(17));
                data.setField17(rs.getString(18));
                data.setField18(rs.getString(19));
                data.setField19(rs.getString(20));
                data.setField20(rs.getString(21));
                cache.put(key, data);
                }
            ...
            }
        catch (SQLException e)
            {...}
        }    
    

    Of course your data object would need the appropriate field names and you will have no channels for all types. It must also implement equals and hashCode and ideally PortableObject.

    JK

  • Merge multiple columns into a single column?

    Hello

    I need to perform queries dynamically and the return values. I need to retrieve the values of the queries in a single column only by concatenating multiple columns. I can't use the PL/SQL in my scenario.
    is it possible to run a query and the result will be extracted in a single column (multiple column values must be concatenated)?

    Thank you
    Raja.
    SELECT rtrim(extract(xmltype(REPLACE(column_value,'
    

    Check this box... Copy it directly to this

    Ravi Kumar

Maybe you are looking for