Trying to figure how to write a pivot sql server statement. I have following table
Source Table
ID |Product |Event |Date
-----------------------------------------------
1 |Laptop |Search |2020-07-17 14:41:13.535
1 |Laptop |Product Page |2020-07-17 14:41:13.535
1 |Laptop |Bought |2020-07-17 14:41:13.535
1 |Tablet |Search |2020-07-18 14:41:13.535
1 |Tablet |Product Page |2020-07-18 14:41:13.535
1 |Tablet |Bought |2020-07-18 14:41:13.535
Desired Output
ID| Product |Search |Product Page |Bought
-----------------------------------------------------------------------------------------------
1 | Laptop |2020-07-17 14:41:13.535 |2020-07-17 14:41:13.535 |2020-07-17 14:41:13.535
1 | Tablet |2020-07-18 14:41:13.535 |2020-07-18 14:41:13.535 |2020-07-18 14:41:13.535
My Query looks like
with V1 as
(
select id,product,event,start_time_local
from table1
)
select id,product,
[search],[product page],[Bought] from V1
PIVOT (Max(start_time_local) for event_type in ([search],[product page],[Bought]))
as PivotTable;
When i take Max(date) it returns with only max value wherein i want to display all dates.