October 17, 2012 at 3:17 pm
I am trying to come up a better way to query this data
;with TempCTE(State,County,City,Action,Timestamp) AS(
select 'Texas','Harris','Houston','Census Taken','11/12/2010 00:00:00' Union all
select 'Texas','Harris','Houston','Census Submitted','12/15/2010 00:00:00' Union all
select 'Oklahoma','Johnson','Tulsa','Census Planned','9/10/2010 00:00:00' Union all
select 'Oklahoma','Johnson','Tulsa','Census Taken','11/11/2010 00:00:00'
) select distinct State,County,City from TempCTE;
here is my expected result
Texas HarrisHoustonCensus Submitted12/15/2010 00:00:00
OklahomaJohnsonTulsaCensus Taken11/11/2010 00:00:00
October 17, 2012 at 3:49 pm
Oh well here is one of the solution. If you have a faster solution, I would love to know it
Thanks!
declare @Temp table(state varchar(50),county varchar(50), city varchar(50),Action varchar(50), ActDateTime DateTime)
insert into @Temp(State,County,City,Action, ActDateTime)
select 'Texas','Harris','Houston','Census Taken','11/12/2010 00:00:00' Union all
select 'Texas','Harris','Houston','Census Submitted','12/15/2010 00:00:00' Union all
select 'Oklahoma','Johnson','Tulsa','Census Planned','9/10/2010 00:00:00' Union all
select 'Oklahoma','Johnson','Tulsa','Census inprogress','11/11/2010 00:00:00' Union all
select 'Oklahoma','Johnson','Tulsa','Census Taken','11/19/2010 00:00:00'
select T1.State, T1.County,T1.City,T1.Action,T1.ActDateTime
from @Temp T1
inner join (select State,County,City, max(ActDateTime) as ActDateTime
from @Temp
group by State,County,City
) T2
ON T1.State=T2.State
and T1.County=T2.County
and T1.City=T2.City
and T1.ActDateTime=T2.ActDateTime
October 17, 2012 at 4:18 pm
Please test the solution, I might have made a mistake.
declare @Temp table(state varchar(50),county varchar(50), city varchar(50),Action varchar(50), ActDateTime DateTime)
insert into @Temp(State,County,City,Action, ActDateTime)
select 'Texas','Harris','Houston','Census Taken','11/12/2010 00:00:00' Union all
select 'Texas','Harris','Houston','Census Submitted','12/15/2010 00:00:00' Union all
select 'Oklahoma','Johnson','Tulsa','Census Planned','9/10/2010 00:00:00' Union all
select 'Oklahoma','Johnson','Tulsa','Census inprogress','11/11/2010 00:00:00' Union all
select 'Oklahoma','Johnson','Tulsa','Census Taken','11/19/2010 00:00:00';
WITH CTE AS(
SELECT State, County, City, Action, ActDateTime,
ROW_NUMBER() OVER( PARTITION BY State, County, City ORDER BY ActDateTime DESC) AS rn
FROM @Temp )
SELECT State, County, City, Action, ActDateTime
FROM CTE
WHERE rn = 1
October 17, 2012 at 5:23 pm
Luis Cazares (10/17/2012)
Please test the solution, I might have made a mistake.
declare @Temp table(state varchar(50),county varchar(50), city varchar(50),Action varchar(50), ActDateTime DateTime)
insert into @Temp(State,County,City,Action, ActDateTime)
select 'Texas','Harris','Houston','Census Taken','11/12/2010 00:00:00' Union all
select 'Texas','Harris','Houston','Census Submitted','12/15/2010 00:00:00' Union all
select 'Oklahoma','Johnson','Tulsa','Census Planned','9/10/2010 00:00:00' Union all
select 'Oklahoma','Johnson','Tulsa','Census inprogress','11/11/2010 00:00:00' Union all
select 'Oklahoma','Johnson','Tulsa','Census Taken','11/19/2010 00:00:00';
WITH CTE AS(
SELECT State, County, City, Action, ActDateTime,
ROW_NUMBER() OVER( PARTITION BY State, County, City ORDER BY ActDateTime DESC) AS rn
FROM @Temp )
SELECT State, County, City, Action, ActDateTime
FROM CTE
WHERE rn = 1
Luis
This works too, It is in facts cleaner implementation, I will see which of those two solutions runs faster as I have to deal with millions of records.
Thanks for quick response.
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply