Forum Moderators: coopster

Message Too Old, No Replies

Group by Multiple Fields

         

paseo

11:43 am on Nov 18, 2008 (gmt 0)

10+ Year Member



Hi,

I have been trying to find a resolution to this all over the web but nothing seems to fit my situation...

I have 1 table with fields. I am trying to setup a query where the result will GROUP by both fields... and be grouped in a separate column defined by an AS statement (group A,B AS xyz)

Example....

Table Name = dates_tbl
****************************
¦ date1 ¦ date 2 ¦
****************************
¦ Nov08 ¦ Dec08 ¦
¦ Jan08 ¦ Jan08 ¦
****************************

As you can see, I have Jan08 duplicated in 1 row...My query...

SELECT date1, date2
FROM dates_tbl
GROUP BY date1, date2 AS xyz....

The group by as is not possible...Does anybody know how to group multiple columns into 1 output? I hope im making sense....

THANKS!

paseo

2:12 pm on Nov 18, 2008 (gmt 0)

10+ Year Member



Well, since nobody decided to chime in, I managed to get the desired result...be it messy, it works..In case anybody is interested, here is the query....

select dtotal, sum(ctotal) as ftotal from

(select date1 as dtotal, count(*) as ctotal from date_tbl
group by dtotal
UNION ALL
select date2 as dtotal, count(*) as ctotal from date_tbl
group by dtotal)

as tmptable group by dtotal

Obviously, put a where clause in when necessary...dtotal will end up being the date, ftotal will be the number of times it exists in either column

willis1480

3:12 pm on Nov 18, 2008 (gmt 0)

10+ Year Member



I dont think it quite makes sense, but I would do something more like this:
SELECT date1, date2, CONCAT(date1,"-",date2) AS mygroup, COUNT(*) AS grouptotal FROM date_tbl GROUP BY mygroup;

very clean and easy...mysql has alot of built in functions