tags:

views:

46

answers:

1

I have a MySQL table with 10 fields. For this particular query, I only care about 4: Title, Variables, Location, Date. I would like to take the distinct values of these four groups and then group by Title, Variables. However, When I use the following query

Select DISTINCT 
       Title, 
       Variables, 
       Location, 
       Date 
  FROM ForecastsTest2 WHERE ...
GROUP BY Variables, Title 
ORDER BY Title

It groups first and then takes distinct results. Is there any way I can switch this order?

A: 

I ended up finding my solution in

SELECT Variables, 
       Title 
  FROM (SELECT DISTINCT Variables, 
                        Title, 
                        Location, 
                        Date 
       FROM MyTABLE as Table1
       ) as Table2
       WHERE ...
       GROUP BY Variables, Title
       ORDER BY TITLE

I guess I didn't do a good job of mentioning this, but I also added a HAVING COUNT(*) >= 2 in the query. In this case, the the count will happen after all non distinct rows have been removed.

Brian
If you're taking just the grouped-fields, you don't need all this mess. Do you really need `Location` and `Date` as you asked?
Y. Shoham