如何 select 基于 int min() 函数的 varchar 列 SQL
How to select varchar column based on int min() function in SQL
我想 select 对每个 tid 分别具有最小 twu 值的项目...我的 table 名称已重新组织
tid items times twu
1 A 7 942
1 D 5 633
1 H 2 1020
2 S 4 551
3 F 4 871
3 B 2 641
4 M 1 665
4 N 7 950
4 Z 10 986
5 B 6 641
5 C 8 370
5 D 1 633
6 J 6 842
6 K 2 397
6 L 5 305
我试过这段代码
select tid,items,twu from REORGANISED where (TID,twu) in
(select TID,min(twu)as TWU from REORGANISED group by tid);
它在 where 条件
中显示了一些错误
您可以通过加入现有子查询和原始 table 本身来获得您想要的结果。此解决方案适用于所有 rdbms。
SELECT a.*
FROM Reorganised a
INNER JOIN
(
select TID,min(twu)as TWU
from REORGANISED
group by tid
) b ON a.TID = b.TID
AND a.TWU = b.TWU
WITH CTE AS (
SELECT tid,items,times,twu,
ROW_NUMBER() OVER (PARTITION BY tid ORDER BY twu DESC) rn
FROM REORGANISED
)
SELECT * FROM CTE WHERE rn=1
row_number
函数应该可以解决问题:
SELECT tid, items, times, twu
FROM (SELECT tid, items, times, twu,
ROW_NUMBER() OVER (PARTITION BY tid ORDER BY twu DESC) AS rn
FROM reorganised) y
WHERE rn = 1
SELECT a.TID,ITEMS
FROM Reorganised a
INNER JOIN
(
select TID,min(twu)as TWU
from REORGANISED
group by tid
) b ON a.TID = b.TID
AND a.TWU = b.TWU
我想 select 对每个 tid 分别具有最小 twu 值的项目...我的 table 名称已重新组织
tid items times twu
1 A 7 942
1 D 5 633
1 H 2 1020
2 S 4 551
3 F 4 871
3 B 2 641
4 M 1 665
4 N 7 950
4 Z 10 986
5 B 6 641
5 C 8 370
5 D 1 633
6 J 6 842
6 K 2 397
6 L 5 305
我试过这段代码
select tid,items,twu from REORGANISED where (TID,twu) in
(select TID,min(twu)as TWU from REORGANISED group by tid);
它在 where 条件
中显示了一些错误您可以通过加入现有子查询和原始 table 本身来获得您想要的结果。此解决方案适用于所有 rdbms。
SELECT a.*
FROM Reorganised a
INNER JOIN
(
select TID,min(twu)as TWU
from REORGANISED
group by tid
) b ON a.TID = b.TID
AND a.TWU = b.TWU
WITH CTE AS (
SELECT tid,items,times,twu,
ROW_NUMBER() OVER (PARTITION BY tid ORDER BY twu DESC) rn
FROM REORGANISED
)
SELECT * FROM CTE WHERE rn=1
row_number
函数应该可以解决问题:
SELECT tid, items, times, twu
FROM (SELECT tid, items, times, twu,
ROW_NUMBER() OVER (PARTITION BY tid ORDER BY twu DESC) AS rn
FROM reorganised) y
WHERE rn = 1
SELECT a.TID,ITEMS
FROM Reorganised a
INNER JOIN
(
select TID,min(twu)as TWU
from REORGANISED
group by tid
) b ON a.TID = b.TID
AND a.TWU = b.TWU