sql server - SQL Query for Max records for Multiple dates -
hi looking sql query select max records based on specific criteria customers. current situation below
customer activity date 1 1 01/02/2015 1 1 02/02/2015 1 1 03/02/2015 1 2 05/02/2015 1 2 07/02/2015 1 3 06/02/2015 2 1 01/01/2015 2 2 05/01/2015 2 3 11/01/2015
my required resultset should
customer activity1maxdate activity2maxdate activity3maxdate 1 03/02/2015 07/02/2015 06/02/2015 2 01/01/2015 05/01/2015 11/01/2015
the activities here limited 4 or 5 therefore ther e no requirement of dynamic column making
any please cheers
what asking called pivot table.
sql server has relational operator pivot , can read documentation here.
try this:
select customer, [0] activity1maxdate , [1] activity2maxdate , [2] activity3maxdate , [3] activity4maxdate , [4] activity5maxdate yourtable pivot ( max(date) activity in ([0], [1], [2], [3], [4]) ) pivottable;
Comments
Post a Comment