下面是一個完整的頭腦彎曲相當於例如用於SQL Server 2005及以後:
create table #Temp
(GroupField int, ValueType varchar(10), Value varchar(2048))
insert into #Temp (GroupField, ValueType, Value) VALUES (1, 'image', 'http://1')
insert into #Temp (GroupField, ValueType, Value) VALUES (1, 'image', 'http://2')
insert into #Temp (GroupField, ValueType, Value) VALUES (2, 'text', 'none')
insert into #Temp (GroupField, ValueType, Value) VALUES (2, 'video', '30mins')
insert into #Temp (GroupField, ValueType, Value) VALUES (2, 'image', 'http://5')
insert into #Temp (GroupField, ValueType, Value) VALUES (3, 'image', 'http://4')
insert into #Temp (GroupField, ValueType, Value) VALUES (3, 'text', 'hello')
insert into #Temp (GroupField, ValueType, Value) VALUES (3, 'image', 'http://7')
insert into #Temp (GroupField, ValueType, Value) VALUES (4, 'image', 'http://0')
SELECT GroupField, ValueType,
LEFT([Values],LEN([Values]) - 1) AS [Values]
FROM (SELECT GroupField, ValueType,
(SELECT value + ', ' AS [text()]
FROM #Temp AS internal
WHERE internal.GroupField = GroupFields.GroupField and internal.ValueType = GroupFields.ValueType
FOR xml PATH ('')
) AS [Values]
FROM (SELECT GroupField, ValueType
FROM #Temp
GROUP BY GroupField, ValueType) AS GroupFields) AS pre_trimmed;
產生的結果爲:
GroupField ValueType Values
1 image http://1, http://2
2 image http://5
2 text none
2 video 30mins
3 image http://4, http://7
3 text hello
4 image http://0
這可能需要一些ORDER BY
條款對你的具體情況?改編自Rational Relational - Emulating MySQL’s GROUP_CONCAT() Function in SQL Server 2005
偉大的作品,但你也可以告訴我,我怎麼只分組圖像,避免文本分組呢? – Basit 2009-09-11 03:27:59
解決方案: SELECT @ groupe,@type,type,GROUP_CONCAT(thumbnail),count(*),date_format(created_date,'%H%k%I%r%T%S%w') FROM( SELECT @group := @group +(NOT(COALESCE(@type,type)= type)or type ='feed')AS grouper, @type:= type, m。* FROM( SELECT @group:= 0, @type:= NULL )瓦爾, 飼料米 爲了通過CREATED_DATE降序 )● GROUP BY石斑魚 – Basit 2009-09-11 05:51:14
這個查詢是工作正常,但直到我開始顯示其他用戶的媒體進料還,這意味着它也有組媒體作爲同一個用戶..我們如何分離即使下一行與前一行相同,也可以分別爲每個不同的用戶分組。 – Basit 2010-01-25 14:24:39