Click here to Skip to main content
15,890,609 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

sql statement,
SQL
select * from STS_VEHICLE_RUNNING_STATUS
WHERE VRS_DEVICEID='ST0001' AND  VRS_UTCTIME BETWEEN '2012-07-02' AND '2012-11-09'

retrieve data from table as,

SQL
 VRS_DEVICEID          VRS_UTCTIME                    VRS_SPEED
-------------          -----------                    ---------
 ST0001                2012-07-02 10:59:26.000        0.36
 ST0001                2012-07-02 11:33:36.000        40
 ST0001                2012-09-06 18:45:29.000        0.26
 ST0001                2012-09-06 19:46:28.000        0.36
 ST0001                2012-10-09 12:10:12.000        50
 ST0001                2012-10-09 01:12:12.000        40


but i want to fill the gridview as below,

SQL
Date              start_time         stop_time       max_speed
----              ----------         ---------       ---------
2012-07-02        10:59:26.000       11:33:36.000      40

2012-09-06        18:45:29.000       19:46:28.000      0.36

2012-10-09        12:10:12.000       01:12:12.000      50


can i retrieve the whole date in single sql statement to fill the gridview as above? i want to do fill the gridview in a button click.

plz help me...

Thanks
Posted
Updated 16-Nov-12 1:23am
v2
Comments
damodara naidu betha 16-Nov-12 7:52am    
Yes we can help you on this. But I have a doubt that what if a device is stated and stopped twice in a particular day? I mean two records in your table for a particular day.
hasbina 16-Nov-12 23:20pm    
@damodara naidu betha
not clear..

1 solution

Try the below query (Assuming you are using MSSQL Server)
SQL
SELECT CONVERT(VARCHAR(10), VRS_UTCTIME, 121) AS [Date],
MIN(SUBSTRING(CONVERT(VARCHAR(23), VRS_UTCTIME, 121), 12,20)) AS [start_time],
MAX(SUBSTRING(CONVERT(VARCHAR(23), VRS_UTCTIME, 121), 12,20)) AS [stop_time],
MAX(VRS_SPEED) AS [max_speed]
FROM STS_VEHICLE_RUNNING_STATUS
WHERE VRS_DEVICEID = 'ST0001' AND  VRS_UTCTIME BETWEEN '2012-07-02' AND '2012-11-09'
GROUP BY CONVERT(VARCHAR(10), VRS_UTCTIME, 121)
 
Share this answer
 
v2
Comments
hasbina 16-Nov-12 23:16pm    
Hi -TR_,

thanks sooooooo much _TR_...
hasbina 16-Nov-12 23:36pm    
Hi _TR_
one problem.max speed getting is not correct...why?..
__TR__ 19-Nov-12 0:25am    
Not sure what is the problem. I am getting the correct result when i execute the above query, for the data you have mentioned in your question.
hasbina 19-Nov-12 0:54am    
@_TR_

sorry sir..its my mistake..now got correct answer..it was max speed datatype problem.
i cast it from varchar to decimal using

MAX( cast(VRS_SPEED as decimal(12,2))) AS [max_peed].


Thanks...
__TR__ 19-Nov-12 0:55am    
No Problem. Glad you found out the issue.

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