Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
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
 
Old May 14th, 2007, 04:16 AM
Registered User
 
Join Date: May 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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
 
Old May 14th, 2007, 04:31 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

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
 
Old May 14th, 2007, 04:50 AM
Registered User
 
Join Date: May 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry, I tried searching, but did not hit upon a solution to my problem.
Can someone help?



Thanks in advance,
Suddha
 
Old May 14th, 2007, 05:17 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

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
 
Old May 14th, 2007, 05:25 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

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
 
Old May 14th, 2007, 05:32 AM
Registered User
 
Join Date: May 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks a lot.
But is it not possible to do it in a single SQL statement.

I am on oracle.



Thanks in advance,
Suddha
 
Old May 14th, 2007, 05:42 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

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
 
Old May 14th, 2007, 05:55 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

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
 
Old May 15th, 2007, 01:26 AM
Registered User
 
Join Date: May 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
 
Old May 16th, 2007, 06:29 PM
Registered User
 
Join Date: May 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to zak_willis
Default

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
Storing checkboxes value in a single row abhishekkashyap27 C# 2005 2 April 8th, 2008 12:13 AM
Consolidating records to a single textbox drnesr Access 5 November 20th, 2006 08:22 AM
Converting large columns into a single row rahulpokharna SQL Server 2000 3 January 10th, 2006 02:14 AM
How to send multiple columns’ data of a single row swadhinm ASP.NET 1.x and 2.0 Application Design 2 June 1st, 2005 07:59 AM
Single or Multiple application. Gibs_poovath General .NET 7 May 25th, 2004 07:54 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.