我提到了this LINK但是当我尝试使用相同的格式时,我很困惑我是否必须使用重复查询。但是我的查询过于冗长。请帮助我。
我的查询给出了以下结果:
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 :(得分: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
答案 1 :(得分: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;
答案 2 :(得分: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