[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Line count by group

Posted on 2002-04-23
4
Medium Priority
?
308 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 70

Expert Comment

by:Scott Pletcher
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 70

Accepted Solution

by:
Scott Pletcher earned 400 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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

650 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