Solved

T-SQL Query Help

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

Accepted Solution

by:
paulmacd earned 250 total points
Comment Utility
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 39

Expert Comment

by:Kyle Abrahams
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

772 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

10 Experts available now in Live!

Get 1:1 Help Now