如果有人能在下面的建議我會很感激: 我的表看起來像這樣:的毗連幾行成一個在T-SQL中使用分組
ID SEQ ACCOUNT AMOUNT DESCRIPTION ... ....
0719 1 8019 222,2 this is the
0719 1 NULL NULL description of
0719 1 NULL NULL account with
0719 1 NULL NULL amount= 222,2
0719 1 NULL NULL NULL
0719 1 NULL NULL NULL
0719 2 8019 111,1 this is the
0719 2 NULL NULL description of
0719 2 NULL NULL account with
0719 2 NULL NULL amount= 111,1
正如你可以看到有一個ID
和一個ACCOUNT
與幾個AMOUNTS
。 我需要結合SEQ
分組的每個條目的DESCRIPTION
列。
我的目標是:
ID SEQ ACCOUNT AMOUNT DESCRIPTION ... ...
0719 1 8019 222,2 this is the description of account with amount= 222,2
0719 2 8019 111,1 this is the description of account with amount= 111,1
我試圖用COALESCE
或FOR XML
運營商,但不能被SEQ
那裏添加分組:
DECLARE @Desc NVARCHAR(8000)
SELECT @Desc = COALESCE(@Desc + ', ', '') + [DESCRIPTION]
FROM [TABLE]
WHERE MDC_ID = '0719'
AND (ACCOUNT = '8019' or ACCOUNT IS NULL)
AND (AMOUNT= 222,2 OR AMOUNT is null)
--GROUP BY SEQ -- DESCRIPTION is invalid in the select list because it is not contained in
--either an aggregate function or the GROUP BY clause
SELECT @Desc
我怎樣才能改變我的腳本?
寫得很好questrion但它的一個副本。答案很混亂。 http://stackoverflow.com/questions/451415/simulating-group-concat-mysql-function-in-microsoft-sql-server-2005 – Jodrell
投票保持開放按照http://meta.stackexchange.com/questions/ 139961/whats-the-policy-on-closing-unique-questions-with-overlapping-but-non-duplicat –