3

I have a table with the following columns:

  • PRODUCT
  • YEAR_UPDATED

Example data:

PROD1,2017
PROD1,2015
PROD2,2014
PROD3,2017

How can I get a list of when each product was updated? Something like:

PRODUCT,2017,2016,2015,2014,etc
PROD1,Y,N,Y,N
PROD2,N,N,N,Y
PROD3,Y,N,N,N

or

PROD1,2017,2015
PROD2,2014
PROD3,2017

Oracle DB

Thanks!

3
  • 1
    Hmm, interesting. What query did you try? Also, what RDBMS are you using? Commented Mar 27, 2017 at 17:27
  • Just to clarify, does it look like this at the beginning: Prod1, 2017; Prod1, 2015; Prod2, 2014; etc.? Commented Mar 27, 2017 at 17:43
  • RDBMS=Oracle; Yes the data matches your example Commented Mar 27, 2017 at 18:22

2 Answers 2

3

I am assuming the table's name is Products, change it to whatever your table's name is.

Oracle

You achieve it by using the LISTAGG function.

select p.Product || ', ' || listagg(p.YEARUPDATED,',') within group (order by p.YEARUPDATED)
from Products p
group by p.Product;

If you are using SQL Server, this is how you can do it.

select p.Product + ', ' + stuff((select ', '+ cast(tp.YearUpdated as varchar(4)) from Products tp where p.Product = tp.Product
          FOR XML PATH('')) , 1, 2, '')
from Products p
group by p.Product

In case you want to quickly test it, you can try this out (using an in-memory table).

declare @Products table (Product varchar(50), YearUpdated int);

insert into @Products values ('Product 1', 2000);
insert into @Products values ('Product 1', 2001);
insert into @Products values ('Product 1', 2002);
insert into @Products values ('Product 1', 2003);
insert into @Products values ('Product 2', 2010);
insert into @Products values ('Product 2', 2011);
insert into @Products values ('Product 4', 2012);
insert into @Products values ('Product 4', 2013);
insert into @Products values ('Product 4', 2015);
insert into @Products values ('Product 3', 2005);

select p.Product + ', ' + stuff((select ', '+ cast(tp.YearUpdated as varchar(4)) from @Products tp where p.Product = tp.Product
          FOR XML PATH('')) , 1, 2, '')
from @Products p
group by p.Product
Sign up to request clarification or add additional context in comments.

Comments

1

Assuming you have id and year columns in your table :

   select cast ( t1.id as  varchar) + ',' + ( case when  t1.rn2 = 1   then  '2015' else '' end )
+ 
   ( case when  t1.rn2 = 2  then  '2015,2016 ' else  '' end ) + 
   ( case when  t1.rn2 =  3 then  '2015,2016,2017' else '' end ) 
 from 
  (select distinct  yourTBL.id , max(yourTBL.rn)
   over ( partition by yourTBL.id order by yourTBL.year rows BETWEEN  UNBOUNDED PRECEDING 
        AND UNBOUNDED following ) as rn2
 from (select id ,  year ,  
      row_number()over (partition by id order by year) as rn from yourTBL ) t) t1

1 Comment

You gotta be kidding us. Why are you hard-coding the string? What if the table has 2000 records with years starting at 1950 through 1999? Also, no Id in the table, the question is clearly stating the columns of the table (PRODUCT and YEAR_UPDATED).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.