 |
| SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

May 14th, 2007, 04:16 AM
|
|
Registered User
|
|
Join Date: May 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Consolidating multiple occurences in a single row
Hi,
Let us say we have a table called, MYTABLE.
Select * from MYTABLE;
ColA ColB
---------- ---------------
1 100112
2 100113
3 100114
4 100112
4 rows selected.
I am looking for a SQL which will report Col B and Col A as
ColB Occurrences
---------- ----------------------
100112 1, 4
100113 2
100114 3
Can you kindly help me?
Thanks in advance,
Suddha
|
|

May 14th, 2007, 04:31 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Suddha,
Can you search here(p2p.wrox)in one of the SQL forums? I remember this question had been asked here and answered long back. Sorry I haven't got time to search that and post the link here for you.
cheers.
_________________________
- Vijay G
Strive for Perfection
|
|

May 14th, 2007, 04:50 AM
|
|
Registered User
|
|
Join Date: May 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Sorry, I tried searching, but did not hit upon a solution to my problem.
Can someone help?
Thanks in advance,
Suddha
|
|

May 14th, 2007, 05:17 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
|
|
May be not searching with the right word. Let me see if I can search it for you. But give me sometime. I am in the middle of something...
_________________________
- Vijay G
Strive for Perfection
|
|

May 14th, 2007, 05:25 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Look for P2P Forum > SQL Server > SQL Server 2000 > "Concatenating like records" topic on the fourth page. That should help you...
_________________________
- Vijay G
Strive for Perfection
|
|

May 14th, 2007, 05:32 AM
|
|
Registered User
|
|
Join Date: May 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks a lot.
But is it not possible to do it in a single SQL statement.
I am on oracle.
Thanks in advance,
Suddha
|
|

May 14th, 2007, 05:42 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
|
|
I didn't know that you are using Oracle. Still you can use this as a template and start working on. Let me post if I can do that with single statement.
Cheers
_________________________
- Vijay G
Strive for Perfection
|
|

May 14th, 2007, 05:55 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Suddha,
I couldn't spend much time on this. You can also take a look at the comma separated list which was referenced within the topic I referred you earlier. Not sure if you looked at it, though not sure if that helps you. Worth taking a look still.
Cheers
_________________________
- Vijay G
Strive for Perfection
|
|

May 15th, 2007, 01:26 AM
|
|
Registered User
|
|
Join Date: May 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks Vijay for all your prompt responses.
The thingis it is easy to do this using PL/SQL but quite hard to do using a SQL stmt.
I would still kindly request you to investigate this in your spare time, as this query would really help me.
Thanks in advance,
Suddha
|
|

May 16th, 2007, 06:29 PM
|
|
Registered User
|
|
Join Date: May 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
If you were using SQL Server Transact SQL, then you could always write a recursive user defined function to build a long string of all the comma separated values.
However, recursion only goes so deep, so I sometimes create a temporary table, and then write a loop that assigns the current value a parameter value that can be used in a where clause for appending to to a string.
Is this making any sense?
Zak
Quote:
quote:Originally posted by ssray23
Hi,
Let us say we have a table called, MYTABLE.
Select * from MYTABLE;
ColA ColB
---------- ---------------
1 100112
2 100113
3 100114
4 100112
4 rows selected.
I am looking for a SQL which will report Col B and Col A as
ColB Occurrences
---------- ----------------------
100112 1, 4
100113 2
100114 3
Can you kindly help me?
Thanks in advance,
Suddha
|
Zak Willis
Managing Director of www.blue-rhino.co.uk
|
|
 |