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 に答える
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 に答える