2017-01-24 131 views
0

我參考了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; 
+0

當LEFT JOIN,把右側表的條件在ON子句中得到真正的左聯接行爲。 (當在何處,你得到正規內加入的結果。) – jarlh

+0

@jarlh:好,這似乎是他的數據庫模式,還需要其他兩個表拿到最後兩個 –

+1

@TimSchmelter,當然...謝謝!似乎我需要另一杯咖啡... – jarlh

回答

0

我覺得這是存檔的最佳方式你的目標。

SELECT is_active, paid, 
tags_title = STUFF(
      (SELECT ',' + tags_title FROM table FOR XML PATH ('')), 1, 1, '' 
      ) 
FROM table GROUP BY is_active, paid 
+0

這是我所追求的,但結果並沒有到來 – sunleo

0

通過添加STUFF領域,也許你能避免使用:

LEFT JOIN task_tracker_tags ttt 
    ON ttt.tags_id = tttm.tags_id 


SELECT proj.is_active, 
     pal.paid, 

     title = STUFF(
       (SELECT ',' + ttt.tags_title 
        FROM task_tracker_tags ttt 
        WHERE ttt.tags_id = tttm.tags_id 
        FOR XML PATH ('')), 1, 1, '') 

FROM person_aim_list pal 
     LEFT JOIN time_tace_map ttm 
       ON ttm.artifact_id = pal.paid 
     LEFT JOIN empires emp 
       ON emp.empire_id = ttm.empire_id 
     LEFT JOIN task_tracker_tags_mapper tttm 
       ON tttm.artifact_id = pal.paid 
     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.empire_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, 
     pal.paid; 
+0

感謝您的答覆,這是行不通的,只有第一個結果不預期之一。 – sunleo

0

你可以做到這一點像

SELECT DISTINCT proj.is_active, 
     pal.paid 
     , STUFF((
      SELECT ',' + ttt.tags_title 
      FROM task_tracker_tags T 
      WHERE T.tags_id = tttm.tags_id 
      FOR XML PATH('') 
      ), 1, 1, '') AS tags_title 

FROM person_aim_list pal 
     LEFT JOIN time_tace_map ttm 
       ON ttm.artifact_id = pal.paid 
     LEFT JOIN empires emp 
       ON emp.empire_id = ttm.empire_id 
     LEFT JOIN task_tracker_tags_mapper tttm 
       ON tttm.artifact_id = pal.paid 
     /* removed 
     LEFT JOIN task_tracker_tags ttt 
       ON ttt.tags_id = tttm.tags_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.empire_id IN (3932, 1733) 
     AND proj.is_active = 1 
     AND pal.due_date <= '01/02/2017' 
     AND pal.due_date >= '01/01/1800' 

編輯:

在你更新你提到的4列像

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 

您需要將其更改爲

SELECT DISTINCT proj.is_active ---, tttm.tags_id, Here your query making wrong output. 
     ,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 
+0

你試過了嗎? @sunleo –

+0

您是否刪除了我上面刪除的左連接?我認爲沒有辦法首先顯示結果。 @sunleo –

+0

將其粘貼到您的問題本身@sunleo –