Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Sushil.SHARMA75
Starting Member
2 Posts |
Posted - 2012-02-09 : 04:15:27
|
| Need help to improve the below querySELECT MAX(currvalue) - MIN(currvalue), CONVERT(VARCHAR(10) , [datetime] , 103)FROM LOGWHERE [datetime] BETWEEN '20111215' AND '20111217'GROUP BY CONVERT(VARCHAR(10) , [datetime] , 103);My DATA is:datetiem currvalue----------------------------------20111215 11:40:30 PM 20020111215 11:48:30 PM 21020111215 11:57:30 PM 24020111216 12:03:30 PM 28020111215 11:40:30 PM 32020111215 11:40:30 PM 350When I run the above query I get20111215 4020111216 70But if we analysis we found that between 20111215 11:57:30 PM 240 and20111216 12:03:30 PM 280difference of 40 is lost.Actually the data should be20111215 4020111216 110Thank you very much for your help |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2012-02-09 : 04:44:01
|
You are wrong, the example is wrong and that is really a pity  The given example data and the given select is bringing:150 15/12/20110 16/12/2011and that is 100% correct! edit: pitty -> pity No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2012-02-09 : 05:21:12
|
So you data is more likely to look like this?datetime currvalue----------------------------------20111215 11:40:30 PM 20020111215 11:48:30 PM 21020111215 11:57:30 PM 24020111216 12:03:30 PM 28020111216 11:40:30 PM 32020111216 11:40:30 PM 350 And what you want for 20111216, is that you want the max from previous date (20111215) compared to max of 20111216, which turns out to be 110. N 56°04'39.26"E 12°55'05.63" |
 |
|
|
Sushil.SHARMA75
Starting Member
2 Posts |
Posted - 2012-02-10 : 02:34:45
|
quote: Originally posted by webfred You are wrong, the example is wrong and that is really a pity  The given example data and the given select is bringing:150 15/12/20110 16/12/2011and that is 100% correct! edit: pitty -> pity No, you're never too old to Yak'n'Roll if you're too young to die.
Sorry the data post was not correct. The data is:datetiem currvalue--------------------- -------------20111215 11:40:30 PM 20020111215 11:48:30 PM 21020111215 11:57:30 PM 24020111216 12:03:30 PM 28020111216 11:40:30 PM 32020111216 11:40:30 PM 350 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-10 : 09:21:42
|
| [code];With Value_DiffAS(SELECT DATEADD(dd,DATEDIFF(dd,0,datetime ),0) AS [Date],MAX(currvalue)-MIN(currvalue) AS diffFROM TableGROUP BY DATEADD(dd,DATEDIFF(dd,0,datetime ),0))SELECT v1.[Date],v2.DiffSoFarFROM Value_Diff v1CROSS APPLY (SELECT SUM(Diff) AS DiffSoFar FROM Value_Diff WHERE [Date] < = v1.[Date] )v2[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|