开发者

MySQL DISTINCT before GROUP

开发者 https://www.devze.com 2022-12-15 20:31 出处:网络
I have a M开发者_如何学运维ySQL 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

I have a M开发者_如何学运维ySQL 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消