0

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! :)

Oxy111
  • 67
  • 1
  • 7
  • Why do you think you need a stored procedure? –  Apr 20 '17 at 15:42
  • @a_horse_with_no_name Sorry I am fairly new to coding, what do you mean by stored procedure? I was thinking I would need to use "as 'Indicator = Y'" and "as 'Indicator = N'" somewhere? is that the store procedure you are referring? is that a store procedure? Sorry if I am mistaken. – Oxy111 Apr 20 '17 at 15:52
  • Well you tagged your question with PL/SQL which is used to write stored procedures in Oracle. –  Apr 20 '17 at 15:53
  • @a_horse_with_no_name That is the software I am using, it's fairly similar to SQL, I don't believe I am writing stored procedure, just regular SQL codes, but PLSQL seems to have a slight variation from regular SQL codes. – Oxy111 Apr 20 '17 at 15:58
  • From the PL/SQL tag: "PL/SQL (Procedural Language/Structured Query Language) is Oracle Corporation's procedural language extension for SQL." – Aleksej Apr 20 '17 at 16:06
  • 1
    @Oxy111 - perhaps you are using the [PL/SQL Developer](https://www.allroundautomations.com/plsqldev.html) IDE? That doesn't mean you are using PL/SQL (the language). And the client you're using isn't relevant for what you are trying to do. – Alex Poole Apr 20 '17 at 16:17

1 Answers1

4

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;
DCookie
  • 42,630
  • 11
  • 83
  • 92