2016-06-21 48 views
1

我有下表具有重複值的表。我只想從中獲取最大長度的文本。如何在Sql Server中使用GROUP BY作爲多列時選擇最長的字符串

+----+---------------------------------+-------------------+ 
| ID | Text       | no    | 
+----+---------------------------------+-------------------+ 
| 1 | lorem ipsum ipsum    | 8955\5445   | 
+----+---------------------------------+-------------------+ 
| 1 | ipsum       | 1879668\4554\4554 | 
+----+---------------------------------+-------------------+ 
| 1 | lorem ipsum ipsum    | 5464    | 
+----+---------------------------------+-------------------+ 
| 2 | lorem ipsum ipsum derome  | 13465465\54555\45 | 
+----+---------------------------------+-------------------+ 
| 2 | lorem ipsum derome ipsum derome | 555454   | 
+----+---------------------------------+-------------------+ 

我的查詢:

select id,MAX(text),MAX(no) from table1 group by id 

結果:

+----+--------------------------+-----------+ 
| id | Text      | no  | 
+----+--------------------------+-----------+ 
| 1 | lorem ipsum ipsum  | 8955\5445 | 
+----+--------------------------+-----------+ 
| 2 | lorem ipsum ipsum derome | 555454 | 
+----+--------------------------+-----------+ 

預期輸出:

+----+--------------------------+-------------------+ 
| id | Text      | no    | 
+----+--------------------------+-------------------+ 
| 1 | lorem ipsum ipsum  | 1879668\4554\4554 | 
+----+--------------------------+-------------------+ 
| 2 | lorem ipsum ipsum derome | 13465465\54555\45 | 
+----+--------------------------+-------------------+ 
+0

編寫查詢 –

+0

對於要長的文字和最大的無每個ID來實現,無關? – jarlh

回答

2

你似乎想要最長的文本和最高的數字看你的預期結果。

讓我們來做一些測試數據;

IF OBJECT_ID('tempdb..#TestData') IS NOT NULL DROP TABLE #TestData 
GO 
CREATE TABLE #TestData (ID int, Text varchar(50), no int) 
INSERT INTO #TestData (ID, Text, no) 
VALUES 
(1,'lorem ipsum ipsum',8955) 
,(1,'ipsum',6879668) 
,(1,'lorem ipsum ipsum',5464) 
,(2,'lorem ipsum ipsum derome',63465465) 
,(2,'lorem ipsum derome ipsum derome',555454) 

我已經做了這個子查詢來分別獲得兩個最大值。第一個內部聯接僅用於篩選,第二個內部聯接返回no列中的最大值。

SELECT DISTINCT 
t.id 
,m.Text Text 
,MAX(t.no) Number 
FROM #TestData t 
LEFT JOIN --Get the max data length 
    (
     SELECT DISTINCT 
     t.ID 
     ,t.Text 
     FROM #TestData t 
     JOIN 
      (
       SELECT 
       ID 
       ,MAX(len(Text)) MaxLen 
       FROM #TestData 
       GROUP BY ID 
      ) mx 
     ON t.ID = mx.ID 
     AND LEN(t.Text) = mx.MaxLen 
    ) m 
ON t.ID = m.ID 
LEFT JOIN --Get the max no 
     (
     SELECT 
     ID 
     ,MAX(LEN(no)) MaxNo 
     FROM #TestData 
     GROUP BY ID 
    ) n 
ON t.ID = n.ID 
AND LEN(t.no) = n.MaxNo 
GROUP BY t.id, m.Text 

結果出來了;

id Text       MaxNo 
1 lorem ipsum ipsum    6879668 
2 lorem ipsum derome ipsum derome 63465465 
+0

我想要的最大長度數量也不是最大數量 –

+0

這將是同樣的事情。根據定義,最大數量將是最長的。 –

+0

在我的情況下,我聲明數字爲varchar它給出不同的輸出 –

-1
SELECT Id,Text,Number 
WHERE LENGTH(description) = (
      SELECT MAX(LENGTH(Text)) 
GROUP BY ID,Text,Number 
0

一個簡單的方法是做一個NOT EXISTS,即只要沒有其他具有相同ID的行具有更長的文本就返回一行。自我JOINGROUP BY,返回每個ID的最大號碼。

select t1.id, t1.Text, t3.no 
from tablename t1 
    join (select id, max(no) no from tablename group by id) t3 on t1.id = t3.id 
where not exists (select * from tablename t2 
        where t2.id = t1.id 
        and length(t2.Text) > length(t1.Text)) 
0

試試這個查詢,這個返回你需要什麼。 它的工作原理

SELECT a.id, 
     a.Text, 
     a.no 
FROM table AS a 
INNER JOIN 
(
    SELECT id, 
      MAX(LEN(Text)) AS lenght 
    FROM table 
    GROUP BY id 
) b 
    ON LEN(a.Text) = b.lenght 
     AND a.id = b.id; 
0

您可以將RANK函數與CTE結合使用。成纔是這樣的:

WITH cte AS (
SELECT 
    id 
    ,text 
    ,no 
    ,RANK() OVER(PARTITION BY id ORDER BY LEN(text) DESC) AS pos 
) 
SELECT * 
FROM cte 
WHERE pos = 1 
+0

這並不會考慮到他還想通過與最長文本字段不匹配的外觀來設置最大「否」。 –

0

而且,可以通過下面的查詢,如果我們想只獲得最大的長度Text

Select top 1 id,Test,no --Top n, depending upon requirement 
From 
(Select id, Test, no, LEN(TEST) as Length 
From #TableName)B 
Order by Length Desc; 
相關問題