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

x
?
Solved

SQL Server - help on a difficult query

Posted on 2011-03-14
8
Medium Priority
?
368 Views
Last Modified: 2012-05-11
This may be pretty strait forward, but I have no idea how to do it or even to google out to do it.

This query is coming from only one table called tbProjectMaterial.  Inside this table I have relevant columns: ProjectID, CustomerPO, and LineNumber.

I need the query to return back data for a given ProjectID and where CustomerPO is not null.
More complex, I need to group by CustomerPO, show a count of the number of records making up the group, and most complex, I need to have a list in the third column of all the line numbers associated with the CustomerPO.

So the data returned might look something like this...

for ProjectID = 140

CustomerPO             Count               LineNumbers
745                           3                       294, 295, 296
748                           2                       297, 298
750                           5                       299, 300, 302, 304, 305

I can pretty much do everything except for the grouping of associated line numbers into one record in a comma delimited fashion as I'm showing above.

Thanks for any help on this.
0
Comment
Question by:JosephEricDavis
[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
  • 4
  • 3
8 Comments
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 35131780
Which database server and what programming language are you using?
0
 
LVL 7

Author Comment

by:JosephEricDavis
ID: 35131945
It's SQL Server 2008.  The project is being built with ASP.NET C#, but I need this logic to take place within a stored procedure.
0
 
LVL 60

Expert Comment

by:HainKurt
ID: 35131982
this is the solution except last part :)

select ProjectID, CustomerPO, count(1) as TotalCount
  from tbProjectMaterial
 where CustomerPO is not null
 group by ProjectID, CustomerPO
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 7

Author Comment

by:JosephEricDavis
ID: 35131996
Well yeah, like I said, I already knew how to do that.  The point of the question is to figure out how to list out the values in the third column by group.
0
 
LVL 60

Expert Comment

by:HainKurt
ID: 35132063
and all in one is
select ProjectID, CustomerPO, count(1) as TotalCount,
(SELECT cast(LineNumber as varchar) + ', '
           FROM ProjectMaterials p2
          WHERE p2.ProjectID = p1.ProjectID and p2.CustomerPO = p1.CustomerPO
          ORDER BY LineNumber
            FOR XML PATH('')
) LineNumbers
  from ProjectMaterials p1
 where CustomerPO is not null
 group by ProjectID, CustomerPO 
 
ProjectID	CustomerPO	TotalCount	LineNumbers
140	745	3	294, 295, 296, 
140	748	2	297, 298, 
140	750	5	299, 300, 302, 304, 305,

Open in new window

0
 
LVL 60

Expert Comment

by:HainKurt
ID: 35132073
0
 
LVL 60

Accepted Solution

by:
HainKurt earned 2000 total points
ID: 35132100
and to get rid of extra ", " at the end
select ProjectID, CustomerPO, LEFT(LineNumbers, len(LineNumbers)-1) LineNumbers from (
select ProjectID, CustomerPO, count(1) as TotalCount,
(SELECT cast(LineNumber as varchar) + ', '
           FROM ProjectMaterials p2
          WHERE p2.ProjectID = p1.ProjectID and p2.CustomerPO = p1.CustomerPO
          ORDER BY LineNumber
            FOR XML PATH('')
) LineNumbers
  from ProjectMaterials p1
 where CustomerPO is not null
 group by ProjectID, CustomerPO 
 ) x order by ProjectID, CustomerPO

ProjectID	CustomerPO	LineNumbers
140	745	294, 295, 296
140	748	297, 298
140	750	299, 300, 302, 304, 305

Open in new window

0
 
LVL 7

Author Closing Comment

by:JosephEricDavis
ID: 35133033
This was my final solution based on your help.

SELECT CustomerPO, [Count], LEFT(LineNumbers, LEN(LineNumbers)-1) AS LineNumbers FROM (
      SELECT CustomerPO, count(*) AS [Count], (
            SELECT CAST(LineNumber AS VARCHAR) + ', '
            FROM tbProjectMaterial p2
            WHERE p2.CustomerPO = p1.CustomerPO
            ORDER BY LineNumber
            FOR XML PATH('')
      ) LineNumbers
      FROM tbProjectMaterial p1
      WHERE CustomerPO IS NOT NULL
      AND ProjectID = @ProjectID
      GROUP BY CustomerPO
) x ORDER BY CustomerPO

Thanks a bunch.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

618 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