Solved

CTE Group By One Column and Return Many Column?

Posted on 2014-02-12
2
299 Views
Last Modified: 2014-02-15
There must be a way to Group By 1 column without including every column. I don't want to Group By t1.id or t2.id, only t1.Company_Name .

Note: CTE is just an idea that might work, I don't have CTE code here.

Note: I have more columns not included in this example for brevity sake...

[Table1]
Company_Name           States          Contact_Name
My Business                  KY; OH             John Doe
My Business                  KY; OH             Mark Smith
My Business                  KY; OH             Bill Jones
My Business                  KY; OH             Dan Moore

[Query]
Select
    t1.Company_Name ,
   Stuff ((SELECT '; ' + s.name AS [text()]
           FROM States s
           WHERE (t1.id = s.id) FOR XML path('')), 1, 1, '') AS [States],
 
   Stuff ((SELECT '; ' + c.Contact_Name AS [text()]
           FROM Contacts c
           WHERE (t2.id = c.id) FOR XML path('')), 1, 1, '') AS [Contacts],
From
    Companies t1
    Join Contacts t2 on t1.id = t2.ContactID

Group By
    t1.Company_Name ],   --I only want to Group on this column
    t1.id,                              --don't want to group by
    t2.id                               --don't want to group by
0
Comment
Question by:WorknHardr
2 Comments
 
LVL 45

Accepted Solution

by:
Kent Olsen earned 500 total points
ID: 39853823
Hi WorkN,

Unfortunately, this functionality doesn't exist in SQL Server.  It does in MySQL, but most DBMS haven't incorporated it.

You'll have to run the small query with the correct GROUP BY and then LEFT or INNER join the table back to that result.


Good Luck,
Kent
0
 

Author Closing Comment

by:WorknHardr
ID: 39861346
thx
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MIcrosoft SQL 2014 Database Copy Question 16 51
Help with Merge Join and Conditional Split in SSIS 6 76
TSQL convert date to string 4 34
SQL Recursion 6 16
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

792 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