Total sum of multiple columns in Oracle SQL Statement by unique ID
19,998
You could use:
SELECT Member_id, Flag1+Flag2+...+ Flag19 AS total
FROM tab;
If any column is nullable you have to handle it for example by using COALESCE
:
SELECT Member_id, COALESCE(Flag1,0) + COALESCE(Flag2,0) + ...
FROM tab;
Related videos on Youtube
Author by
cardonas
Updated on May 25, 2022Comments
-
cardonas almost 2 years
I have a table of members_id and Flags . I want to sum up how many flags a member_id has (like below). I have about 19 Flags which can have a 1 or 0 in the flag field . So basically I want to sum up the 1 in each column per Member_Id
Member_Id | Flag2| Flag3| Flag4|Flag5|Flag6|Flag7|Flag8| 999999b 1 0 0 0 1 1 1 777777a 0 1 1 0 1 0 0
Desired Result
Member_Id | Total 999999b 4 777777a 3
-
Juan Carlos Oropeza almost 6 yearsYou could normalize your table.
-
Abhilash Reddy almost 6 yearsIs your member_id is unique?
-
-
cardonas almost 6 yearsI did do the above but I got back a list of the member_ids and the totals were empty for every member_id except 1
-
Lukasz Szozda almost 6 years@cardonas COALESCE or NVL. You probably have a lot of nulls
-
cardonas almost 6 yearsThank you that was the issue .. I did do the nvl and got the results I needed.