| Author | Topic | 
                            
                                    | Clages1Yak Posting Veteran
 
 
                                        69 Posts | 
                                            
                                            |  Posted - 2012-12-28 : 11:43:10 
 |  
                                            | Hi , i have a table like thisDocument Prod 12345    xxx  12345    yyy123777   xxx    123777   zzz123777   kkki would like to make a select and get thisSelect document, Prod  from tableand get this  ( I dont have Column seq in the table)Document Prod   Seq12345    xxx    112345    yyy    2 123777   xxx    1123777   zzz    2 123777   kkk    3 is there a easy  way  to do this?i am using SQL2008TksClages |  | 
       
                            
                       
                          
                            
                                    | Clages1Yak Posting Veteran
 
 
                                    69 Posts | 
                                        
                                          |  Posted - 2012-12-28 : 11:54:38 
 |  
                                          | I found this soluction, but too slow with big tableis there another way?tksClagesselect OD.OrderID, LineNumber, OD.ProductID, UnitPrice, Quantity, Discount   from  Northwind.dbo.[Order Details] OD       join         (select count(*) LineNumber,                 a.OrderID, a.ProductID                from Northwind.dbo.[Order Details] A join                     Northwind.dbo.[Order Details] B                      on  A.ProductID >= B.ProductID                         and A.OrderID = B.OrderID                  group by A.OrderID, A.ProductID) N          on OD.OrderID= N.OrderID and              OD.ProductID = N.ProductID    where OD.OrderID < 10251    order by OD.OrderID, OD.ProductIDOrderID     LineNumber  ProductID   UnitPrice             Quantity Discount                 ----------- ----------- ----------- --------------------- -------- --------------- 10248       1           11          14.0000               12       0.010248       2           42          9.8000                10       0.010248       3           72          34.8000               5        0.010249       1           14          18.6000               9        0.010249       2           51          42.4000               40       0.010250       1           41          7.7000                10       0.010250       2           51          42.4000               35       0.1500000110250       3           65          16.8000               15       0.15000001 |  
                                          |  |  | 
                            
                       
                          
                            
                                    | jimfMaster Smack Fu Yak Hacker
 
 
                                    2875 Posts | 
                                        
                                          |  Posted - 2012-12-28 : 12:11:00 
 |  
                                          | SELECT Document,Prod  ,[SEQ] = row_number() over (partition by Document order by Prod)FROM  tableJimEveryday I learn something that somebody else already knew |  
                                          |  |  | 
                            
                       
                          
                            
                                    | sodeepMaster Smack Fu Yak Hacker
 
 
                                    7174 Posts | 
                                        
                                          |  Posted - 2012-12-28 : 12:36:29 
 |  
                                          | quote:This will not work in SQL 2000Originally posted by jimf
 SELECT Document,Prod  ,[SEQ] = row_number() over (partition by Document order by Prod)FROM  tableJimEveryday I learn something that somebody else already knew
 
  Do like this. If you have identity PK you don't need to create identity column. declare @t table (document int,prod Varchar(10))insert @t select 12345,'xxx'insert @t select 12345,'xxx'insert @t select 123777,'xxx'insert @t select 123777,'zzz'insert @t select 123777,'kkk'Select ID = Identity(int,1,1) ,* into #Tfrom @T tselect t.*,(select Count(*)        from    #T tt        Where tt.document = t.document        and tt.ID < = t.ID        )from #T tID	document	prod	(No column name)1	12345	xxx	12	12345	xxx	23	123777	xxx	14	123777	zzz	25	123777	kkk	3 |  
                                          |  |  | 
                            
                       
                          
                            
                                    | Clages1Yak Posting Veteran
 
 
                                    69 Posts | 
                                        
                                          |  Posted - 2012-12-28 : 14:12:49 
 |  
                                          | Jim, you kill the problemI will use only with MS-SQL2008 tks Clages |  
                                          |  |  | 
                            
                       
                          
                            
                                    | sodeepMaster Smack Fu Yak Hacker
 
 
                                    7174 Posts | 
                                        
                                          |  Posted - 2012-12-28 : 14:36:04 
 |  
                                          | Did it work or not? |  
                                          |  |  | 
                            
                       
                          
                            
                                    | jimfMaster Smack Fu Yak Hacker
 
 
                                    2875 Posts | 
                                        
                                          |  Posted - 2012-12-28 : 14:47:38 
 |  
                                          | The op said he's using 2008,  he just posted in the wrong place.JimEveryday I learn something that somebody else already knew |  
                                          |  |  | 
                            
                       
                          
                            
                                    | sodeepMaster Smack Fu Yak Hacker
 
 
                                    7174 Posts | 
                                        
                                          |  Posted - 2012-12-28 : 15:15:32 
 |  
                                          | quote:Yah. I think now it becomes important to ask OP " Are you using SQL 2000? before we provide helpOriginally posted by jimf
 The op said he's using 2008,  he just posted in the wrong place.JimEveryday I learn something that somebody else already knew
 
   |  
                                          |  |  | 
                            
                       
                          
                            
                                    | Michael Valentine JonesYak DBA Kernel (pronounced Colonel)
 
 
                                    7020 Posts | 
                                        
                                          |  Posted - 2012-12-29 : 00:32:49 
 |  
                                          | It is important to understand that row_number() is the row number of a query, not a table.The DBASE RECNO() function is a pointer to the current row in a table, a concept that just does not exist in SQL Server.There is no internal row number for a particular row in a table unless there is a key column defined for that row in the data, like an IDENTITY column.CODO ERGO SUM |  
                                          |  |  | 
                            
                       
                          
                            
                                    | Clages1Yak Posting Veteran
 
 
                                    69 Posts | 
                                        
                                          |  Posted - 2013-01-09 : 07:04:51 
 |  
                                          | for my needs  works  finei need just a sequence breaking by Orderi have a table with several orders  in each order  several productssince SQL2008 doesnt have rownumber, like recno(from dbase)this query posted by JIM  solved my problemstks againCarlos LagesDecBrazil |  
                                          |  |  | 
                            
                            
                                |  |