没有选中的行时生成零

时间:2013-10-04 07:25:03

标签: sql tsql reporting-services sql-server-2008-r2 isnull

每一天的好日子

我有这个代码

 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它只是弹出我不喜欢的空白,

你可以帮我解决这个问题吗?结果应该是这样的

------------------------------------------------------------------  
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而不是空白。

3 个答案:

答案 0 :(得分:2)

编辑:添加了一个月表来处理空的临时表。

将内部联接更改为左联接。

DECLARE @months TABLE (ReportMonth VARCHAR(20) NOT NULL)
INSERT INTO @months VALUES 
('January'),('February'),('March'),('April'),
('May'),('June'),('July'),('August'),
('September'),('October'),('November'),('December')


SELECT 
    'Expired Item -'+ COALESCE(DateName(mm,DATEADD(MM,4,AE.fld_LoanDate)), M.ReportMonth) as [Month]
    ,COUNT(ISNULL(PIT.fld_PawnItemID,0))'COUNT'
    ,SUM(ISNULL(PIT.fld_KaratGram,0))'GRAMS'
    ,SUM(ISNULL(PH.fld_PrincipalAmt,0))'PRINCIPAL'
FROM 
    @months M
    LEFT JOIN #AllExpired AE
    ON M.ReportMonth = DateName(mm,DATEADD(MM,4,AE.fld_LoanDate))
    LEFT JOIN Transactions.tbl_PawnItem PIT
    ON AE.fld_PawnMainID=PIT.fld_PawnMainID
    LEFT JOIN Transactions.tbl_PawnHisto PH
    ON AE.fld_PawnMainID=PH.fld_PawnMainID
GROUP BY M.ReportMonth

UNION ALL

/*SELECT EXPIRED AFTER 5 MONTHS*/
SELECT 
    'Expired Item -'+ 'Expired Item -'+ COALESCE(DateName(mm,DATEADD(MM,5,AE.fld_LoanDate)), M.ReportMonth) as [Month]
    ,COUNT(ISNULL(PIT.fld_PawnItemID,0))'COUNT'
    ,SUM(ISNULL(PIT.fld_KaratGram,0))'GRAMS'
    ,SUM(ISNULL(PH.fld_PrincipalAmt,0))'PRINCIPAL'
FROM 
    @months M
    LEFT JOIN #AllExpired AE
    ON M.ReportMonth = DateName(mm,DATEADD(MM,5,AE.fld_LoanDate))
    LEFT JOIN #ExpAfterFiveMonths E5 
    ON AE.fld_PawnMainID=E5.fld_PawnMainID
    LEFT JOIN Transactions.tbl_PawnItem PIT
    ON E5.fld_PawnMainID=PIT.fld_PawnMainID
    LEFT JOIN Transactions.tbl_PawnHisto PH
    ON E5.fld_PawnMainID=PH.fld_PawnMainID
GROUP BY M.ReportMonth

UNION ALL

/*SELECT EXPIRED AFTER 6 MONTHS*/
SELECT 
    'Expired Item -'+ COALESCE(DateName(mm,DATEADD(MM,6,AE.fld_LoanDate)), M.ReportMonth) as [Month]
    ,COUNT(ISNULL(PIT.fld_PawnItemID,0))'COUNT'
    ,SUM(ISNULL(PIT.fld_KaratGram,0))'GRAMS'
    ,SUM(ISNULL(PH.fld_PrincipalAmt,0))'PRINCIPAL'
FROM 
    @months M
    LEFT JOIN #AllExpired AE
    ON M.ReportMonth = DateName(mm,DATEADD(MM,6,AE.fld_LoanDate))
    LEFT JOIN #ExpAfterSixMonths E6
    ON AE.fld_PawnMainID=E6.fld_PawnMainID
    LEFT JOIN Transactions.tbl_PawnItem PIT
    ON E6.fld_PawnMainID=PIT.fld_PawnMainID
    LEFT JOIN Transactions.tbl_PawnHisto PH
    ON E6.fld_PawnMainID=PH.fld_PawnMainID    
GROUP BY M.ReportMonth

答案 1 :(得分:1)

在您的选择中尝试此操作

SELECT (CASE WHEN (AE.fld_LoanDate IS null) THEN 0 ELSE AE.fld_LoanDate END) as fld_LoanDate

答案 2 :(得分:0)

尽管这两个答案都符合提问者的要求,但我会建议'在(范围)',“分组依据”,以及左连接和日期范围内的LoanDate。

所以,你有这样的表:

CREATE TABLE tPawnItem
(
    fPawnItemID int auto_increment primary key,
    fDescription varchar(30),
    fKaratGram float
);
CREATE TABLE tPawnHisto
(
    fPawnMainID int primary key,
    fPrincipalAmt int,
    fRedeemed date
);
create table tExp
(
    fPawnMainID int primary key,
    fLoanDate date
);

这几乎就是我的建议(我没有笔记本电脑,所以现在没有mysql),

SELECT 
    'Expired Item -'+ DATENAME(MM,tExp.fLoanDate) as [Month]
    ,COUNT(ISNULL(tPawnItem.fPawnItemID,0))'COUNT'
    ,SUM(ISNULL(tPawnItem.fKaratGram,0))'GRAMS'
    ,SUM(ISNULL(tPawnHisto.fPrincipalAmt,0))'PRINCIPAL'
FROM  tExp
    INNER JOIN tPawnItem
    ON tExp.fPawnMainID=tPawnItem.fPawnMainID
    INNER JOIN tPawnHisto
    ON tExp.fPawnMainID=tPawnHisto.fPawnMainID
WHERE t.Exp.fLoanDate in (
    DATENAME(MM,DATEADD(MM,4,tExp.fLoanDate)),
    DATENAME(MM,DATEADD(MM,5,tExp.fLoanDate)),
    DATENAME(MM,DATEADD(MM,6,tExp.fLoanDate)),
    DATENAME(MM,DATEADD(MM,7,tExp.fLoanDate))
)
GROUP BY DATENAME(MM,tExp.fLoanDate);