今天是个好日子
我有这个代码
SELECT
'Expired Item -'+ DateName(mm,DATEADD(MM,4,AE.LOAN)) as [Month]
,COUNT(ISNULL(PIT.ID,0))'COUNT'
,SUM(ISNULL(PIT.KGRAM,0))'GRAMS'
,SUM(ISNULL(PH.AMOUNT,0))'PRINCIPAL'
FROM #AllExpired AE
INNER JOIN Transactions.ITEM PIT
ON AE.MAINID=PIT.MAINID
INNER JOIN Transactions.HISTO PH
ON AE.MAINID=PH.MAINID
GROUP BY DATENAME(MM,(DATEADD(MM,4,AE.LOAN)))
UNION ALL
/*SELECT EXPIRED AFTER 5 MONTHS*/
SELECT
'Expired Item -'+ DateName(mm,DATEADD(MM,5,AE.LOAN)) as [Month]
,COUNT(ISNULL(PIT.ID,0))'COUNT'
,SUM(ISNULL(PIT.KGRAM,0))'GRAMS'
,SUM(ISNULL(PH.AMOUNT,0))'PRINCIPAL'
FROM #ExpAfterFiveMonths E5
INNER JOIN Transactions.ITEM PIT
ON E5.MAINID=PIT.MAINID
INNER JOIN Transactions.HISTO PH
ON E5.MAINID=PH.MAINID
INNER JOIN #AllExpired AE
ON AE.MAINID=E5.MAINID
GROUP BY DATENAME(MM,(DATEADD(MM,5,AE.LOAN)))
UNION ALL
/*SELECT EXPIRED AFTER 6 MONTHS*/
SELECT
'Expired Item -'+ DateName(mm,DATEADD(MM,6,AE.LOAN)) as [Month]
,COUNT(ISNULL(PIT.ID,0))'COUNT'
,SUM(ISNULL(PIT.KGRAM,0))'GRAMS'
,SUM(ISNULL(PH.AMOUNT,0))'PRINCIPAL'
FROM #ExpAfterSixMonths E6
INNER JOIN Transactions.ITEM PIT
ON E6.MAINID=PIT.MAINID
INNER JOIN Transactions.HISTO PH
ON E6.MAINID=PH.MAINID
INNER JOIN #AllExpired AE
ON AE.MAINID=E6.MAINID
GROUP BY DATENAME(MM,(DATEADD(MM,6,AE.LOAN)))
它工作正常,问题是当 Select 语句没有检索到任何行时,它们变为空而不是替换零
而不是用 0 0 0 生成单词月份,它只是弹出我不喜欢的空,,
你能帮我实现吗?结果应该是这样的
------------------------------------------------------------------
MONTH | Count | Grams | Principal |
October |123123 | 123123 | 123123213 |
November | 0 | 0 | 0 | // this should appear if no rows where selected instead of blank
这是我在 temptables 中生成项目的代码
SELECT TE.MAINID
,TE.EXPIRY
,TE.LOAN
,PM.STORAGE
into #AllExpiredAfterFiveAndSix
FROM #ExpiredAfterFiveandSixMon TE
inner join Transactions.TABLEMAIN PM
on TE.MAINID = PM.MAINID
inner join #AllExpired E4
on E4.MAINID=TE.MAINID
WHERE ((cast(TE.EXPIRY as date) < cast(TE.newloandate as date))
OR(TE.NewLoanDate is null and ((cast(TE.EXPIRY as date) < cast(PM.DATERED as date)) or PM.STATUS = 7 or PM.STATUS = 5)) )
AND (PM.STORAGE BETWEEN 3 AND 14 OR PM.STORAGE=17)
/*EXPIRED AFTER 5 MONTHS*/
select AE.MAINID
,AE.LOAN
,AE.STORAGE
,ae.EXPIRY
into #ExpAfterFiveMonths
from #AllExpiredAfterFiveAndSix AE
inner join #AllExpired E4
on E4.MAINID=AE.MAINID
where MONTH(AE.EXPIRY)= MONTH(dateadd(mm,1,E4.EXPIRY))
/*EXPIRED AFTER 6 MONTHS*/
select AE.MAINID
,AE.LOAN
,AE.STORAGE
,ae.EXPIRY
into #ExpAfterSixMonths
from #AllExpiredAfterFiveAndSix AE
inner join #AllExpired E4
on E4.MAINID=AE.MAINID
where MONTH(AE.EXPIRY)= MONTH(dateadd(mm,2,E4.EXPIRY))
CREATE NONCLUSTERED INDEX IDX_ExpAfterFiveMonths ON #ExpAfterFiveMonths(MAINID)
CREATE NONCLUSTERED INDEX IDX_ExpAfterSixMonths ON #ExpAfterSixMonths(MAINID)
我希望你能帮助我,因为我只是 sql 的初学者
我已经尝试使用 is NULL 正如您在上面看到的那样,但我不知道我是否正确实施它
希望您的好意和考虑谢谢:)
****编辑**
临时表不包含任何值,我只希望它输出 0 而不是空白。