Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

CTE Group By One Column and Return Many Column?

Posted on 2014-02-12
2
Medium Priority
?
309 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
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…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

596 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