Solved

SQL Server - help on a difficult query

Posted on 2011-03-14
8
363 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 83

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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

839 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