我想顯示以下字串:
蘋果、香蕉、腰果、甜甜圈、大象、魚
變成這種格式:
蘋果、香蕉 其他 4 家
我只想顯示前兩個物件并休息計數!
uj5u.com熱心網友回復:
正如其他人所說,這不是 SQL Server 的作業,而是您的表示層。根據您的預期結果,我假設您的逗號表示您將分隔資料存盤在資料庫中;一個致命的缺陷。如果你規范了你的設計,你可能很容易在你的應用層實作這一點。
當您使用非規范化資料時,您需要先對其進行規范化,然后再對其進行重新聚合。我使用任意TOP的(每次運行查詢時行可能不同),因為序數引數僅(當前)在 Azure SQL 資料庫中可用;希望序數引數將在 SQL Server 2022 中。
無論如何,這行得通,但再次修復您的設計,并在應用程式層執行此操作。如果您不在 SQL Server 2017 上,那么這是必須的,而不是非常強烈的建議。
DECLARE @Values int = 2; --parameterised, but you could hard code
WITH Split AS(
SELECT SS.[value],
COUNT(*) OVER () AS [Rows]
FROM (VALUES('Apple,Banana,Cashew,Doughnut,Elephant,Fish'))V(YourDenormalisedData)
CROSS APPLY STRING_SPLIT(V.YourDenormalisedData,',') SS),
ArbitraryTop AS(
SELECT TOP (@Values)
value,
[Rows]
FROM Split)
SELECT STRING_AGG(value,', ')
CASE WHEN MAX([Rows]) > @Values THEN CONCAT(' ',MAX([Rows])-@Values,' others') ELSE '' END
FROM ArbitraryTop;
uj5u.com熱心網友回復:
就像其他人所說的那樣,這不是 SQL Server 的作業,而是您的表示層
如果你必須在sql中做,那么也許你可以這樣做。
我確實假設您在多行中有這些值,并且需要通過一些分組將它們連接起來,如果沒有,那么在您的問題中更清楚
select o.id,
( select top 2 string_agg(o2.name, ', ')
from ( select top 2 o2.name,
o2.id
from object o2
where o2.id = o.id
) o2
where o2.id = o.id
) ' and ' convert(varchar(50), count(o.name) - 2) ' others'
from object o
group by o.id
在此處查看此DBFiddle以查看它的作業原理
結果看起來像這樣
| ID | (無列名) |
|---|---|
| 1 | 蘋果、香蕉和其他 4 家 |
| 2 | Peer、鯰魚和其他 0 人 |
| 3 | 狗、鯨魚和其他 1 人 |
如果您不想顯示“和 x 其他人”,您可以像這樣更改查詢
select o.id,
( select top 2 string_agg(o2.name, ', ')
from ( select top 2 o2.name, o2.id
from object o2
where o2.id = o.id
) o2
where o2.id = o.id
) case when count(o.name) > 2 then ' and ' convert(varchar(50), count(o.name) - 2) ' others'
else ''
end
from object o
group by o.id
| ID | (無列名) |
|---|---|
| 1 | 蘋果、香蕉和其他 4 家 |
| 2 | 同行,鯰魚 |
| 3 | 狗、鯨魚和其他 1 人 |
編輯不支持 string_agg 的 sql server
如果您有不支持該string_agg功能的舊版本的 sql server,您可以使用它XML PATH來連接值并stuff洗掉額外的,
select o.id,
stuff(( select top 2 ', ' o2.name
from ( select top 2 o2.name,
o2.id
from object o2
where o2.id = o.id
) o2
where o2.id = o.id
for XML PATH('')
), 1, 2, ''
) case when count(o.name) > 2 then ' and ' convert(varchar(50), count(o.name) - 2) ' others'
else ''
end
from object o
group by o.id
DBFiddle 使用 XML 路徑
結果將再次相同
uj5u.com熱心網友回復:
這個任務可以通過游標的力量來解決,通過使用函式split_string將行除以分隔符。
--@string - our input string
DECLARE @string NVARCHAR(MAX) = 'Apple,Banana,Cashew,Doughnut,Elephant,Fish';
--@count - the number of words in @string
DECLARE @count INT = 0;
--@countrestwords - count of rest words
DECLARE @countrestwords INT = 0;
--@resultstring - result string
DECLARE @resultstring NVARCHAR(MAX) = '';
DECLARE stringcursor CURSOR FOR
SELECT
VALUE
FROM string_split(@string,',')
OPEN stringcursor
FETCH FROM stringcursor INTO @string
WHILE @@FETCH_STATUS = 0
BEGIN
IF @count = 0
BEGIN
SET @resultstring = @string;
END
ELSE IF @count = 1
BEGIN
SET @resultstring = @resultstring ',' @string ;
END
ELSE
BEGIN
SET @resultstring = @resultstring;
SET @countrestwords = @countrestwords 1;
--SELECT @countrestwords
END
SET @count = @count 1;
FETCH NEXT FROM stringcursor INTO @string
END
CLOSE stringcursor
DEALLOCATE stringcursor
SELECT @resultstring ' ' CONVERT(NVARCHAR(MAX),@countrestwords) ' others' ;
GO
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/466139.html
上一篇:SQL更新按特定日期排序的重復項
