我有SQL语句:
select col1,col2,col3,col4,col5 from table1
group by col1,col2,col3,col4
生成下表:
我想只显示行,相同参数的值最小。 结果应如下:
怎么写呢?
答案 0 :(得分:1)
除非我遗漏了一些明显的东西,否则应该只是这样:
select col1, col2, col3, col4, min(col5)
from table1
group by col1, col2, col3, col4
答案 1 :(得分:1)
请在下面试试。
SELECT Year
,Month
,COL1
,COL2
,COL3
,COL4
,COL5
FROM
(SELECT Year
,Month
,COL1
,COL2
,COL3
,COL4
,COL5
,ROW_NUMBER() OVER(PARTITION BY COL4 ORDER BY COL5) AS RN
FROM table1) AS X
WHERE X.RN = 1
答案 2 :(得分:0)
将带有OVER子句的MIN()用于具有set的最小值的所有行:
SELECT
col1,
col2,
col3,
col4,
col5,
min(col5) OVER (PARTITION BY col1, col2, col3, col4) AS min_for_columns_set
FROM table1
UPD: 只有组的最小值:
SELECT
col1,
col2,
col3,
col4,
min(col5) as min_value
FROM table1
group BY col1, col2, col3, col4