这是我的代码。谁能告诉我哪些或哪里不正确?我花了3天多的时间试图找出它并且没有看到它,我的报告将在下周发布。
SELECT
t1.IT1 as "Issue Type",
t1.sum(REC) as "Total Received",
t2.sum(RES) as "Total Resolved",
t1.Count(project_key) as REC,
t2.Count(project_key) as RES
FROM
(select
IT1 as "Issue Type",
sum(REC) as "Total Received"
FROM
(case
when issue_type = 'MIS Sub-task' then 'MIS'
when issue_type = 'Reporting Sub-task' then 'Reporting'
when issue_type = 'NICE Analytics Sub-task' then 'NICE Analytics'
when issue_type = 'Demand Mitigation Sub-task' then 'Demand Mitigation'
else issue_type
end as IT1,
Count(project_key) as REC
FROM jira17.FCI4JIRA_common_view
WHERE issue_type in ('MIS', 'MIS Sub-task', 'Reporting', 'Reporting Sub-Task', 'NICE Analytics', 'NICE Analytics Sub-Task', 'Demand Mitigation', 'Demand Mitigation Sub-Task') and
status not like 'Cancelled' and
date_created between to_date ('01/12/2013', 'dd/mm/yyyy') and to_date ('01/01/2014', 'dd/mm/yyyy') group by issue_type) group by IT1) t1
inner join t2 on t1.IT1 = t2.IT2
(select
IT2 as "Issue Type",
sum(RES) as "Total Resolved"
FROM
(case
when issue_type = 'MIS Sub-task' then 'MIS'
when issue_type = 'Reporting Sub-task' then 'Reporting'
when issue_type = 'NICE Analytics Sub-task' then 'NICE Analytics'
when issue_type = 'Demand Mitigation Sub-task' then 'Demand Mitigation'
else issue_type
end as IT2,
Count(project_key) as RES
from jira17.FCI4JIRA_common_view
where issue_type in ('MIS', 'MIS Sub-task', 'Reporting', 'Reporting Sub-Task', 'NICE Analytics', 'NICE Analytics Sub-Task', 'Demand Mitigation', 'Demand Mitigation Sub-Task') and
status not like 'Cancelled' and
date_resolved between to_date ('01/12/2013', 'dd/mm/yyyy') and to_date ('01/01/2014', 'dd/mm/yyyy') group by issue_type) group by IT2) t2
答案 0 :(得分:2)
这是一个非常复杂的SQL句子。因为这个,你的问题是不可读的。社区无法花时间调试您的查询。你应该能够自己做。我建议你“划分和统治”,因为这样,你应该了解CTE的表达方式。使用CTE,您可以以更易读的方式组织查询。
对于你的句子:
With T11 as (
select
case
when issue_type = 'MIS Sub-task' then 'MIS'
when issue_type = 'Reporting Sub-task' then 'Reporting'
when issue_type = 'NICE Analytics Sub-task' then 'NICE Analytics'
when issue_type = 'Dema...gation Sub-task' then 'Demand Mitigation'
else issue_type
end as IT1,
Count(project_key) as REC
FROM
jira17.FCI4JIRA_common_view
WHERE issue_type in ('MIS', 'MIS Sub-task', 'Reporting', ...) and
status not like 'Cancelled' and
date_created between to_date ('01/12/2013', 'dd/mm/yyyy')
and to_date ('01/01/2014', 'dd/mm/yyyy')
GROUP BY issue_type ),
T1 as (
SELECT
t1.IT1 as "Issue Type",
t1.sum(REC) as "Total Received",
t2.sum(RES) as "Total Resolved",
t1.Count(project_key) as REC,
t2.Count(project_key) as RES
FROM
FROM T11
group by IT1 ),
-- and so on ...
"How to use SQL Server CTEs to make your T-SQL code readable by humans"是T-SQL的帖子,但您可以从中学习。