我有一个问题,关于sql中的SUM函数给出NULL,因此制动表的所有结构:(x-month,y-cities,value-Nettotal)但是我根据y错误地排列了值-cities,因为没有空闲空间,我的意思是0值,这里是截图,使其更清晰:
以及我之前询问的问题的链接,但无法得到明确答案:group by cities
所以我和Dave DuPlantis到目前为止写的是:
查询:
<cfquery name="GET_SALES_TOTAL" datasource="#dsn#">
SELECT SUM(COALESCE(NETTOTAL,0)) NETTOTAL,
SC.CITY_ID,
SC.CITY_NAME,
M.INVOICE_MONTH
FROM SETUP_CITY SC
LEFT OUTER JOIN COMPANY C
ON SC.CITY_ID = C.CITY
LEFT OUTER JOIN #DSN2_ALIAS#.INVOICE I
ON C.COMPANY_ID = I.COMPANY_ID
,
(
SELECT DISTINCT DATEPART(MM,INVOICE_DATE) INVOICE_MONTH
FROM #DSN2_ALIAS#.INVOICE
) M
WHERE PURCHASE_SALES = 1
AND DATEPART(MM,I.INVOICE_DATE) = M.INVOICE_MONTH
AND SC.COUNTRY_ID=1
GROUP BY M.INVOICE_MONTH,
SC.CITY_ID,
SC.CITY_NAME
ORDER BY M.INVOICE_MONTH,
SC.CITY_ID,
SC.CITY_NAME
</cfquery>
和表格:
<table cellpadding="3" cellspacing="1" class="color-border">
<tr class="color-header">
<td class="txtbold" nowrap width="100">Aylar / Sehirler</td>
<cfoutput query="GET_SALES_GRAND_TOTAL" group="city_id">
<td class="txtbold">#city_name#</td>
</cfoutput>
</tr>
<cfoutput query="GET_SALES_TOTAL" group="invoice_month"><!--- months first --->
<tr class="color-row"><!--- month-specific stuff goes here --->
<td class="txtbold">
#invoice_month#
</td>
<cfoutput group="city_id"><!--- city-specific stuff --->
<td>
#tlformat(nettotal,2)#<!--- format NETTOTAL however you want here --->
</td>
</cfoutput>
</tr>
</cfoutput>
</table>
有没有人有想法解决这类问题?!谢谢大家的帮助!
答案 0 :(得分:1)
正如Leigh所观察到的,每个城市需要相同的月份才能根据需要填充输出 - 通过将发票链接到WHERE子句中的子查询M,您已将其转换为内部联接。将查询修改为类似以下的内容应该有效:
<cfquery name="GET_SALES_TOTAL" datasource="#dsn#">
SELECT SUM(COALESCE(NETTOTAL,0)) NETTOTAL,
SC.CITY_ID,
SC.CITY_NAME,
M.INVOICE_MONTH
FROM SETUP_CITY SC
LEFT OUTER JOIN COMPANY C
ON SC.CITY_ID = C.CITY
CROSS JOIN
( SELECT DISTINCT DATEPART(MM,INVOICE_DATE) INVOICE_MONTH
FROM #DSN2_ALIAS#.INVOICE
) M
LEFT OUTER JOIN #DSN2_ALIAS#.INVOICE I
ON C.COMPANY_ID = I.COMPANY_ID
AND DATEPART(MM,I.INVOICE_DATE) = M.INVOICE_MONTH
WHERE PURCHASE_SALES = 1
AND SC.COUNTRY_ID=1
GROUP BY M.INVOICE_MONTH,
SC.CITY_ID,
SC.CITY_NAME
ORDER BY M.INVOICE_MONTH,
SC.CITY_ID,
SC.CITY_NAME
</cfquery>