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.
Author |
Topic |
carriehoff
Starting Member
29 Posts |
Posted - 2010-11-12 : 14:56:34
|
Hi all, For the following table:CompanyContactID CompanyID ContactFirstName 1 1000 Billie 2 1000 WilliamI'd like to write a query with a combined result, but in only one row:a.CompanyContactID, a.CompanyID, a.ContactFirstName, b.CompanyContactID, b.CompanyID, b.ContactFirstNameSo the results for the above table would be:1 1000 Billie 2 1000 WilliamI tried to join the table to itself, which combined the number of rows from 2 to 4. I'd like to reduce the number of rows to 1.Can anybody help?Thanks,Carrie |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-11-12 : 15:16:31
|
Do it in front end and not in sql. No, you're never too old to Yak'n'Roll if you're too young to die. |
|
|
carriehoff
Starting Member
29 Posts |
Posted - 2010-11-12 : 15:18:07
|
Ok, that's certainly an idea, but I was really hoping this was possible within SQL.Anyone else? |
|
|
X002548
Not Just a Number
15586 Posts |
|
carriehoff
Starting Member
29 Posts |
Posted - 2010-11-12 : 15:29:09
|
Ah, no I wasn't. I wasn't sure this was possible.I was only joining on the one similar field I could see, company ID:select c1.companycontactid, c1.companyid, c1.contactfirstname, c2.companycontactid, c2.companyid, c2.contactfirstname from companycontact c1join companycontact c2on c1.companyid = c2.companyidHowever, I used your suggestion (not knowing that it was possible before today), and achieved the result I was seeking:select c1.companycontactid, c1.companyid, c1.contactfirstname, c2.companycontactid, c2.companyid, c2.contactfirstname from companycontact c1join companycontact c2on c1.companycontactid = c2.companycontactid + 1Thank you very much, this was exactly what I was looking for! |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2010-11-12 : 15:34:00
|
quote: Originally posted by carriehoff Ah, no I wasn't. I wasn't sure this was possible.I was only joining on the one similar field I could see, company ID:select c1.companycontactid, c1.companyid, c1.contactfirstname, c2.companycontactid, c2.companyid, c2.contactfirstname from companycontact c1join companycontact c2on c1.companyid = c2.companyidHowever, I used your suggestion (not knowing that it was possible before today), and achieved the result I was seeking:select c1.companycontactid, c1.companyid, c1.contactfirstname, c2.companycontactid, c2.companyid, c2.contactfirstname from companycontact c1join companycontact c2on c1.companycontactid = c2.companycontactid + 1Thank you very much, this was exactly what I was looking for!
Great..now I'm doomed to SQL Hell for this ;-)And Comapny = CompanyID as wellIf you have more than 2 of these thing you will get a Cartesian productBrett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspxhttp://weblogs.sqlteam.com/brettk/http://brettkaiser.blogspot.com/ |
|
|
X002548
Not Just a Number
15586 Posts |
|
X002548
Not Just a Number
15586 Posts |
Posted - 2010-11-12 : 15:43:27
|
Maybe you'd like to write a FunctionCREATE FUNCTION [dbo].[udf_Category_csv]()RETURNS @table TABLE (CompanyCd varchar(20), CertCategory varchar(8000))AS BEGIN DECLARE @CertCategory varchar(8000) , @CompanyCD varchar(20) DECLARE Cert_c CURSOR FOR SELECT DISTINCT CompanyCD FROM VENDOR_CATEGORY ORDER BY CompanyCD OPEN Cert_c FETCH NEXT FROM Cert_c INTO @CompanyCD WHILE @@FETCH_STATUS = 0 BEGIN SET @CertCategory = null SELECT @CertCategory = COALESCE(@CertCategory + ', ', '') + RTRIM(CertCategory) FROM VENDOR_CATEGORY WHERE CompanyCd = @CompanyCd INSERT INTO @table(CompanyCd, CertCategory) SELECT @CompanyCd, @CertCategory FETCH NEXT FROM Cert_c INTO @CompanyCD END CLOSE Cert_C DEALLOCATE Cert_C RETURNENDGO Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspxhttp://weblogs.sqlteam.com/brettk/http://brettkaiser.blogspot.com/ |
|
|
carriehoff
Starting Member
29 Posts |
Posted - 2010-11-12 : 16:03:42
|
Yes, thank you - I did catch that about matching up the company IDs. In my data's case, there are 2 types of IDs, and I simply wanted those results returned in one row. Your help made that very easy for me. THanks again.Carrie |
|
|
|
|
|
|
|