Solved

T-SQL Query Help

Posted on 2011-03-23
4
214 Views
Last Modified: 2012-05-11
select id,ordername,ordernum,trailernum,city,@orderdetailid from
orders
where id in
(1,2,3,4)


Each ID will return multiple records in this query.

My requirement is to get an incremental value starting 1 for @orderdetailid.

eg: if id=1 returns 4 records and id=2 returns 2 records the output should be like the below


id ............ @orderdetailid
1                     1
1                     2
1                     3
1                     4
2                     1
2                     2

Can you please help me with this. I want to avoid using a cursor or loop here.

Thanks,
0
Comment
Question by:rocky_lotus_newbie
  • 2
4 Comments
 
LVL 34

Accepted Solution

by:
Paul MacDonald earned 250 total points
ID: 35197943
Can you just:

select id,ordername,ordernum,trailernum,city,@orderdetailid from
orders
where id in
(1,2,3,4)
ORDER BY ID, @orderdetailid
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 35198140
Why is this a variable: @orderdetailid

looks like there should be a join to the orders_details table.
0
 
LVL 9

Assisted Solution

by:mayank_joshi
mayank_joshi earned 250 total points
ID: 35198224
you may try this:-
WITH cte AS
(
select id,ordername,ordernum,trailernum,city,ROW_NUMBER() OVER ( PARTITION BY id ORDER BY ordernum ) AS 'orderdetailid' from  orders where id in (1,2,3,4)
) 
SELECT * FROM cte order by id,orderdetailid

Open in new window

0
 
LVL 9

Expert Comment

by:mayank_joshi
ID: 35198309
in the portion PARTITION BY id ORDER BY ordernum you can use any column(s) after order by
 as per your requirement
e.g.,

WITH cte AS
(
select id,ordername,ordernum,trailernum,city,ROW_NUMBER() OVER ( PARTITION BY id ORDER BY ordernum,ordername ) AS 'orderdetailid' from  orders where id in (1,2,3,4)
) 
SELECT * FROM cte order by id,orderdetailid

Open in new window

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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 how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

856 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