Click here to Skip to main content
15,886,037 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hi,

the SQL,

SQL
SELECT DATE,VRS_DEVICEID,LOCATION FROM 
(
SELECT  VRS_DEVICEID,'('+VRS_LATITUDE+', '+VRS_LONGITUDE+')' AS LOCATION,
SUBSTRING(CONVERT(VARCHAR(23), VRS_UTCTIME, 121),1,16) AS DATE
FROM STS_VEHICLE_RUNNING_STATUS )AS T1

GROUP BY DATE,
VRS_DEVICEID,LOCATION ORDER BY DATE ASC



retrieve data as below

SQL
     DATE          VRS_DEVICEID    LOCATION
     ----          ------------    --------

2012-07-02 10:59       ST001        TVM
2012-07-02 10:59       ST001        EKM
2012-07-02 10:59       ST001        ATTIGAL

2012-07-02 11:00       ST001        KOTTAKKAL
2012-07-02 11:00       ST001        KARAMANA

2012-07-02 11:02       ST001        MANNARKKAD
2012-07-02 11:02       ST001        PALAKKAD


But i want to retrieve it as below

SQL
  DATE          VRS_DEVICEID    LOCATION
     ----          ------------    --------

2012-07-02 10:59       ST001        TVM

2012-07-02 11:00       ST001        KOTTAKKAL

2012-07-02 11:02       ST001        MANNARKKAD


That is, the top row of each DATE.

Thanks..
Posted
Updated 21-Nov-12 23:39pm
v2
Comments
Herman<T>.Instance 22-Nov-12 6:07am    
how can you have the same ST001 ID at 3 different locations?

1 solution

SQL
SELECT DATE,VRS_DEVICEID,LOCATION FROM
(
    SELECT Row_number() over (
        PARTITION BY SUBSTRING(CONVERT(VARCHAR(23), VRS_UTCTIME, 121),1,16) order by VRS_DEVICEID,'('+VRS_LATITUDE+', '+VRS_LONGITUDE+')' ) as rowno,
        VRS_DEVICEID,'('+VRS_LATITUDE+', '+VRS_LONGITUDE+')' AS LOCATION,
    SUBSTRING(CONVERT(VARCHAR(23), VRS_UTCTIME, 121),1,16) AS DATE
    FROM STS_VEHICLE_RUNNING_STATUS
)AS T1
where rowno = 1
ORDER BY DATE ASC
 
Share this answer
 
v5
Comments
hasbina 22-Nov-12 5:55am    
@digimanus,
the SQL executed with error
Incorrect syntax near 'partion'.
Herman<T>.Instance 22-Nov-12 5:59am    
sorry you are right it is the word partition
hasbina 22-Nov-12 5:59am    
k.executed successfully.correction PARTITION BY.

But not get the desired result.

why?

Thanks..
Herman<T>.Instance 22-Nov-12 5:59am    
which result do you get?
Herman<T>.Instance 22-Nov-12 6:01am    
changed again. replaced the lat/lon combination with VRS_DEVICEID, cause that is what you are requesting in the main select, not the global position

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