Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 joining records from same table

Author  Topic 

jryannel
Starting Member

13 Posts

Posted - 2003-12-30 : 20:26:37
I have created a temporary table to join a lot of invoices together. However I have companies in multiple times because they have multiple invoices. Can I combine these records into a single record?? I have no idea how to write a query like this.

I'm thinking like this

product invoice title orderid
widget 1 test 6
widget 2 test 6

combine into
product invoice1 invoice2 title orderid
widget 1 2 test 6



Thanks, Jeff

ehorn
Master Smack Fu Yak Hacker

1632 Posts

Posted - 2003-12-30 : 20:57:28
[code]select product, min(invoice) invoice1, max(invoice) invoice2, title,orderid
from prod
group by product, title,orderid
[/code]
Go to Top of Page

mohdowais
Sheikh of Yak Knowledge

1456 Posts

Posted - 2003-12-31 : 01:33:25
Jeff, what happens when you have more than two invoices? Would you add a column for every invoice number? Perhaps you want to show those invoice numbers as a comma-separated list. Take a look at this article for more information: http://www.sqlteam.com/item.asp?ItemID=2368

OS
Go to Top of Page

jryannel
Starting Member

13 Posts

Posted - 2003-12-31 : 09:03:17
ehorn, how do i account for 3 or more invoices? It seems like your query works for up to 2..Of course my example only had 2 in it, but I have some companies listed 3 or more times.

Thanks
Go to Top of Page
   

- Advertisement -