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 |
|
NguyenL71
Posting Yak Master
228 Posts |
Posted - 2009-11-30 : 15:45:59
|
| --SQL 2005.I need to return CompanyName only once and all the FullName associates with it. Below is the Business rule.Thank you so much in advance.IF OBJECT_ID('Tempdb.dbo.#Test', 'u') IS NOT NULL DROP TABLE #TestGOCREATE TABLE #Test ( CompId INT NULL, CompanyName VARCHAR(30) NULL, FullName VARCHAR(30) NULL)GOINSERT INTO #Test VALUES(10, 'Sears', 'John Smith')INSERT INTO #Test VALUES(10, 'Sears', 'Lisa Smith')INSERT INTO #Test VALUES(11, 'Target', 'Peter Smith')INSERT INTO #Test VALUES(11, 'Target', 'Christine Smith')GO SELECT * FROM #Test go-- Business Rule: Display CompanyName only once and all the FullName associate with it.-- Result want:CompanyName-----------Sears -- companynameJohn SmithLisa SmithTarget --companynamePeter SmithChristine Smith |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2009-11-30 : 15:59:10
|
| Do you need the FullNames comma separated?Like the one here...http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=136357 |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
NguyenL71
Posting Yak Master
228 Posts |
Posted - 2009-11-30 : 17:28:03
|
quote: Originally posted by X002548 how aboutSELECT [Name] FROM ( SELECT DISTINCT CompId, CompanyName AS [Name], 1 AS LineOrder FROM #Test UNION ALL SELECT CompId, ' ' + FullName AS [Name], 2 AS LineOrder FROM #Test) AS XXXORDER BY CompId, LineOrder, [Name] Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam
Thank you so much.L |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
matrixmind
Starting Member
9 Posts |
Posted - 2009-12-01 : 07:31:19
|
quote: Originally posted by NguyenL71 --SQL 2005.I need to return CompanyName only once and all the FullName associates with it. Below is the Business rule.Thank you so much in advance.IF OBJECT_ID('Tempdb.dbo.#Test', 'u') IS NOT NULL DROP TABLE #TestGOCREATE TABLE #Test ( CompId INT NULL, CompanyName VARCHAR(30) NULL, FullName VARCHAR(30) NULL)GOINSERT INTO #Test VALUES(10, 'Sears', 'John Smith')INSERT INTO #Test VALUES(10, 'Sears', 'Lisa Smith')INSERT INTO #Test VALUES(11, 'Target', 'Peter Smith')INSERT INTO #Test VALUES(11, 'Target', 'Christine Smith')GO SELECT * FROM #Test go-- Business Rule: Display CompanyName only once and all the FullName associate with it.-- Result want:CompanyName-----------Sears -- companynameJohn SmithLisa SmithTarget --companynamePeter SmithChristine Smith
with abas(select distinct '<<'+companyname+'>>' as companyname ,CompID,1 as [Tag] From #Testunion allselect FullName,CompID,2 as [Tag] From #Test)select companyname From ab order by CompID,tagDinesh SharmaMatrix SolutionSr.Software Engg. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-12-01 : 07:42:24
|
quote: Originally posted by matrixmind
quote: Originally posted by NguyenL71 --SQL 2005.I need to return CompanyName only once and all the FullName associates with it. Below is the Business rule.Thank you so much in advance.IF OBJECT_ID('Tempdb.dbo.#Test', 'u') IS NOT NULL DROP TABLE #TestGOCREATE TABLE #Test ( CompId INT NULL, CompanyName VARCHAR(30) NULL, FullName VARCHAR(30) NULL)GOINSERT INTO #Test VALUES(10, 'Sears', 'John Smith')INSERT INTO #Test VALUES(10, 'Sears', 'Lisa Smith')INSERT INTO #Test VALUES(11, 'Target', 'Peter Smith')INSERT INTO #Test VALUES(11, 'Target', 'Christine Smith')GO SELECT * FROM #Test go-- Business Rule: Display CompanyName only once and all the FullName associate with it.-- Result want:CompanyName-----------Sears -- companynameJohn SmithLisa SmithTarget --companynamePeter SmithChristine Smith
with abas(select distinct '<<'+companyname+'>>' as companyname ,CompID,1 as [Tag] From #Testunion allselect FullName,CompID,2 as [Tag] From #Test)select companyname From ab order by CompID,tagDinesh SharmaMatrix SolutionSr.Software Engg.
This is just same way of which was already suggested except the usage of cte MadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|