How to group by less columns than selected

試著忘記壹切 提交于 2020-01-15 09:11:11

问题


I'm facing a problem here (using SQL Server 2005).

My SELECT query looks like this:

SELECT 
a.str_column1, b.str_column2, c.date_column3, c.guid_column4
FROM table
....
joining the other tables here to get my columns
....
GROUP BY 
    a.str_column1, b.str_column2, c.date_column3, c.guid_column4

This will give something like this

a.str_column1    b.str_column2    c.date_column3    c.guid_column4
------------------------------------------------------------------
a1               b1                15/07/2013       someID    
a2               b2                05/06/2012       someID
a1               b1                07/08/2013       someID
....

Now I want so that it's grouped by a.str_column1 and b.str_column2, only getting the most recent one (order by c.dat_column3)

a.str_column1    b.str_column2    c.date_column3    c.guid_column4
------------------------------------------------------------------
a1               b1                07/08/2013       someID
a2               b2                05/06/2012       someID

Any idea how I can accomplish this with SQL?


回答1:


You can use ROW_NUMBER(), and may be able to eliminate the GROUP BY entirely:

SELECT
    *
FROM (
  SELECT 
  a.str_column1, b.str_column2, c.date_column3, c.guid_column4,
  ROW_NUMBER() OVER (PARTITION BY a.str_column1, b.str_column2
                     ORDER BY c.date_column3 DESC) as rn
  FROM table
  ....
  joining the other tables here to get my columns
  ....
  --No longer needed GROUP BY a.str_column1, b.str_column2, c.date_column3, c.guid_column4
) t
WHERE t.rn = 1

In order to be able to query on the result of the ROW_NUMBER() function, you have to place your existing query (with the new column in the SELECT list) into a subquery (as above) or a Common Table Expression.




回答2:


You sholud use max function for date_column3 column and remove the column from group by clause as below

SELECT 
a.str_column1, b.str_column2, max(c.date_column3) as column3, c.guid_column4
FROM table
....
joining the other tables here to get my columns
....
GROUP BY a.str_column1, b.str_column2,c.guid_column4


来源:https://stackoverflow.com/questions/18097341/how-to-group-by-less-columns-than-selected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!