0

今天是个好日子

我有这个代码

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

4

3 回答 3

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
于 2013-10-07T15:06:56.963 回答
1

在您的选择中试试这个

SELECT (CASE WHEN (AE.fld_LoanDate IS null) THEN 0 ELSE AE.fld_LoanDate END) as fld_LoanDate
于 2013-10-04T07:30:11.383 回答
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);
于 2013-10-08T01:23:24.250 回答