Link to home
Start Free TrialLog in
Avatar of cswebdev
cswebdev

asked on

SQL table "unique" rows

table 1:
ip# | date                   |  state
-----------------------------
1   | 8/8/2000 1:00pm   | CA
1   |8/8/2000 2:00pm    | CA
1  | 8/8/2000 3:00 pm   |  IN
1  | 8/8/2000 4:00 pm   | IN  
-------------------------------------
the data in table looks like above. What I want to do is create a new table having the same structure, but I want to insert only the first instance of each hits in a state in the given day i.e the new table should look:
ip# | date                   |  state
-----------------------------
1   | 8/8/2000 1:00pm | CA
1  | 8/8/2000 3: pm     |  IN
-------------------------------------
Time is required and I don't want to use cursor-- it has millions of rows.




ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
;with cte as (
select *, rn = row_number() OVER ( partition by convert(varchar, [date],112) order by [date] asc )
from urtable )
SELECT [ip#], [date], [state]
from cte
where rn = 1
missed the 'state'

;with cte as (
select *, rn = row_number() OVER ( partition by convert(varchar, [date],112), [state] order by [date] asc )
from urtable )
SELECT [ip#], [date], [state]
from cte
where rn = 1
Avatar of cswebdev
cswebdev

ASKER

awesome answer!!