CASE SELECT限制返回的行

时间:2017-02-14 10:55:48

标签: sql sql-server case case-when select-case

我遇到SELECT CASE语句限制返回的行数的情况。虽然我不是一个专业的数据库,但是我接受了一些合理的SQL并且我认为SELECT CASE实际上不会限制结果?如果WHEN合格,只需执行CASE的THEN部分。

为了给出一些上下文,我有一个交易表,只要在某个租赁活动中在仓库和客户之间移动物品时就会记录该交易表。每个交易都被注册为具有参考订单和交易日期的行。为了获得客户收据交易和返回到单一行的仓库交易,我使用了(可能是原油!)FROM SELECT。但是,这部分似乎没问题。

我在SQL Server 2012上。

现在,根据所有这些发生的日期,我已经采用了第一个日期和最后一个日期,并应用了一个SELECT CASE来定义一些条件和结果日期差异计算。

DECLARE @RUNDATE AS DECIMAL(8) = '20170101'
DECLARE @DAYFIRST AS DATETIME = DATEADD(m, DATEDIFF(m, 0, CONVERT(DATE, CAST(@RUNDATE AS VARCHAR(8)), 112)),0)
DECLARE @DAYEND AS DATETIME = DATEADD(s, -1, DATEADD(m, DATEDIFF(m, 0,     CONVERT(DATE, CAST(@RUNDATE AS VARCHAR(8)), 112))+1, 0))
DECLARE @DAYSINPERIOD AS DECIMAL(2) = DAY(EOMONTH(CONVERT(DATE, CAST(@RUNDATE AS     VARCHAR(8)), 112)))

SELECT MTITNO AS 'Item Number',
MTBANO AS 'Serial Number',
MTRORN AS 'Rental Order',
MTRORL AS 'Rental Order Line',
MTTRQT AS 'Delivered Quantity',
CONVERT(DATE, CAST(MTTRDT AS VARCHAR(8)), 112) AS 'Customer Receipt Date',      --received in M3 to the customer, although this is aligned to when qty left the yard manually by logistics
CONVERT(DATE, CAST(NEXTDATE AS VARCHAR(8)), 112) AS 'Return To Depot Date',     --last date in the return chain, receiving to the yard.  If still in transit this equals the termination date
CASE
    WHEN CONVERT(DATE, CAST(MTTRDT AS VARCHAR(8)), 112) < @DAYFIRST AND NEXTDATE IS NULL                                            --is started before current period and no return activity or termination
    THEN @DAYSINPERIOD                                                                                                              --simply count the days in the current month as utilised
    ELSE 0
END AS 'On Rent Entire Period',
CASE
    WHEN CONVERT(DATE, CAST(MTTRDT AS VARCHAR(8)), 112) < @DAYFIRST AND NEXTDATE >= @DAYFIRST AND NEXTDATE <= @DAYEND               --is started before current period but has a return activity/termination date
    THEN DATEDIFF("DD",@DAYFIRST,CONVERT(DATE, CAST(NEXTDATE AS VARCHAR(8)), 112))+1                                                    --difference between start of month and the nextdate (+1 for date difference include all)
    ELSE 0
END AS 'Started Previous Period, Ended This Period',
CASE
    WHEN CONVERT(DATE, CAST(MTTRDT AS VARCHAR(8)), 112) >= @DAYFIRST AND NEXTDATE IS NULL                                           --is started in the current period and no return activity or termination
    THEN DATEDIFF(D,CONVERT(DATE, CAST(MTTRDT AS VARCHAR(8)), 112),@DAYEND)+1                                                       --difference between start date and last day of the current month (in line with utilisation) (+1 for date differnce include all)
    ELSE 0
END AS 'Started This Period, No End Date',
CASE
   WHEN CONVERT(DATE, CAST(MTTRDT AS VARCHAR(8)), 112) >= @DAYFIRST AND NEXTDATE >= @DAYFIRST AND NEXTDATE <= @DAYEND               --is started in the current period and has a return activity/termination date
   THEN DATEDIFF(D,CONVERT(DATE, CAST(MTTRDT AS VARCHAR(8)), 112),CONVERT(DATE,    CAST(NEXTDATE AS VARCHAR(8)), 112))+1                --different between start date and the nextdate (+1 for date differnce include all)
    ELSE 0
END AS 'Started and Ended This Period',                                                   --current period rental days (should match utilisation as sum of all lines)
@DAYSINPERIOD AS 'Calendar Days in Month',                                        --how many days in the current month
@DAYFIRST AS 'First of Month',                                                    --what is the date of the first day of the current month
@DAYEND AS 'Last of Month'                                                        --what is the date of the last day of the current month
FROM
    (
     SELECT MTITNO,
            MTBANO,
            MTRORN,
            MTRORL,
            MTTRQT,
            MTTRDT,
            (
             SELECT MIN(MTTRDT)
             FROM MVXJDTA.MITTRA T2
             WHERE T1.MTITNO = T2.MTITNO
             AND T1.MTBANO = T2.MTBANO
             AND T1.MTRORN = T2.MTRORN
             AND T1.MTRORL = T2.MTRORL
             AND T1.MTTRQT = T2.MTTRQT
             AND T2.MTTRDT > T1.MTTRDT
             ) AS NEXTDATE
    FROM MVXJDTA.MITTRA T1
    WHERE MTCONO = 880
    AND MTTTYP = '50'
    AND MTTRQT > 0
    AND MTCAMU <> ''
    AND MTRORN LIKE 'A0%'
    ) T

这只返回整个数据集中的前两行。 2 rows

但是,如果我注释掉第4个SELECT CASE,那么我会返回所有记录?! all rows

-- CASE
--    WHEN CONVERT(DATE, CAST(MTTRDT AS VARCHAR(8)), 112) >= @DAYFIRST AND NEXTDATE >= @DAYFIRST AND NEXTDATE <= @DAYEND                --is started in the current period and has a return activity/termination date
--    THEN DATEDIFF(D,CONVERT(DATE, CAST(MTTRDT AS VARCHAR(8)), 112),CONVERT(DATE, CAST(NEXTDATE AS VARCHAR(8)), 112))+1                --different between start date and the nextdate (+1 for date differnce include all)
--     ELSE 0
-- END AS 'Started and Ended This Period',                                                    --current period rental days (should match utilisation as sum of all lines)

奇怪的是,第3行应该与我刚刚注释掉的条件匹配。

任何人都可以帮助我更好地理解为什么SELECT CASE会对返回的行进行限制吗?而我的语法可能很糟糕可能是原因!!!

0 个答案:

没有答案