Oxy111
Oxy111

Reputation: 67

PLSQL Querying one variable and spliting into two column

So I have a table called Table1, with two columns, Product, and indicator.

Table1

Product    Indicator
Product 1     Y
Product 1     Y
Product 1     Y
Product 1     N
Product 1     N
Product 2     Y
Product 2     Y
Product 2     Y
Product 2     Y
Product 2     Y

and I want to be able to run a query to show results like this

            Indicator = Y   Indicator = N
Product 1        Y               Y
Product 2        Y               N

Thanks in advance! :)

Upvotes: 0

Views: 56

Answers (1)

user330315
user330315

Reputation:

No need for PL/SQL, this can be done with plain SQL, using conditional aggregation.

select product, 
       case 
         when count(case when indicator = 'Y' then 1 end) > 0 then 'Y'
         else 'N'
       end as "Indicator = Y",
       case 
         when count(case when indicator = 'N' then 1 end) > 0 then 'Y'
         else 'N'
       end as "Indicator = N"
from table1
group by product
order by product;

Upvotes: 4

Related Questions