条款的MSSQL案例

时间:2017-07-12 07:46:05

标签: sql sql-server

我有以下查询。

    SELECT DISTINCT datepart(day, eventdetails_datetime) as dates , bu_events.event_daysbeforeType
FROM "bu_events" 

INNER JOIN "bu_eventdetails" ON "bu_eventdetails"."event_id" = "bu_events"."event_id"

WHERE "bu_events"."is_active" = 1 
AND "bu_events"."is_deleted" =0 
AND "bu_eventdetails"."is_active" = 1 
AND "bu_eventdetails"."is_deleted" =0 
AND "bu_events"."service_id" = '31' 
AND (DATEDIFF(Minute, BU_EventDetails.eventDetails_datetime, '2017-07-12 10:33:19') <=0 or coalesce(event_always_available,0) = 1 ) 
AND 

(DATEDIFF(d, '2017-07-12 10:33:19',BU_EventDetails.eventDetails_datetime) >= coalesce(BU_Events.event_daysbefore, 0) or coalesce(event_always_available,0) = 1 ) 


AND ( select max(availabletable_amount) 
    from bu_availabletable 
    inner join bu_eventdetails on bu_eventdetails.eventdetail_id = bu_availabletable.eventdetail_id 
    inner join bu_eventpricegroups on bu_eventpricegroups.event_id = bu_eventdetails.event_id 
    where bu_availabletable.eventdetail_id = bu_eventdetails.eventdetail_id 
    and bu_eventpricegroups.eventpricegroup_always_available = 0 ) > 0 
    AND datepart(Month,eventdetails_datetime) = 07 
    AND datediff(d,getdate(),eventdetails_datetime)>=0 

我想做的是,如果bu_events.event_daysbeforeType是1,那么(DATEDIFF(d, '2017-07-12 10:33:19',BU_EventDetails.eventDetails_datetime) >= coalesce(BU_Events.event_daysbefore, 0) or coalesce(event_always_available,0) = 1 )如果是{2}那么(DATEDIFF(hour, '2017-07-12 10:33:19',BU_EventDetails.eventDetails_datetime) >= coalesce(BU_Events.event_daysbefore, 0) or coalesce(event_always_available,0) = 1 )

因此,根据bu_events.event_daysbeforeType我希望与D或HOUR约会。

到目前为止,我尝试了但不工作的是:

SELECT DISTINCT datepart(day, eventdetails_datetime) as dates , bu_events.event_daysbeforeType
FROM "bu_events" 

INNER JOIN "bu_eventdetails" ON "bu_eventdetails"."event_id" = "bu_events"."event_id"

WHERE "bu_events"."is_active" = 1 
AND "bu_events"."is_deleted" =0 
AND "bu_eventdetails"."is_active" = 1 
AND "bu_eventdetails"."is_deleted" =0 
AND "bu_events"."service_id" = '31' 
AND (DATEDIFF(Minute, BU_EventDetails.eventDetails_datetime, '2017-07-12 10:33:19') <=0 or coalesce(event_always_available,0) = 1 ) 
AND 

CASE 
WHEN bu_events.event_daysbeforeType = 1 THEN
(DATEDIFF(d, '2017-07-12 10:33:19',BU_EventDetails.eventDetails_datetime) >= coalesce(BU_Events.event_daysbefore, 0) or coalesce(event_always_available,0) = 1 )  
ELSE 
(DATEDIFF(hour, '2017-07-12 10:33:19',BU_EventDetails.eventDetails_datetime) >= coalesce(BU_Events.event_daysbefore, 0) or coalesce(event_always_available,0) = 1 )  
END

AND ( select max(availabletable_amount) 
    from bu_availabletable 
    inner join bu_eventdetails on bu_eventdetails.eventdetail_id = bu_availabletable.eventdetail_id 
    inner join bu_eventpricegroups on bu_eventpricegroups.event_id = bu_eventdetails.event_id 
    where bu_availabletable.eventdetail_id = bu_eventdetails.eventdetail_id 
    and bu_eventpricegroups.eventpricegroup_always_available = 0 ) > 0 
    AND datepart(Month,eventdetails_datetime) = 07 
    AND datediff(d,getdate(),eventdetails_datetime)>=0 

任何想法我做错了什么?

2 个答案:

答案 0 :(得分:2)

您不能使用像您这样的案例。 case是一个表达式,并尝试将其用作流控制 来自MSDN:

  

CASE表达式不能用于控制Transact-SQL语句,语句块,用户定义函数和存储过程的执行流程。有关流控制方法的列表,请参阅控制流语言(Transact-SQL)。

不要使用大小写,请执行以下操作:

AND 
(
    (
        bu_events.event_daysbeforeType = 1 
        AND (DATEDIFF(d, '2017-07-12 10:33:19',BU_EventDetails.eventDetails_datetime) >= coalesce(BU_Events.event_daysbefore, 0) or coalesce(event_always_available,0) = 1 )  
    )
    OR 
    (
        bu_events.event_daysbeforeType <> 1 
        AND (DATEDIFF(hour, '2017-07-12 10:33:19',BU_EventDetails.eventDetails_datetime) >= coalesce(BU_Events.event_daysbefore, 0) or coalesce(event_always_available,0) = 1 )  
    )

)

答案 1 :(得分:0)

CASE is for computing the value and as SQL has no logical (boolean) values it shouldn't be mixed up with logical expressions but a computed value is a valid argument of logical expression. Try

AND (CASE WHEN bu_events.event_daysbeforeType = 1 
      THEN (DATEDIFF(d, '2017-07-12 10:33:19',BU_EventDetails.eventDetails_datetime) 
      ELSE DATEDIFF(hour, '2017-07-12 10:33:19',BU_EventDetails.eventDetails_datetime) 
    END >= coalesce(BU_Events.event_daysbefore, 0) or coalesce(event_always_available,0) = 1 
)