home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.databases.ms-sqlserver      Notorious Rube Goldberg contraption      19,505 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 17,720 of 19,505   
   Erland Sommarskog to phdate   
   Re: Number generator   
   12 Dec 09 20:36:14   
   
   From: esquel@sommarskog.se   
      
   phdate (drscrypt@gmail.com) writes:   
   > Unfortunately not.  I use this technique elsewhere but what I need is   
   > the ability to generate such numbers one at a time.  And sometimes, I   
   > need 2 or 3 of these at a time, depending on the results of the formulas   
   > I use in the package.   
      
   Instead of row_number, you can use a correlated subquery with COUNT(*):   
      
      SELECT O.OrderID, O.CustomerID, O.OrderDate,   
             (SELECT COUNT(*)   
              FROM   Orders O2   
              WHERE  O2.CustomerID = O.CustomerID   
                AND  O2.OrderID   <= O.OrderID) AS OrdnoForCustomer   
      FROM    Orders O   
      ORDER   BY O.CustomerID, O.OrderID   
      
   The performance for larger result sets is awful, but for 2-3 it should   
   not be a problem.   
      
   --   
   Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se   
      
   Links for SQL Server Books Online:   
   SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx   
   SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx   
   SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca