Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
SQL
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 ..!
Posted
Comments
KapilMuni 13-Aug-15 5:37am    
You are using Sum(Timepaid)/60 .so that , it sum up TimePaid as per grouping ,then apply Pivot ( max(timepaid)) over it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900