Solved

T-SQL Query Help

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

776 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