Solved

SQL Server - help on a difficult query

Posted on 2011-03-14
8
364 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 52

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
Independent Software Vendors: 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!

 
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 52

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 52

Expert Comment

by:Huseyin KAHRAMAN
ID: 35132073
0
 
LVL 52

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
migrating to phpbb forum from vBulletin 4.2 3 41
sql query help 15 55
Formating field inside mysql query 2 33
SQL Server 2008 R2 service pack updates 5 46
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.

730 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