How can I count the amount of values in different columns in oracle plsql

Jeroen Wallenus

For example, I have a table with these values:

ID Date Col1 Col2 Col3 Col4
1 01/11/2021 A A B
2 01/11/2021 B B

The A and B values are dynamic, they can be other characters as well.

Now I need somehow to get to the result that id 1 has 2 occurences of A and one of B. Id 2 has 0 occurences of A and 2 occurences of B.

I'm using dynamic SQL to do this:

for v_record in table_cursor
loop
    for i in 1 .. 4
    loop
        v_query := 'select col'||i||' from table where id = '||v_record.id;
        execute immediate v_query into v_char;
        if v_char = "any letter I'm checking" then
            amount := amount + 1;
        end if;
    end loop;
    -- do somehting with the amount
end loop;

But there has to be a better much more efficient way to do this.

I don't have that much knowledge of plsql and I really don't know how to formulate this question in google. I've looked into pivot, but I don't think that will help me out in this case.

I'd appreciate it if someone could help me out.

Tim Biegeleisen

Assuming the number of columns would be fixed at four, you could use a union aggregation approach here:

WITH cte AS (
    SELECT ID, Col1 AS val FROM yourTable UNION ALL
    SELECT ID, Col2 FROM yourTable UNION ALL
    SELECT ID, Col3 FROM yourTable UNION ALL
    SELECT ID, Col4 FROM yourTable
)

SELECT
    t1.ID,
    t2.val,
    COUNT(c.ID) AS cnt
FROM (SELECT DISTINCT ID FROM yourTable) t1
CROSS JOIN (SELECT DISTINCT val FROM cte) t2
LEFT JOIN cte c
    ON c.ID = t1.ID AND
       c.val = t2.val
WHERE
    t2.val IS NOT NULL
GROUP BY
    t1.ID,
    t2.val;

This produces:

screen capture from demo link below

Demo

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I get the count of different values of one colum

Oracle 11g how I can group values from two different columns

How can I display different components with a different specified amount of time?

How can I count different values stored in one cell in SQL?

How can I get the count of the amount of grouped columns as a column?

How can I get the count of the amount of rows for this result set in php?

list all columns with amount of different values

How can I count the amount of relevant results in an observableArray for each filter?

How to get count for different values in different columns in SQL

Oracle - How can I improve this PLSQL nested loop to retrieve information?

Oracle SQL How can I separate values from a column in two different columns?

How can I count occurences of different integer values (in a particular column) for rows with different factor values(another column)?

how do i get count of columns from 3 different but identical table with column values greater than 0

how can I count the amount of duplicate rows with this IF statement?

How do I set a table with different amount of columns on each row?

How can I count replicate values across columns?

How can I make an array from table columns in plsql

How can I count unique values by columns

how can I get the total count of same name of different columns in one table in MySQL?

How can I transform a dataframe by putting different values of a column into different columns?

How can I count values that can be grouped by another columns in R

I want to count rows of two columns with different values

How can I groupby a DataFrame at the same time I count the values and put in different columns?

How can I count cumulative variables in pandas, but split the results into different columns?

How can I count the number of zeros in all columns for a table in oracle sql

How can I count the same values ​in the array and create a different array?

Given a set of boolean columns how can I get the top 5 columns with the most amount of true values

How can I have the same index for two different columns where the columns do not have unqiue values?

How can I check in a pandas dataframe for a different values in different columns at the same time?