Solved

SQL Server - help on a difficult query

Posted on 2011-03-14
8
365 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 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 55

Expert Comment

by:Huseyin KAHRAMAN
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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
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 55

Expert Comment

by:Huseyin KAHRAMAN
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 55

Expert Comment

by:Huseyin KAHRAMAN
ID: 35132073
0
 
LVL 55

Accepted Solution

by:
Huseyin KAHRAMAN 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 eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

726 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