Solved

SQL Server - help on a difficult query

Posted on 2011-03-14
8
360 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
  • 4
  • 3
8 Comments
 
LVL 82

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 51

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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 51

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 51

Expert Comment

by:HainKurt
ID: 35132073
0
 
LVL 51

Accepted Solution

by:
HainKurt earned 500 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Help with SQL Query 23 39
Mysql Crashing Intermittently 16 39
Creating Alerts in sql sever 2 13
BULK LOGGED - log full 9 13
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
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.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now