May 13, 2014 at 10:18 am
Hi,
I have a data set that comprises of a transaction_id and then a list of line_id's for that transaction.
eg:
transaction_id = 12345 : Line_id = 129172
transaction_id = 12345 : Line_id = 1654572
transaction_id = 12345 : Line_id = 56229172
This would imply that 3 items were purchased for a single transaction.
The output I'm looking for is follows:
transaction_id = 12345 : Line_id = 1
transaction_id = 12345 : Line_id = 2
transaction_id = 12345 : Line_id = 3
If there are 5 Line_id's for a transaction_id then the output I'm looking for would be
transaction_id = 992345: Line_id = 1
transaction_id = 992345: Line_id = 2
transaction_id = 992345: Line_id = 3
transaction_id = 992345: Line_id = 4
transaction_id = 992345: Line_id = 5
If there are duplicate Line_id's i.e purchased two of the same product, then the logic must carry on as above
eg:
transaction_id = 56474: Line_id = 1
transaction_id = 56474: Line_id = 2
Thanks a million!
May 13, 2014 at 10:24 am
You can use Row_Number with partition...
with SomeData as
(
select 12345 as TransactionID, 129172 as Line_id union all
select 12345, 1654572 union all
select 12345, 56229172 union all
select 12345, 13456 union all
select 12345, 22342 union all
select 12345, 332344 union all
select 992345, 12345666 union all
select 992345, 22333 union all
select 992345, 33345 union all
select 992345, 46345345 union all
select 992345, 5634
)
select TransactionID, ROW_NUMBER() over (PARTITION BY TransactionID order by Line_id) as RowNum
from SomeData
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
May 13, 2014 at 10:31 am
That's great! Thanks so much 🙂
May 13, 2014 at 10:52 am
mic.con87 (5/13/2014)
That's great! Thanks so much 🙂
You're welcome. Glad that worked for you.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply