我创建了一个查询,用于从发票表中提取年度销售总额,但是当我运行查询时,我得到了很多0列和欺骗行数据,因为不是每年都有,而且麻烦有销售。这是我的代码。
SELECT * INTO #REVTEMP FROM
(
SELECT
YEAR(CONVERT(CHAR(10), DATEADD(d, INVC_RDAT +5843, 0), 1)) [YEAR]
,ISNULL(b.BILLTO_NUMB,c.BILLTO_NUMB)[BILL TO NUMB]
,ISNULL(B.BILLTO_NAME, C.BILLTO_NAME) [CLIENT]
,SUM(CAST(NET_DLR AS MONEY)) [SALES REVENUE]
FROM placeholder.JR80 a
left outer join placeholder.DBO.JC10 b on b.JOB_NUMB = a.JOB_NUMB
left outer join placeholder.DBO.OE10 C ON C.ORD_NUMB = A.JOB_NUMB
--where b.BILLTO_NUMB = 13651
GROUP BY
b.BILLTO_NUMB
,C.BILLTO_NUMB
,YEAR(CONVERT(CHAR(10), DATEADD(d, INVC_RDAT +5843, 0), 1))
,B.BILLTO_NAME
,C.BILLTO_NAME
--order by YEAR(CONVERT(CHAR(10), DATEADD(d, INVC_RDAT +5843, 0), 1))
) AS #REVETEMP
SELECT
#REVTEMP.[BILL TO NUMB]
,CASE WHEN #REVTEMP.YEAR = 2006 THEN #REVTEMP.[SALES REVENUE] ELSE 0 END AS [2006 REVENUE]
,CASE WHEN #REVTEMP.YEAR = 2007 THEN #REVTEMP.[SALES REVENUE] ELSE 0 END AS [2007 REVENUE]
,CASE WHEN #REVTEMP.YEAR = 2008 THEN #REVTEMP.[SALES REVENUE] ELSE 0 END AS [2008 REVENUE]
,CASE WHEN #REVTEMP.YEAR = 2009 THEN #REVTEMP.[SALES REVENUE] ELSE 0 END AS [2009 REVENUE]
,CASE WHEN #REVTEMP.YEAR = 2010 THEN #REVTEMP.[SALES REVENUE] ELSE 0 END AS [2010 REVENUE]
,CASE WHEN #REVTEMP.YEAR = 2011 THEN #REVTEMP.[SALES REVENUE] ELSE 0 END AS [2011 REVENUE]
,CASE WHEN #REVTEMP.YEAR = 2012 THEN #REVTEMP.[SALES REVENUE] ELSE 0 END AS [2012 REVENUE]
,CASE WHEN #REVTEMP.YEAR = 2013 THEN #REVTEMP.[SALES REVENUE] ELSE 0 END AS [2013 REVENUE]
,CASE WHEN #REVTEMP.YEAR = 2014 THEN #REVTEMP.[SALES REVENUE] ELSE 0 END AS [2014 REVENUE]
,CASE WHEN #REVTEMP.YEAR = 2015 THEN #REVTEMP.[SALES REVENUE] ELSE 0 END AS [2015 REVENUE]
,CASE WHEN #REVTEMP.YEAR = 2016 THEN #REVTEMP.[SALES REVENUE] ELSE 0 END AS [2016 REVENUE]
FROM #REVTEMP
DROP TABLE #REVTEMP
我的结果看起来像这样
BILL TO NUMB 2006 REVENUE 2007 REVENUE 2008 REVENUE 2009 REVENUE 2010 REVENUE 2011 REVENUE 2012 REVENUE 2013 REVENUE 2014 REVENUE 2015 REVENUE 2016 REVENUE
00022 0.00 726.28 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
00022 0.00 0.00 0.00 1835.79 0.00 0.00 0.00 0.00 0.00 0.00 0.00
00022 0.00 0.00 0.00 0.00 802.21 0.00 0.00 0.00 0.00 0.00 0.00
00022 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
00027 0.00 346.22 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
00037 0.00 0.00 0.00 1242.78 0.00 0.00 0.00 0.00 0.00 0.00 0.00
00037 0.00 0.00 0.00 1940.50 0.00 0.00 0.00 0.00 0.00 0.00 0.00
00037 0.00 0.00 0.00 0.00 255.21 0.00 0.00 0.00 0.00 0.00 0.00
00123 0.00 986.20 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
我希望每个麻烦只有一行包含他们所有的数据。我想我可能会过度思考我的过程,但我无法弄清楚如何摆脱额外的行。
答案 0 :(得分:1)
如果我理解你的要求,你需要一个小组并汇总案例陈述(总和似乎是一个好主意)所以:
SELECT
#REVTEMP.[BILL TO NUMB]
,sum(CASE WHEN #REVTEMP.YEAR = 2006 THEN #REVTEMP.[SALES REVENUE] ELSE 0 END) AS [2006 REVENUE]
,sum(CASE WHEN #REVTEMP.YEAR = 2007 THEN #REVTEMP.[SALES REVENUE] ELSE 0 END) AS [2007 REVENUE]
,sum(CASE WHEN #REVTEMP.YEAR = 2008 THEN #REVTEMP.[SALES REVENUE] ELSE 0 END) AS [2008 REVENUE]
,sum(CASE WHEN #REVTEMP.YEAR = 2009 THEN #REVTEMP.[SALES REVENUE] ELSE 0 END) AS [2009 REVENUE]
,sum(CASE WHEN #REVTEMP.YEAR = 2010 THEN #REVTEMP.[SALES REVENUE] ELSE 0 END) AS [2010 REVENUE]
,sum(CASE WHEN #REVTEMP.YEAR = 2011 THEN #REVTEMP.[SALES REVENUE] ELSE 0 END) AS [2011 REVENUE]
,sum(CASE WHEN #REVTEMP.YEAR = 2012 THEN #REVTEMP.[SALES REVENUE] ELSE 0 END) AS [2012 REVENUE]
,sum(CASE WHEN #REVTEMP.YEAR = 2013 THEN #REVTEMP.[SALES REVENUE] ELSE 0 END) AS [2013 REVENUE]
,sum(CASE WHEN #REVTEMP.YEAR = 2014 THEN #REVTEMP.[SALES REVENUE] ELSE 0 END) AS [2014 REVENUE]
,sum(CASE WHEN #REVTEMP.YEAR = 2015 THEN #REVTEMP.[SALES REVENUE] ELSE 0 END) AS [2015 REVENUE]
,sum(CASE WHEN #REVTEMP.YEAR = 2016 THEN #REVTEMP.[SALES REVENUE] ELSE 0 END) AS [2016 REVENUE]
FROM #REVTEMP
group by #REVTEMP.[BILL TO NUMB]