SELECT meterid,transdatetime,[8] as [8 - 9],[9] as [9 - 10],[10] as [10 - 11],[11] as [11 - 12],[12] as [12 - 1],[13]as[1 - 2],[14]as[2 - 3],[15]as[3 - 4],[16]as[4 - 5],[17]as[5 - 6],[18]as[6 - 7],[19]as[7 - 8],[20]as[8 - 9] FROM
(SELECT meterid,
CONVERT(VARCHAR(10), max(transdatetime),111) transdatetime,
DATEPART(HOUR, transdatetime) [Hour] ,
(sum(TimePaid)/60) [TimePaid]
FROM transactionscreditcard
where transdatetime between '2014-04-03 08:00:00' and '2014-04-05 20:00:00'
and customerid=800 AND meterid=20410809
group by DATEPART(HOUR, transdatetime),meterid,TimePaid) AL1
PIVOT(MAX([TimePaid]) FOR [Hour] IN ([8],[9],[10],[11],[12],[13],[14],[15],[16],[17],[18],[19],[20])
) P;
When i run the above query, i am getting the sum of column.
For date 2014/04/03
20410809 | 2014/04/03| NULL| NULL| NULL| NULL| NULL| NULL| NULL| NULL| 4| NULL| NULL|
For date 2014/04/04
20410809 | 2014/04/04| NULL| NULL| 1| 2| 13| NULL| NULL| NULL| NULL| NULL| NULL|
For date 2014/04/04
20410809 | 2014/04/07| NULL| NULL| 257| 279| 2| NULL| NULL| NULL| NULL| NULL|
For date 2014/04/03 - 2014/04/07
20410809 | 2014/04/03| NULL| NULL| NULL| NULL| NULL| NULL| NULL| NULL| 4| NULL| NULL|
20410809 | 2014/04/04| NULL| NULL| NULL| NULL| NULL| NULL| NULL| NULL| NULL| NULL| NULL|
20410809 | 2014/04/07| NULL| NULL| 258| 281| 15| NULL| NULL| NULL| NULL| NULL| NULL|
You can see when I try to fetch data from date 2014/04/03 - 2014/04/07, there are 3 record, but data is adding up.
Please help me ..!