在where子句中使用Case结果 - 我想使用TorL - SQL Server的结果

时间:2014-07-25 11:40:10

标签: sql sql-server case

DECLARE @TodayDayOfWeek INT
DECLARE @EndOfPrevWeek DateTime
DECLARE @StartOfPrevWeek DateTime
DECLARE @EndOfThisWeek DateTime
DECLARE @StartOfThisWeek DateTime

- Delcared the Parameters

SET @TodayDayOfWeek = datepart(dw, GetDate())
SET @EndOfPrevWeek = DATEADD(dd, -@TodayDayOfWeek, GetDate())
SET @StartOfPrevWeek = DATEADD(dd, -(@TodayDayOfWeek+6), GetDate())
SET @StartOfThisWeek = DATEADD(dd, -@TodayDayOfWeek, GetDate())+1
SET @EndOfThisWeek = DATEADD(dd, -@TodayDayOfWeek, GetDate())+7

- 设置本周或上周使用的参数日期

select st.SALESID
    ,   st.SALESNAME
    ,   st.CUSTACCOUNT
    ,   st.CUSTOMERREF
    ,   st.SALESSTATUS
    ,   cast (sl.LINENUM as decimal (18,0))
    ,   sl.ITEMID
    ,   cast (sl.SALESQTY as decimal (18,0))
    ,   cast (sl.LINEAMOUNT as decimal(18,2))
    ,   case when st.CREATEDDATETIME between @StartOfPrevWeek  and @EndOfPrevWeek 
then 'L'
when st.CREATEDDATETIME between @StartOfThisWeek and @EndOfThisWeek
then 'T'
End 'TorL'

from salestable st
inner join salesline sl
on st.SALESID = sl.SALESID

where st.DATAAREAID = 'fr'

order by TorL

TorL是案例结果,我想用where来过滤它,只显示本周T或L为T或上周L为何。

由于

5 个答案:

答案 0 :(得分:1)

执行此操作的令人讨厌的方法是添加将“T”或“L”设置为WHERE子句的条件:

DECLARE @TodayDayOfWeek INT
DECLARE @EndOfPrevWeek DATE
DECLARE @StartOfPrevWeek DATE
DECLARE @EndOfThisWeek DATE
DECLARE @StartOfThisWeek DATE
--Delcared the Parameters

SET @TodayDayOfWeek = datepart(dw, GetDate())
SET @EndOfPrevWeek = DATEADD(dd, -@TodayDayOfWeek, GetDate())
SET @StartOfPrevWeek = DATEADD(dd, -(@TodayDayOfWeek+6), GetDate())
SET @StartOfThisWeek = DATEADD(dd, -@TodayDayOfWeek, GetDate())+1
SET @EndOfThisWeek = DATEADD(dd, -@TodayDayOfWeek, GetDate())+7

select st.SALESID
    ,   st.SALESNAME
    ,   st.CUSTACCOUNT
    ,   st.CUSTOMERREF
    ,   st.SALESSTATUS
    ,   cast (sl.LINENUM as decimal (18,0))
    ,   sl.ITEMID
    ,   cast (sl.SALESQTY as decimal (18,0))
    ,   cast (sl.LINEAMOUNT as decimal(18,2))
    ,   case when CAST(st.CREATEDDATETIME AS DATE) between @StartOfPrevWeek  and @EndOfPrevWeek 
then 'L'
when CAST(st.CREATEDDATETIME AS DATE) between @StartOfThisWeek and @EndOfThisWeek
then 'T'
End 'TorL'

from salestable st
inner join salesline sl
on st.SALESID = sl.SALESID

where st.DATAAREAID = 'fr'
AND (CAST(st.CREATEDDATETIME AS DATE) between @StartOfPrevWeek  and @EndOfThisWeek)

order by TorL

编辑:制作变量DATETIME会导致不必要的问题,因为时间戳会消除某些日期。对于此解决方案,建议创建DATE类型的变量,然后CAST the st.CREATEDATETIME AS DATE。

答案 1 :(得分:0)

您不能在where子句中使用与定义相同级别的列别名。只需使用子查询或CTE:

with t as (
      <your query here without the `order by`>
     )
select t.*
from t
where torl in ('L', 'T')
order by torl;

作为注释:不要使用单引号来转义列标识。虽然允许,但往往会导致混乱。在这种情况下,您根本不需要任何转义字符。仅对字符串和日期常量使用单引号。

答案 2 :(得分:0)

;WITH MyCTE AS
(

    select st.SALESID
        ,   st.SALESNAME
        ,   st.CUSTACCOUNT
        ,   st.CUSTOMERREF
        ,   st.SALESSTATUS
        ,   cast (sl.LINENUM as decimal (18,0)) AS LINENUM 
        ,   sl.ITEMID
        ,   cast (sl.SALESQTY as decimal (18,0)) AS SALESQTY 
        ,   cast (sl.LINEAMOUNT as decimal(18,2)) AS LINEAMOUNT 
        ,   case when st.CREATEDDATETIME between @StartOfPrevWeek  and @EndOfPrevWeek 
    then 'L'
    when st.CREATEDDATETIME between @StartOfThisWeek and @EndOfThisWeek
    then 'T'
    End Torl

    from salestable st
    inner join salesline sl
    on st.SALESID = sl.SALESID

    where st.DATAAREAID = 'fr'

)

SELECT * 
FROM MyCTE
WHERE
Torl = 'T'

答案 3 :(得分:0)

试试这个

Select * 
from (
    select st.SALESID
        ,   st.SALESNAME
        ,   st.CUSTACCOUNT
        ,   st.CUSTOMERREF
        ,   st.SALESSTATUS
        ,   cast (sl.LINENUM as decimal (18,0))
        ,   sl.ITEMID
        ,   cast (sl.SALESQTY as decimal (18,0))
        ,   cast (sl.LINEAMOUNT as decimal(18,2))
        ,   case when st.CREATEDDATETIME between @StartOfPrevWeek  and @EndOfPrevWeek 
    then 'L'
    when st.CREATEDDATETIME between @StartOfThisWeek and @EndOfThisWeek
    then 'T'
    End 'TorL' 'Name'

from salestable st
inner join salesline sl
on st.SALESID = sl.SALESID

where st.DATAAREAID = 'fr'


) t
where Name = 'T' or name = 'L'

按TorL订购

答案 4 :(得分:0)

只需添加不使用CTE或子查询的答案。在这种情况下,您可以使用CROSS APPLY,它允许您在查询中使用已定义的字段。它使查询更加优雅。

select st.SALESID
    ,   st.SALESNAME
    ,   st.CUSTACCOUNT
    ,   st.CUSTOMERREF
    ,   st.SALESSTATUS
    ,   cast (sl.LINENUM as decimal (18,0))
    ,   sl.ITEMID
    ,   cast (sl.SALESQTY as decimal (18,0))
    ,   cast (sl.LINEAMOUNT as decimal(18,2))
    ,   N.TorL
from salestable st
inner join salesline sl
on st.SALESID = sl.SALESID
CROSS APPLY (
   SELECT case when st.CREATEDDATETIME between @StartOfPrevWeek  and @EndOfPrevWeek then 'L'
               when st.CREATEDDATETIME between @StartOfThisWeek and @EndOfThisWeek then 'T'
          End
   ) N(TorL)
where st.DATAAREAID = 'fr'
AND N.TorL = 'L'
order by N.TorL

可以在此处SQL Fiddle找到适用于您的问题的CROSS APPLY的简化演示