dbTalk Databases Forums  

pure SQL results flattening?

comp.databases comp.databases


Discuss pure SQL results flattening? in the comp.databases forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
metaperl
 
Posts: n/a

Default pure SQL results flattening? - 02-23-2007 , 08:31 AM






Hello,

let's say at table has columns A, B and C and I do:

SELECT A, B, COUNT(*)
FROM TABLE
GROUP BY A, B

so, we know that each result "record" has
a unique (A,B) and a sum of which Cs had that A and B.

Now, is there anyway in pure SQL to provide a comma separated list of
those Cs? E.g:

A1, B1, "C1, C2, C3"
A2, B2, "C1, C4, C5"

Otherwise, I suppose I would have to resort to stored procedures or a
scripting language to post-process the database results.


Reply With Quote
  #2  
Old   
strawberry
 
Posts: n/a

Default Re: pure SQL results flattening? - 02-23-2007 , 09:01 AM






On Feb 23, 2:31 pm, "metaperl" <metap... (AT) gmail (DOT) com> wrote:
Quote:
Hello,

let's say at table has columns A, B and C and I do:

SELECT A, B, COUNT(*)
FROM TABLE
GROUP BY A, B

so, we know that each result "record" has
a unique (A,B) and a sum of which Cs had that A and B.

Now, is there anyway in pure SQL to provide a comma separated list of
those Cs? E.g:

A1, B1, "C1, C2, C3"
A2, B2, "C1, C4, C5"

Otherwise, I suppose I would have to resort to stored procedures or a
scripting language to post-process the database results.
Yes. It might look something like this:

SELECT A, B, COUNT( * ) AS cnt, GROUP_CONCAT(C ORDER BY C SEPARATOR ',
' ) AS C
FROM TABLE
GROUP BY A, B
ORDER BY cnt DESC



Reply With Quote
  #3  
Old   
--CELKO--
 
Posts: n/a

Default Re: pure SQL results flattening? - 02-24-2007 , 09:03 AM



Quote:
.. each result "record" has a unique (A,B) and a sum of which Cs had that A and B.
At least you seem to know that rows are not anything like records, but
if so, why did you ask this question?

Quote:
Now, is there anyway in pure SQL to provide a comma separated list of those Cs?

No, of course not! Also why did you use double quotes on a string?


Why do you wish to destroy First Normal Form (1NF) with a
concatendated list structure? It is the foundation of RDBMS, after
all.

Why are you formatting data in the back end? The basic principle of a
tiered architecture is that display is done in the front end and never
in the back end. This a more basic programming principle than just
SQL and RDBMS.





Reply With Quote
  #4  
Old   
Tonkuma
 
Posts: n/a

Default Re: pure SQL results flattening? - 02-24-2007 , 09:17 AM



Do you mean what is pure SQL?
SQL 2003 standard (or may be SQL99) included recursive query.
If you used recursive query, it is easy to get the result you want.


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.