2015-01-03 54 views
0

INT MIN()函數我想選擇具有最低值TWU每個TID seperately ......我的表名被重組如何基於在SQL

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 
項目中選擇VARCHAR列

我想這碼

select tid,items,twu from REORGANISED where (TID,twu) in 
(select TID,min(twu)as TWU from REORGANISED group by tid); 

這表明在條件

+0

請提供錯誤信息。 –

+0

在接近','的預期條件的上下文中指定的非布爾類型的表達式。 這是我的錯誤評論 – ASKA

回答

2

你可以通過加入現有的子查詢和原始表本身你想要的結果有些誤差。該解決方案適用於所有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 
+0

非常感謝你sooo先生... itz工作:) – ASKA

0
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 
0

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 
0

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

+0

功勞歸於@john woo – ASKA