SQL 按 desc 排序不适用于 group by
SQL sort by desc doesn't work with group by
我试图用列 tahun 和 semester 获取最新的列 id_status(时间戳完全相同所以我不能使用它),我可以用这段代码没问题:
SELECT A.id_ips, B.tahun, B.semester, B.id_status FROM data_ips A
LEFT JOIN data_status_mahasiswa B
ON A.id_mahasiswa = B.id_mahasiswa
ORDER BY B.tahun DESC, B.semester DESC
id_ips
tahun
semester
id_status
155324
2012
2
6
155324
2012
1
7
155324
2009
2
1
155324
2009
1
2
155324
2008
2
9
155324
2008
1
1
155325
2015
2
8
155325
2015
1
3
155325
2010
2
4
155325
2010
1
9
155325
2009
2
9
155325
2009
1
4
但是当我尝试用 id_ips 列对其进行分组时,数据似乎不再是有序的...
SELECT X.* FROM
(
SELECT A.id_ips, B.tahun, B.semester, B.id_status FROM data_ips A
LEFT JOIN data_status_mahasiswa B
ON A.id_mahasiswa = B.id_mahasiswa
ORDER BY B.tahun DESC, B.semester DESC
) AS X
GROUP BY X.id_ips
id_ips
tahun
semester
id_status
155324
2008
1
1
155325
2009
1
4
我试过像这样在同一个地方进行 GROUP BY 和 ORDER BY:
SELECT A.id_ips, B.tahun, B.semester, B.id_status FROM db_mahasiswa.data_ips A
LEFT JOIN db_mahasiswa.data_status_mahasiswa B
ON A.id_mahasiswa = B.id_mahasiswa
GROUP BY A.id_ips
ORDER BY B.tahun DESC, B.semester DESC
还有这个:
SELECT X.* FROM
(
SELECT A.id_ips, B.tahun, B.semester, B.id_status FROM data_ips A
LEFT JOIN data_status_mahasiswa B
ON A.id_mahasiswa = B.id_mahasiswa
ORDER BY B.tahun DESC, B.semester DESC
) AS X
GROUP BY X.id_ips
ORDER BY X.tahun DESC, X.semester DESC
但是结果还是一样,我要的是:
id_ips
tahun
semester
id_status
155324
2012
2
6
155325
2015
2
8
不知道哪里出了问题...?
由于您想要每个 id_ips 的 tahun、学期和 id_status 的最高值,请尝试使用 group by
子句聚合 max()
。
SELECT X.id_ips,max(X.tahun) as tahun, max(X.semester) as semester, B.id_status as id_status FROM
(
SELECT A.id_ips, B.tahun, B.semester, B.id_status,id_mahasiswa FROM data_ips A
LEFT JOIN data_status_mahasiswa B
ON A.id_mahasiswa = B.id_mahasiswa
ORDER BY B.tahun DESC, B.semester DESC
) AS X
left jion data_status_mahasiswa B
on X.id_mahasiswa = B.id_mahasiswa
GROUP BY X.id_ips
ORDER BY X.tahun
这个查询:
SELECT X.*
FROM (SELECT A.id_ips, B.tahun, B.semester, B.id_status
FROM data_ips A LEFT JOIN
data_status_mahasiswa B
ON A.id_mahasiswa = B.id_mahasiswa
ORDER BY B.tahun DESC, B.semester DESC
) X
GROUP BY X.id_ips;
根本不正确,应该会产生语法错误。为什么?外部查询中的 GROUP BY
列与 SELECT
列不一致。令人高兴的是,这 returns 是几乎所有数据库中的错误。
如果这是你想要的:
I tried to get the latest column id_status with column tahun and semester (the timestamp is all the same so I can't use it)
然后一种方法使用 window 函数:
SELECT X.*
FROM (SELECT A.id_ips, B.tahun, B.semester, B.id_status,
ROW_NUMBER() OVER (PARTITION BY A.id_ips ORDER BY B.tahun DESC, B.semester DESC) as seqnum
FROM data_ips A LEFT JOIN
data_status_mahasiswa B
ON A.id_mahasiswa = B.id_mahasiswa
) X
WHERE seqnum = 1;
请注意,此查询中根本没有聚合。你不是在“聚合”任何东西。您只想选择正确的行,这是通过 filtering 而不是 aggregation.
您想要的结果的解决方案是不使用 group by
,因为您没有聚合任何东西,您只是根据两个后续列的顺序为每个 id_ips 选择一行, row_number
最适合:
with cte as (
select *, Row_Number() over(partition by id_ips order by tahun desc, semester desc) rn
from t
)
select id_ips, tahun, semester, id_status
from cte
where rn=1
我试图用列 tahun 和 semester 获取最新的列 id_status(时间戳完全相同所以我不能使用它),我可以用这段代码没问题:
SELECT A.id_ips, B.tahun, B.semester, B.id_status FROM data_ips A
LEFT JOIN data_status_mahasiswa B
ON A.id_mahasiswa = B.id_mahasiswa
ORDER BY B.tahun DESC, B.semester DESC
id_ips | tahun | semester | id_status |
---|---|---|---|
155324 | 2012 | 2 | 6 |
155324 | 2012 | 1 | 7 |
155324 | 2009 | 2 | 1 |
155324 | 2009 | 1 | 2 |
155324 | 2008 | 2 | 9 |
155324 | 2008 | 1 | 1 |
155325 | 2015 | 2 | 8 |
155325 | 2015 | 1 | 3 |
155325 | 2010 | 2 | 4 |
155325 | 2010 | 1 | 9 |
155325 | 2009 | 2 | 9 |
155325 | 2009 | 1 | 4 |
但是当我尝试用 id_ips 列对其进行分组时,数据似乎不再是有序的...
SELECT X.* FROM
(
SELECT A.id_ips, B.tahun, B.semester, B.id_status FROM data_ips A
LEFT JOIN data_status_mahasiswa B
ON A.id_mahasiswa = B.id_mahasiswa
ORDER BY B.tahun DESC, B.semester DESC
) AS X
GROUP BY X.id_ips
id_ips | tahun | semester | id_status |
---|---|---|---|
155324 | 2008 | 1 | 1 |
155325 | 2009 | 1 | 4 |
我试过像这样在同一个地方进行 GROUP BY 和 ORDER BY:
SELECT A.id_ips, B.tahun, B.semester, B.id_status FROM db_mahasiswa.data_ips A
LEFT JOIN db_mahasiswa.data_status_mahasiswa B
ON A.id_mahasiswa = B.id_mahasiswa
GROUP BY A.id_ips
ORDER BY B.tahun DESC, B.semester DESC
还有这个:
SELECT X.* FROM
(
SELECT A.id_ips, B.tahun, B.semester, B.id_status FROM data_ips A
LEFT JOIN data_status_mahasiswa B
ON A.id_mahasiswa = B.id_mahasiswa
ORDER BY B.tahun DESC, B.semester DESC
) AS X
GROUP BY X.id_ips
ORDER BY X.tahun DESC, X.semester DESC
但是结果还是一样,我要的是:
id_ips | tahun | semester | id_status |
---|---|---|---|
155324 | 2012 | 2 | 6 |
155325 | 2015 | 2 | 8 |
不知道哪里出了问题...?
由于您想要每个 id_ips 的 tahun、学期和 id_status 的最高值,请尝试使用 group by
子句聚合 max()
。
SELECT X.id_ips,max(X.tahun) as tahun, max(X.semester) as semester, B.id_status as id_status FROM
(
SELECT A.id_ips, B.tahun, B.semester, B.id_status,id_mahasiswa FROM data_ips A
LEFT JOIN data_status_mahasiswa B
ON A.id_mahasiswa = B.id_mahasiswa
ORDER BY B.tahun DESC, B.semester DESC
) AS X
left jion data_status_mahasiswa B
on X.id_mahasiswa = B.id_mahasiswa
GROUP BY X.id_ips
ORDER BY X.tahun
这个查询:
SELECT X.*
FROM (SELECT A.id_ips, B.tahun, B.semester, B.id_status
FROM data_ips A LEFT JOIN
data_status_mahasiswa B
ON A.id_mahasiswa = B.id_mahasiswa
ORDER BY B.tahun DESC, B.semester DESC
) X
GROUP BY X.id_ips;
根本不正确,应该会产生语法错误。为什么?外部查询中的 GROUP BY
列与 SELECT
列不一致。令人高兴的是,这 returns 是几乎所有数据库中的错误。
如果这是你想要的:
I tried to get the latest column id_status with column tahun and semester (the timestamp is all the same so I can't use it)
然后一种方法使用 window 函数:
SELECT X.*
FROM (SELECT A.id_ips, B.tahun, B.semester, B.id_status,
ROW_NUMBER() OVER (PARTITION BY A.id_ips ORDER BY B.tahun DESC, B.semester DESC) as seqnum
FROM data_ips A LEFT JOIN
data_status_mahasiswa B
ON A.id_mahasiswa = B.id_mahasiswa
) X
WHERE seqnum = 1;
请注意,此查询中根本没有聚合。你不是在“聚合”任何东西。您只想选择正确的行,这是通过 filtering 而不是 aggregation.
您想要的结果的解决方案是不使用 group by
,因为您没有聚合任何东西,您只是根据两个后续列的顺序为每个 id_ips 选择一行, row_number
最适合:
with cte as (
select *, Row_Number() over(partition by id_ips order by tahun desc, semester desc) rn
from t
)
select id_ips, tahun, semester, id_status
from cte
where rn=1