我有以下列和列的数据类型
DailyWorkingHours as Daily (money)
case_completion_hour as casecomp (money)
xdate as month (datetime)
我有以下查询
select
sum(cpw.DailyWorkingHours) as Daily,
sum(ca.case_completion_hour) / 60 as casecomp,
DateName( month , DateAdd( month , month(ca.xdate) , 0 ) - 1 ) as month
from reseller_user_profiles rup
join cases c on c.reseller_user_ID=rup.reseller_user_ID
join case_action ca on ca.case_ID=c.case_ID
join tblCaseProjectWorkers cpw on c.reseller_user_ID=cpw.resellerUserID
join tblCaseProjectsNew cpn on cpn.ID=c.project_ID
group by
DateName( month , DateAdd( month , month(ca.xdate) , 0 ) - 1 )
order by DateName( month , DateAdd( month , month(ca.xdate) , 0 ) - 1 )
如果我运行查询,它将返回如下数据
Daily (column) - casecomp (column) - month (column)
1088,00 - 0,3333 - February
544,00 - 0,3344 - February
321,00 - 0,3377 - February
150,00 - 0,3387 - January
332,00 - 0,3330 - January
658,00 - 4,3331 - April
问题:
如何选择查询结果如下(即使月份为空,我想显示12个月)
Daily (column) - casecomp (column) - month (column)
150,00 - 0,3387 - January
332,00 - 0,3330 -
1088,00 - 0,3333 - February
544,00 - 0,3344 -
321,00 - 0,3377 -
NULL - NULL - March
658,00 - 4,3331 - April
NULL - NULL - May
NULL - NULL - June
NULL - NULL - July
NULL - NULL - August
NULL - NULL - September
NULL - NULL - October
NULL - NULL - November
NULL - NULL - December
在结果查询中按月内列进行月份列分组。根据实现这个,我的查询代码中要改变什么?
答案 0 :(得分:0)
试一试。
CREATE TABLE #Months (Num int, Name varchar(15))
INSERT INTO #Months SELECT 1, 'January'
INSERT INTO #Months SELECT 2, 'February'
INSERT INTO #Months SELECT 3, 'March'
INSERT INTO #Months SELECT 4, 'April'
INSERT INTO #Months SELECT 5, 'May'
INSERT INTO #Months SELECT 6, 'June'
INSERT INTO #Months SELECT 7, 'July'
INSERT INTO #Months SELECT 8, 'August'
INSERT INTO #Months SELECT 9, 'September'
INSERT INTO #Months SELECT 10, 'October'
INSERT INTO #Months SELECT 11, 'November'
INSERT INTO #Months SELECT 12, 'December'
SELECT *
FROM
#Months m
LEFT JOIN
(
select
sum(cpw.DailyWorkingHours) as Daily,
sum(ca.case_completion_hour) / 60 as casecomp,
DateName( month , DateAdd( month , month(ca.xdate) , 0 ) - 1 ) as month
from reseller_user_profiles rup
join cases c on c.reseller_user_ID=rup.reseller_user_ID
join case_action ca on ca.case_ID=c.case_ID
join tblCaseProjectWorkers cpw on c.reseller_user_ID=cpw.resellerUserID
join tblCaseProjectsNew cpn on cpn.ID=c.project_ID
group by
DateName( month , DateAdd( month , month(ca.xdate) , 0 ) - 1 )
) yours
ON yours.month = m.Name
ORDER BY m.Num
答案 1 :(得分:0)
试试这个:
;with Months as (
select 1 As Months_Number,
DateName(month, '2015-01-01') As Months_Name
UNION ALL
select Months_Number + 1 , DateName(month, DateAdd(month, Months_Number, '2015-01-01')) As Months_Name
FROM Months
WHERE Months_Number < 12
)
select
sum(cpw.DailyWorkingHours) as Daily,
sum(ca.case_completion_hour) / 60 as casecomp,
Months_Name
from Months
left join (
reseller_user_profiles rup
join cases c on c.reseller_user_ID=rup.reseller_user_ID
join case_action ca on ca.case_ID=c.case_ID
join tblCaseProjectWorkers cpw on c.reseller_user_ID=cpw.resellerUserID
join tblCaseProjectsNew cpn on cpn.ID=c.project_ID) on month(ca.xdate) = Months_Number
group by Months_Name
order by Months_Name
兴趣点:
CTE
为您提供从0到11的计数。这样可以实现简单而准确的left join
条件。 (参见我对EarlOfEnnui的回答的评论)CTE
还会为您提供月份名称。这允许更简单的group by
和order by
条款。left join
的{{1}}与您的所有其他表格相同(请注意,所有其他表格都有括号