语句不适用于我的数据的情况

时间:2018-11-20 13:31:17

标签: sql sql-server

在工作案例陈述中,我被卡住了

例如,我有以下情况

Amt         StartDate   EndDate     Port    Trade
10.00       9/21/2018   9/21/2020   NULL    NULL
54,523.00   11/14/2018  11/15/2018  NULL    NULL
756.00      11/14/2018  11/15/2018  NULL    NULL
456.00      11/14/2018  11/15/2018  NULL    NULL
86.00       11/14/2018  11/15/2018  NULL    NULL
86.00       11/14/2018  11/15/2018  NULL    NULL
453.00      11/14/2018  11/15/2018  NULL    NULL
786.00      11/14/2018  11/15/2018  NULL    NULL
86.00       11/14/2018  11/15/2018  NULL    NULL
568.00      11/14/2018  11/15/2018  NULL    NULL
12,358.00   11/14/2018  11/15/2018  NULL    NULL
45,388.00   11/5/2018   12/5/2018   NULL    NULL
75,368.00   8/9/2018    12/20/2018  call    collateral
783,678.00  7/13/2018   1/14/2019   NULL    NULL
  • 1)我正在寻找的是开始日期和结束日期的差= 1 或类似“%call%”和Trade =“抵押品”的港口,然后修改
  • 2)当开始日期和结束日期之差> 7 和Port不喜欢'%call%'并先交易<>'抵押'然后再修改

我的第一个条件有效

select CASE WHEN DATEDIFF(DAY,CAST(Startdate AS DATE),CAST(Enddate AS DATE))=1
      OR (Port like 'Call' and [Trade]='collateral') 
   THEN amt ELSE 0 END AS money1
   from tablename

但是第二个条件不起作用

select CASE WHEN (DATEDIFF(DAY,CAST(Startdate AS DATE),CAST(Enddate AS DATE))> 7 AND 
                DATEDIFF(DAY,CAST(Startdate AS DATE),CAST(Enddate AS DATE)) <= 9999) 
                and [Trade] <> 'collateral' and Portfolio not like '%Call%'
                THEN amt ELSE 0 END AS money2    from tablename

o / p除外

Amt         StartDate   EndDate money1              money2
10.00       9/21/2018   9/21/2020                   10.00
54,523.00   11/14/2018  11/15/2018  54,523.00   
756.00      11/14/2018  11/15/2018  756.00  
456.00      11/14/2018  11/15/2018  456.00  
86.00       11/14/2018  11/15/2018  86.00   
86.00       11/14/2018  11/15/2018  86.00   
453.00      11/14/2018  11/15/2018  453.00  
786.00      11/14/2018  11/15/2018  786.00  
86.00       11/14/2018  11/15/2018  86.00   
568.00      11/14/2018  11/15/2018  568.00  
12,358.00   11/14/2018  11/15/2018  12,358.00   
45,388.00   11/5/2018   12/5/2018                   45,388.00
75,368.00   8/9/2018    12/20/2018  75,368.00   
783,678.00  7/13/2018   1/14/2019                   783,678.00

需要帮助,我正在使用Sql Server 2012。

2 个答案:

答案 0 :(得分:1)

您喜欢的运算符应为'%Call%'

select CASE WHEN (DATEDIFF(DAY,CAST(Startdate AS DATE),CAST(Enddate AS DATE))> 7 AND 
                DATEDIFF(DAY,CAST(Startdate AS DATE),CAST(Enddate AS DATE)) <= 9999) 
                and [Trade] <> 'collateral' and Portfolio not like '%Call%'
                THEN amt ELSE 0 END AS money2 from tablename

答案 1 :(得分:1)

检查PortFolioTrade是否为NULL
所以:

SELECT CASE WHEN DATEDIFF(DAY,CAST(Startdate AS DATE),CAST(Enddate AS DATE))> 7 AND 
                 DATEDIFF(DAY,CAST(Startdate AS DATE),CAST(Enddate AS DATE)) <= 9999 AND
                 ([Trade] IS NULL OR [Trade] <> 'collateral') AND
                 (Portfolio IS NULL OR Portfolio NOT LIKE '%call%')
            THEN amt ELSE 0 END AS money2
FROM tablename;