Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys, I have a sql question that I'm kinda stuck on. Ok I have a table with the names in the format ID, Name like so

1 Airtel > India
2 Banglalink > India
3 Teletalk > India
4 CityCell > India
5 Pakistan Cellular Warid
6 Teletalk > Pakistan Cellular Warid
7 CityCell > Pakistan Cellular Warid
8 Banglalink > Pakistan Cellular Warid
9 Airtel > Pakistan Cellular Warid

I only want to return the distinct name with multiple I'd e.g
Airtel 1,9
Banglalink 2,8 etc...

Can someone help me do this in one query please
Posted

1 solution

Try this:
SQL
SELECT Name, Id=STUFF((SELECT ',' + CONVERT(VarChar(10), Id)
                       FROM myTable b
                       WHERE b.Name = a.Name
                       FOR XML PATH('')), 1, 1, '')
FROM myTable a
GROUP BY Name


[edit]Typo: "2" for "1" in STUFF 3rd parameter - OriginalGriff)

"Hey thank you so much for your reply, it still only gives single Id's. The example I have above all the names are individual entries in the same table in the data base. So for instance I need to look for all instances of lets say 'Airtel'. So there are two names which start with Airtel in that table Id's 1,9. Any idea how to display this. Thanks again for your help."

That's what it does, in the only way you can if you don't know how many IDs there are per Name.
If I try it:
C#
Name            PaidAmount
AAAA      	500
BBBB      	750
AAAA      	150
CCCC      	400
DDDD      	300
AAAA      	100
CCCC      	350
DDDD      	450
XXX       	666
XXX       	666
XXX       	666
XXX       	666
XXX       	666
XXX       	666
XXX       	666
XXX       	666
And execute
SQL
SELECT Name, PaidAmount=
   STUFF((SELECT ',' + CONVERT(VarChar(10), PaidAmount)
      FROM myTable b
      WHERE b.Name = a.Name
      FOR XML PATH('')), 1, 1, '')
FROM myTable a
GROUP BY Name
Then I get:
Name            PaidAmount
AAAA      	500,150,100
BBBB      	750
CCCC      	400,350
DDDD      	300,450
XXX       	666,666,666,666,666,666,666,666
Which is exactly what you wanted...

I suspect that you got the names wrong, or didn't think about what it is doing...:laugh:
 
Share this answer
 
v3
Comments
frostcox 18-May-13 5:35am    
Hey thank you so much for your reply, it still only gives single Id's. The example I have above all the names are individual entries in the same table in the data base. So for instance I need to look for all instances of lets say 'Airtel'. So there are two names which start with Airtel in that table Id's 1,9. Any idea how to display this. Thanks again for your help.
OriginalGriff 18-May-13 5:56am    
Answer updated
frostcox 18-May-13 6:03am    
Hey but the name is different in my table so lets say 'Airtel > India' that is the name in the column in the database. Your query only matches exact names where I'm looking for all ids that start with the name left of the greater than sign, sorry for not being clear.
OriginalGriff 18-May-13 6:15am    
Ouch! No, that's actually a PITA!
The problem is that you can't return anything that isn't in an aggregate function or explicitly named in the GROUP BY clause - so you can't group by a substring of the name, as you can't use that exact value in the rest of the expression.

Nasty.

You can do it, but what I would suggest is that if at all possible you break your database column in two parts: before the greater than, and after it. You can then use GROUP BY on the former alone, and it will all work.
If you can't, then you will need to write yourself a Stored Procedure that creates a temporary table containing the split information with the other values you are interested in, and then use GROUP BY on that.

If at any point you are going to have a significant number of entries, then the SP route is going to waste time and memory significantly compared to the "do it properly" approach of splitting your data.
frostcox 18-May-13 6:23am    
Unfortunately splitting it is not an option at the minute, I agree it's terrible design but I'm only following orders:), I was just hoping there was a quick query to return the data, that's fine il do as you suggested thanks very much!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900