Solved

GROUPBY where a nVarChar-field from each row is within the group concatenated in one field

Posted on 2014-10-07
5
108 Views
Last Modified: 2014-10-07
Hi,

Suppose I have following data in a table.
NameID Product TypeID     Amount
1      ProdA   1          100
1      ProdB   1          150
2      ProdC   1           50
3      ProdA   1          100
3      ProdC   1           50
4      ProdB   1          150

Open in new window

The result I need to get is:
NameID Product       TypeID     Amount
1      ProdA ProdB   1          250
2      ProdC         1           50
3      ProdA ProdC   1          150
4      ProdB         1          150

Open in new window

So all distinct rows are grouped, regardless of the product, but all products per NameID will be combined into one field.
There might be more than two products per NameID.
How can this be done?
0
Comment
Question by:Delphiwizard
[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
5 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40365804
select NameID,
ltrim((select ' ' + Product
from myTable B
where A.NameID = B.NameID
for xml path(''), elements)) as Product, TypeID, sum(Amount) as Amount
from myTable A
group by NameID, TypeID

Open in new window

0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 40365833
Hi Wizard,

There are several ways to do this, though none are truly trivial.  A function or stored procedure can be used to loop on the similar data to generate the concatenated name string.  You can also use recursive SQL to generate the concatenated name.  I'll be glad to walk you through the recursive SQL if you'd like.

Here's an article that describes the process.

   http://www.experts-exchange.com/Database/DB2/A_3618-Recursive-SQL-in-DB2-Converting-rows-to-columns.html


Kent
0
 
LVL 50

Expert Comment

by:Vitor Montalvão
ID: 40365853
You can use STUFF function to do the trick.
SELECT P1.NameID, STUFF((SELECT ' '+P2.Product FROM YourTableNameHere P2 WHERE P2.NameID=P1.NameID FOR XML PATH('')) , 1 , 1 , ''), P1.TypeID, SUM(P1.Amount)
FROM YourTableNameHere P1
GROUP BY P1.NameID, P1.TypeID

Open in new window

0
 
LVL 2

Accepted Solution

by:
Pratik Makwana earned 500 total points
ID: 40365939
Here is your solution.....

select nameid, product, typeid,Amount
from (
  select nameid,
  STUFF ((Select ' '+Product From ProductDetail p1 Where p2.NameID=P1.NameID
            For XML PATH('')),1,1,'') as Product, typeid
  , row_number() over (partition by nameid order by nameid) r
  , sum(amount) over (partition by nameid) as Amount
  from productdetail p2
) firstRow
where firstRow.r = 1
0
 

Author Closing Comment

by:Delphiwizard
ID: 40366082
This solution was the only one that worked. Thanks all for sharing.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How can I get the entire database script? 7 34
SQL- GROUP BY 4 53
denied execute as 13 57
when sa SQL Login becomes locked 6 33
After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

739 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