我參考了this LINK,但是當我嘗試使用相同的格式時,我很困惑我是否必須使用重複查詢。是否因爲太冗長請幫助我這個。Group By和STUFF在sql server中的組合結果
我的查詢,給出以下結果:
is_active, paid, tags_title
1 20 Testing
1 20 Development
1 21 Development
1 21 Testing
1 22 UI
要求的結果:
is_active, paid, tags_title
1 20 Testing, Development
1 21 Testing, Development
1 22 UI
更新精確以下查詢:
SELECT DISTINCT proj.is_active, tttm.tags_id,
pal.project_artifact_id
, STUFF((
SELECT ',' + t.tags_title
FROM task_tracker_tags T
WHERE T.tags_id = tttm.tags_id
FOR XML PATH('')
), 1, 1, '') AS tags_title
FROM project_artifact_list pal
LEFT JOIN task_tracker_mapper ttm
ON ttm.artifact_id = pal.Project_Artifact_Id
LEFT JOIN employees emp
ON emp.employee_id = ttm.employee_id
LEFT JOIN task_tracker_tags_mapper tttm
ON tttm.artifact_id = pal.Project_Artifact_id
LEFT JOIN projects proj
ON proj.project_id = tttm.project_id
WHERE pal.Child_Priority IN ('High','Low')
AND pal.artifact_status IN ('Open')
AND emp.employee_id IN (3932,1733)
AND proj.is_active = 1
AND pal.Due_Date <= '01/02/2017'
AND pal.Due_Date >= '01/01/1800'
GROUP BY proj.is_active,Project_Artifact_Id,tttm.tags_id;
當LEFT JOIN,把右側表的條件在ON子句中得到真正的左聯接行爲。 (當在何處,你得到正規內加入的結果。) – jarlh
@jarlh:好,這似乎是他的數據庫模式,還需要其他兩個表拿到最後兩個 –
@TimSchmelter,當然...謝謝!似乎我需要另一杯咖啡... – jarlh