?
Solved

CTE Group By One Column and Return Many Column?

Posted on 2014-02-12
2
Medium Priority
?
307 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 46

Accepted Solution

by:
Kent Olsen earned 2000 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

Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Suggested Courses

765 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