Solved

T-SQL Query Help

Posted on 2011-03-23
4
209 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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.

929 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

21 Experts available now in Live!

Get 1:1 Help Now