-3

私は12か月/年すべてを選択しようとしています。そして、次のTSQLコードでこれができると思いました。ただし、これには私が望むすべての月が含まれるわけではありません。これの原因は何ですか?これは変更されたコードです:

DECLARE @END_YEAR VARCHAR(10)
DECLARE @END_MONTH VARCHAR(10)

SET @END_YEAR = '2010'
SET @END_MONTH = '10'

DECLARE @TheMonthLastDate DATETIME
DECLARE @TempDate DATETIME
SET @TempDate = '2010-11-01 00:00:00.000'
SET @TheMonthLastDate = '2010-11-01 00:00:00.000'

;with months
as  
(
select dateadd(month, -1, dateadd(day, datediff(day, 0, @TempDate), 0)) as m
union all
select dateadd(month, -1, m)
from months
where   m > dateadd(month, -12, @TempDate)
)
,yourTable(DateOpened, DateClosed)
as
(select TSK_START_DATE, BTK_CLOSED_DATE
FROM [PROC].ALL_AUDIT
WHERE 
(BTK_CLOSED_DATE < @TheMonthLastDate OR
TSK_START_DATE < @TheMonthLastDate 
)
)
select      yt.DateClosed 'r2', m.m 'r3',
        month(coalesce(yt.DateClosed, m.m)) as 'MonthClosed',
        year(coalesce(yt.DateClosed, m.m)) as 'YearClosed'
from    months m
left join yourTable yt
    on      
    (  datepart(year, yt.DateClosed) = DATEPART(year, m.m)
    and datepart(month, yt.DateClosed) = DATEPART(month, m.m) 
    or    
      datepart(year, yt.DateOpened) = DATEPART(year, m.m)
    and datepart(month, yt.DateOpened) = DATEPART(month, m.m) 
    )
AND year(coalesce(yt.DateClosed, m.m)) = 2010
order by yt.DateClosed

したがって、上記のクエリはすべての月を返すわけではありません。しかし、上記のWHERE行を次のように変更すると、次のようになります。

FROM [PROC].ALL_AUDIT
    WHERE
BTK_CLOSED_DATE < @TheMonthLastDate

この場合、このクエリは12か月すべてを返します。どうすればいいの?

WHEREがBTK_CLOSED_DATE<@TheMonthLastDateの場合に表示される、必要な出力:

r2  r3  MonthClosed YearClosed
NULL    2010-06-01 00:00:00.000 6   2010
NULL    2009-11-01 00:00:00.000 11  2009
2010-01-06 20:02:19.127 2010-01-01 00:00:00.000 1   2010
2010-01-27 23:13:45.570 2010-01-01 00:00:00.000 1   2010
2010-02-15 14:49:14.427 2010-02-01 00:00:00.000 2   2010
2010-02-15 14:49:14.427 2009-12-01 00:00:00.000 2   2010

しかし、代わりにWHEREを使用する場合:(BTK_CLOSED_DATE <@TheMonthLastDate OR TSK_START_DATE <@TheMonthLastDate)

それから私は見る:

r2  r3  MonthClosed YearClosed
NULL    2010-10-01 00:00:00.000 10  2010
NULL    2010-09-01 00:00:00.000 9   2010
NULL    2010-09-01 00:00:00.000 9   2010
NULL    2010-08-01 00:00:00.000 8   2010
NULL    2010-08-01 00:00:00.000 8   2010
...

したがって、最初の結果では、2010年6月にNULLが表示されることに注意してください。これは、私が望んでいることです。この問題は、私のデータに2009〜2011年のデータが含まれているという事実に関係していると思いますが、年ではなく月のみを比較しています。この追加ロジックをどのように追加しますか?

4

1 に答える 1

2

データを削減する唯一の場所は、すでに特定したWHERE句を使用することです。したがって、期待するすべての月が取得されない理由は、列TSK_START_DATEがすべての月の@TheMonthLastDateより小さくないためです。

この仮説を証明するには、クエリの次のセクションを実行します(where句の一部をコメントアウトし、「yourTable」cteの下のすべてを削除しました)。結果には、欠落している月のTSK_Start_Date列に何が返されているかが示され、<@TheMonthLastDate句を適用したときに行が欠落している理由を特定するのに役立ちます。

DECLARE @END_YEAR VARCHAR(10)
DECLARE @END_MONTH VARCHAR(10)

SET @END_YEAR = '2010'
SET @END_MONTH = '10'

DECLARE @TheMonthLastDate DATETIME
DECLARE @TempDate DATETIME
SET @TempDate = '2010-11-01 00:00:00.000'
SET @TheMonthLastDate = '2010-11-01 00:00:00.000'

;with months
as  
(
select dateadd(month, -1, dateadd(day, datediff(day, 0, @TempDate), 0)) as m
union all
select dateadd(month, -1, m)
from months
where   m > dateadd(month, -12, @TempDate)
)

,yourTable(DateOpened, DateClosed)
as
(select TSK_START_DATE, BTK_CLOSED_DATE
FROM [PROC].ALL_AUDIT
WHERE 
(BTK_CLOSED_DATE < @TheMonthLastDate OR
--TSK_START_DATE < @TheMonthLastDate 
)
)
select * , @TheMonthLastDate TheMonthLastDate from yourTable
于 2011-08-18T12:43:04.347 に答える