如果我运行查询我没有得到输出(它显示空值)

时间:2016-08-11 09:53:51

标签: sql sql-server-2008 tsql

SELECT DISTINCT
    [BILLDATE]as BILLDATE
    ,PROPERTYCODE   
    ,[RESTUARANTNAME] as RESTUARANTNAME
    ,rt.FOODREVENUE
    ,rt.LIQUORREVENUE
    ,rt.TOBACCOREVENUE
    ,rt.SOFTDRINKSREVENUE
    ,rt.OTHERREVENUE
    ,rt.FOODCOVERS
    ,rt.LIQUORCOVERS
    ,rt.SOFTDRINKSCOVERS
    ,rt.TOBACCOCOVERS
    ,rt.OTHERCOVERS
from CoversToday t
cross apply     
    (select
        SUM([FOODREVENUE])as FOODREVENUE
      ,SUM([LIQUORREVENUE])as LIQUORREVENUE
      ,SUM([SOFTDRINKSREVENUE])as SOFTDRINKSREVENUE
      ,SUM([TOBACCOREVENUE])as TOBACCOREVENUE
      ,SUM([OTHERREVENUE])as OTHERREVENUE
      ,SUM([FOODCOVERS])as FOODCOVERS
      ,SUM([LIQUORCOVERS])as LIQUORCOVERS
      ,SUM([SOFTDRINKSCOVERS])as SOFTDRINKSCOVERS
      ,SUM([TOBACCOCOVERS])as TOBACCOCOVERS
      ,SUM([OTHERCOVERS])as OTHERCOVERS
     from CoversToday
     where 
     PROPERTYcode = t.PROPERTYcode and [RESTUARANTNAME]=t.[RESTUARANTNAME] and
     [BILLDATE] BETWEEN dateadd(year, datediff(MONTH, 0,t.[BILLDATE]),0) -- start of month
AND t.[BILLDATE] 
    ) as rt

3 个答案:

答案 0 :(得分:1)

