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 |
lgameiro
Starting Member
2 Posts |
Posted - 2013-11-19 : 10:13:14
|
Hi,I have a small query to do, and i'm playing with Row_Number().I will try to give a easy example:Col1 Col22013-09-22 09:49:47.363 AAA2013-09-29 12:52:06.843 BBBBBB2013-11-12 13:14:20.727 AAA2013-11-12 13:19:37.980 AAA2013-11-12 13:38:29.967 BBBBBB2013-11-18 15:54:25.523 AAA2013-11-18 15:54:39.120 AAA2013-11-18 15:55:02.063 AAAWhat i want is:RowNb Col1 Col21 2013-09-22 09:49:47.363 AAA1 2013-09-29 12:52:06.843 BBBBBB1 2013-11-12 13:14:20.727 AAA2 2013-11-12 13:19:37.980 AAA1 2013-11-12 13:38:29.967 BBBBBB1 2013-11-18 15:54:25.523 AAA2 2013-11-18 15:54:39.120 AAA3 2013-11-18 15:55:02.063 AAAI already try very possibilities, and nothing give me this, cause every time that finds AAA he continues with the previous number, so stays like:1 2013-09-22 09:49:47.363 AAA1 2013-09-29 12:52:06.843 BBBBBB2 2013-11-12 13:14:20.727 AAA3 2013-11-12 13:19:37.980 AAA2 2013-11-12 13:38:29.967 BBBBBB4 2013-11-18 15:54:25.523 AAA5 2013-11-18 15:54:39.120 AAA6 2013-11-18 15:55:02.063 AAAQuery:Select ROW_NUMBER() over(PARTITION BY Col2 order by Col1) as NbRow,Col1,Col2From <table> |
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-11-19 : 10:40:50
|
The output you are getting is consistent with the query you posted. Perhaps this is what you want?Select ROW_NUMBER() over(PARTITION BY Col2, CAST(Col1 AS DATE) order by Col1) as NbRow,Col1,Col2From <table> |
|
|
lgameiro
Starting Member
2 Posts |
Posted - 2013-11-19 : 10:52:24
|
Thanks James,But Col1 (the date) cant go in partition, cause i need the hours and seconds.What i'm asking, if its possible with ROw_Number(), ordering by the date, and every time that the other column changes, restart the row_numberSo AAA - BBBBBB - AAA - AAA - BBBBBB gives in Row_Number() 1 - 1 - 1 - 2 - 1 and not 1 - 1 - 2 - 3 - 2 |
|
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-11-19 : 14:21:51
|
You can do something like shown below - I may not have the exact logic, but the goal is that you want to partition by your break points first, and then get the rownumbers on that partitioned setSELECT col1, col2, ROW_NUMBER() OVER(PARTITION BY col2,N ORDER BY col1) AS RNFROM (SELECT *, ROW_NUMBER() OVER (ORDER BY col1)- ROW_NUMBER() OVER (PARTITION BY col2 ORDER BY col1) AS NFROM <table>)s ORDER BY col1 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-11-20 : 02:34:09
|
quote: Originally posted by lgameiro Thanks James,But Col1 (the date) cant go in partition, cause i need the hours and seconds.What i'm asking, if its possible with ROw_Number(), ordering by the date, and every time that the other column changes, restart the row_numberSo AAA - BBBBBB - AAA - AAA - BBBBBB gives in Row_Number() 1 - 1 - 1 - 2 - 1 and not 1 - 1 - 2 - 3 - 2
Why? whats the issue I think previously posted suggestion will give you exactly your output. You can still get hours minutes etc from Col1 even if you used it within PARTITION BYsee this illustration.declare @t table(Col1 datetime,Col2 varchar(10))insert @tvalues('2013-09-22 09:49:47.363','AAA'),('2013-09-29 12:52:06.843','BBBBBB'),('2013-11-12 13:14:20.727','AAA'),('2013-11-12 13:19:37.980','AAA'),('2013-11-12 13:38:29.967','BBBBBB'),('2013-11-18 15:54:25.523','AAA'),('2013-11-18 15:54:39.120','AAA'),('2013-11-18 15:55:02.063','AAA')select ROW_NUMBER() over (PARTITION BY Col2,DATEDIFF(dd,0,Col1) ORDER BY Col1) AS RowNb,Col1,DATEPART(hh,Col1) AS hourPart,DATEPART(ss,Col1) AS SecondsPart ,Col2FROM @tORDER BY Col1output------------------------------------------------------------------RowNb Col1 Col2 hourPart SecondsPart------------------------------------------------------------------1 2013-09-22 09:49:47.363 AAA 9 471 2013-09-29 12:52:06.843 BBBBBB 12 61 2013-11-12 13:14:20.727 AAA 13 202 2013-11-12 13:19:37.980 AAA 13 371 2013-11-12 13:38:29.967 BBBBBB 13 291 2013-11-18 15:54:25.523 AAA 15 252 2013-11-18 15:54:39.120 AAA 15 393 2013-11-18 15:55:02.063 AAA 15 2 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|
|
|
|
|