Solved

Line count by group

Posted on 2002-04-23
4
258 Views
Last Modified: 2008-02-01
For the life of me I can not remember how to do the following:

Given a table with the following:
ordr_sys_key_id, ordr_dt, locn_sys_key_id
1001, 2002/03/04, 305
1003, 2002/03/04, 305
1011, 2002/03/04, 305
1002, 2002/03/04, 306
1004, 2002/03/04, 306
1012, 2002/03/04, 306

I want ro produce something like this in a query:
1001 2002/03/04 305 1
1003 2002/03/04 305 2
1011 2002/03/04 305 3
1002 2002/03/04 306 1
1004 2002/03/04 306 2
1012 2002/03/04 306 3

Thanks,
Darek


0
Comment
Question by:dtomyn
  • 2
4 Comments
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 6963862
Something like this should do it:

SELECT ordr_sys_key_id, ordr_dt, locn_sys_key_id,
    (SELECT COUNT(*) + 1
     FROM tablex x2
     WHERE x1.locn_sys_key_id = x2.locn_sys_key_id
     AND x1.ordr_sys_key_id < x2.ordr_sys_key_id) AS 'Line#'
FROM tablex x1
ORDER BY locn_sys_key_id, ordr_sys_key_id

Notes:
*) this method may not perform very well on a really large table
*) haven't tested this yet -- will do so ASAP

0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 100 total points
ID: 6963877
Oops, got some logic backwards.  In the WHERE, need to change "<" to ">".  Also, I didn't format the date properly.  Here is the corrected query:

SELECT ordr_sys_key_id, CONVERT(CHAR(10),ordr_dt,111) AS 'Order Date', locn_sys_key_id,
   (SELECT COUNT(*) + 1
    FROM tablex x2
    WHERE x1.locn_sys_key_id = x2.locn_sys_key_id
    AND x1.ordr_sys_key_id > x2.ordr_sys_key_id) AS 'Line#'
FROM tablex x1
ORDER BY locn_sys_key_id, ordr_sys_key_id
0
 
LVL 5

Author Comment

by:dtomyn
ID: 6964030
Perfect (basically), thanks.
0
 

Expert Comment

by:gchavezr
ID: 6964032
I´m just looking
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now