Thursday, May 1, 2014

multiple aggregation using pivot in sqlserver

like this



SELECT name,
MIN(CASE WHEN DAY([time])=1 THEN CONVERT(time,[time]) END) AS [in],
MAX(CASE WHEN DAY([time])=1 THEN CONVERT(time,[time]) END) AS [out],
MIN(CASE WHEN DAY([time])=2 THEN CONVERT(time,[time]) END) AS [in],
MAX(CASE WHEN DAY([time])=2 THEN CONVERT(time,[time]) END) AS [out],
MIN(CASE WHEN DAY([time])=3 THEN CONVERT(time,[time]) END) AS [in],
MAX(CASE WHEN DAY([time])=3 THEN CONVERT(time,[time]) END) AS [out],
...
MIN(CASE WHEN DAY([time])=31 THEN CONVERT(time,[time]) END) AS [in],
MAX(CASE WHEN DAY([time])=31 THEN CONVERT(time,[time]) END) AS [out]
FROM Table
GROUP BY name,
DATEDIFF(mm,0,[time])





Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://ift.tt/19nLNVq http://ift.tt/1iEAj0c


No comments:

Post a Comment