月份开始时为dateadd(MONTH, datediff(MONTH, 0, t.[BILLDATE]), 0)而非dateadd(year,...

年初是dateadd(YEAR, datediff(YEAR, 0, t.[BILLDATE]), 0)

答案 1 :(得分:0)

dateadd(year, datediff(MONTH, 0,t.[BILLDATE]),0)

这是你的错误

请参阅DateDiff

更新:

纠正错误:

DateAdd(MONTH, DateDiff(MONTH, 0, T.[BILLDATE]), 0)
祝你好运

答案 2 :(得分:0)

声明@fymonth int = 4; - 财年第一个月。

SELECT DISTINCT      BILLDATE     ,PROPERTYCODE     ,FOODREVENUE作为TODAY_FOODREVENUE     ,rt.FOODREVENUE_MTOD     ,rt.FOODREVENUE_YTOD     ,LODYREVENUE,TODAY_LIQUORREVENUE     ,rt.LIQUORREVENUE_MTOD     ,rt.LIQUORREVENUE_YTOD     ,TOBACCOREVENUE作为TODAY_TOBACCOREVENUE     ,rt.TOBACCOREVENUE_MTOD     ,rt.TOBACCOREVENUE_YTOD     ,SOFTDRINKSREVENUE作为TODAY_SOFTDRINKSREVENUE     ,rt.SOFTDRINKSREVENUE_MTOD     ,rt.SOFTDRINKSREVENUE_YTOD     ,其余为今天_天哪     ,rt.OTHERREVENUE_MTOD     ,rt.OTHERREVENUE_YTOD     ,FOODCOVERS作为TODAY_FOODCOVERS     ,rt.FOODCOVERS_MTOD     ,rt.FOODCOVERS_YTOD     ,LIQUORCOVERS作为TODAY_LIQUORCOVERS     ,rt.LIQUORCOVERS_MTOD     ,rt.LIQUORCOVERS_YTOD     ,SOFTDRINKSCOVERS作为TODAY_SOFTDRINKSCOVERS     ,rt.SOFTDRINKSCOVERS_MTOD     ,rt.SOFTDRINKSCOVERS_YTOD     ,TOBACCOCOVERS作为TODAY_TOBACCOCOVERS     ,rt.TOBACCOCOVERS_MTOD     ,rt.TOBACCOCOVERS_YTOD     ,其他人作为TODAY_OTHERCOVERS     ,rt.OTHERCOVERS_MTOD     ,rt.OTHERCOVERS_YTOD

FROM CoversToday t 交叉申请
    (选择

----FOODREVENUE

    SUM(t2.Foodrevenue) AS  FOODREVENUE_YTOD

     ,SUM(CASE WHEN t2.BILLDATE BETWEEN 
            -- start of month for t.BILLDATE
        dateadd(MONTH, datediff(MONTH, 0, t3.BILLDATE), 0)
        AND t3.BILLDATE
     THEN t2.Foodrevenue END) AS FOODREVENUE_MTOD

 ----LIQUORREVENUE

 ,SUM(t2.LIQUORREVENUE) AS  LIQUORREVENUE_YTOD

     ,SUM(CASE WHEN t2.BILLDATE BETWEEN 
            -- start of month for t.BILLDATE
        dateadd(MONTH, datediff(MONTH, 0, t3.BILLDATE), 0)
        AND t3.BILLDATE
     THEN t2.LIQUORREVENUE END) AS LIQUORREVENUE_MTOD    

 ----TOBACCOREVENUE  

 ,SUM(t2.TOBACCOREVENUE) AS  TOBACCOREVENUE_YTOD

     ,SUM(CASE WHEN t2.BILLDATE BETWEEN 
            -- start of month for t.BILLDATE
        dateadd(MONTH, datediff(MONTH, 0, t3.BILLDATE), 0)
        AND t3.BILLDATE
     THEN t2.TOBACCOREVENUE END) AS TOBACCOREVENUE_MTOD    

 ----SOFTDRINKSREVENUE     


 ,SUM(t2.SOFTDRINKSREVENUE) AS  SOFTDRINKSREVENUE_YTOD

     ,SUM(CASE WHEN t2.BILLDATE BETWEEN 
            -- start of month for t.BILLDATE
        dateadd(MONTH, datediff(MONTH, 0, t3.BILLDATE), 0)
        AND t3.BILLDATE
     THEN t2.SOFTDRINKSREVENUE END) AS SOFTDRINKSREVENUE_MTOD    

 ----OTHERREVENUE
    ,SUM(t2.OTHERREVENUE) AS  OTHERREVENUE_YTOD

     ,SUM(CASE WHEN t2.BILLDATE BETWEEN 
            -- start of month for t.BILLDATE
        dateadd(MONTH, datediff(MONTH, 0, t3.BILLDATE), 0)
        AND t3.BILLDATE
     THEN t2.OTHERREVENUE END) AS OTHERREVENUE_MTOD    

 ----FOODCOVERS   

    ,SUM(t2.FOODCOVERS) AS  FOODCOVERS_YTOD

     ,SUM(CASE WHEN t2.BILLDATE BETWEEN 
            -- start of month for t.BILLDATE
        dateadd(MONTH, datediff(MONTH, 0, t3.BILLDATE), 0)
        AND t3.BILLDATE
     THEN t2.FOODCOVERS END) AS FOODCOVERS_MTOD    

 ----  LIQUORCOVERS 

    ,SUM(t2.LIQUORCOVERS) AS  LIQUORCOVERS_YTOD

     ,SUM(CASE WHEN t2.BILLDATE BETWEEN 
            -- start of month for t.BILLDATE
        dateadd(MONTH, datediff(MONTH, 0, t3.BILLDATE), 0)
        AND t3.BILLDATE
     THEN t2.LIQUORCOVERS END) AS LIQUORCOVERS_MTOD    

 ----  SOFTDRINKSCOVERS

  ,SUM(t2.SOFTDRINKSCOVERS) AS  SOFTDRINKSCOVERS_YTOD

     ,SUM(CASE WHEN t2.BILLDATE BETWEEN 
            -- start of month for t.BILLDATE
        dateadd(MONTH, datediff(MONTH, 0, t3.BILLDATE), 0)
        AND t3.BILLDATE
     THEN t2.SOFTDRINKSCOVERS END) AS SOFTDRINKSCOVERS_MTOD 


    ----  TOBACCOCOVERS

  ,SUM(t2.TOBACCOCOVERS) AS  TOBACCOCOVERS_YTOD

     ,SUM(CASE WHEN t2.BILLDATE BETWEEN 
            -- start of month for t.BILLDATE
        dateadd(MONTH, datediff(MONTH, 0, t3.BILLDATE), 0)
        AND t3.BILLDATE
     THEN t2.TOBACCOCOVERS END) AS TOBACCOCOVERS_MTOD    


       ----  OTHERCOVERS

  ,SUM(t2.OTHERCOVERS) AS  OTHERCOVERS_YTOD

     ,SUM(CASE WHEN t2.BILLDATE BETWEEN 
            -- start of month for t.BILLDATE
        dateadd(MONTH, datediff(MONTH, 0, t3.BILLDATE), 0)
        AND t3.BILLDATE
     THEN t2.OTHERCOVERS END) AS OTHERCOVERS_MTOD   

 from CoversToday t2
 JOIN CoversToday t3 ON t3.CoversTodayid = t.CoversTodayid -- change as needed to get 1 to 1 JOIN
 where 
 t2.PROPERTYCODE = t.PROPERTYCODE and
 t2.BILLDATE BETWEEN 
            -- FY start for t.BILLDATE
            dateadd(MONTH,
              @fymonth - CASE WHEN month(t.BILLDATE) >= @fymonth THEN 1 ELSE 13 END,
              dateadd(YEAR, datediff(YEAR, 0, t.BILLDATE), 0))
            AND t.BILLDATE 
) as rt

ORDER BY BILLDATE