'无效的对象名称'运行查询时出错

时间:2016-08-26 06:30:29

标签: sql sql-server tsql

在下面运行查询时出现错误:

  

Msg 208,Level 16,State 1,Line 1无效的对象名称' t'

我做错了什么?

我的查询:

SELECT 
     t.ACCOUNTDATE
    ,t.PropertyName
    ,isnull(t.FOODREVENUE_TODAY,0) AS FOODREVENUE_TODAY_CY,isnull(y.FOODREVENUE_TODAY,0) AS FOODREVENUE_TODAY_PY
    ,isnull(t.FOODREVENUE_MTOD,0) AS FOODREVENUE_MTOD_CY,isnull(y.FOODREVENUE_MTOD,0) AS FOODREVENUE_MTOD_PY
    ,isnull(t.FOODREVENUE_YTOD,0) AS FOODREVENUE_YTOD_CY,isnull(y.FOODREVENUE_YTOD,0) AS FOODREVENUE_YTOD_PY
    ,isnull(t.LIQUORREVENUE_TODAY,0) AS LIQUORREVENUE_TODAY_CY,isnull(y.LIQUORREVENUE_TODAY,0) AS LIQUORREVENUE_TODAY_PY
    ,isnull(t.LIQUORREVENUE_MTOD,0) AS LIQUORREVENUE_MTOD_CY,isnull(y.LIQUORREVENUE_MTOD,0) AS LIQUORREVENUE_MTOD_PY
    ,isnull(t.LIQUORREVENUE_YTOD,0) AS LIQUORREVENUE_YTOD_CY,isnull(y.LIQUORREVENUE_YTOD,0) AS LIQUORREVENUE_YTOD_PY
    ,isnull(t.SOFTDRINKSREVENUE_TODAY,0) AS SOFTDRINKSREVENUE_TODAY_CY,isnull(y.SOFTDRINKSREVENUE_TODAY,0) AS SOFTDRINKSREVENUE_TODAY_PY
    ,isnull(t.SOFTDRINKSREVENUE_MTOD,0) AS SOFTDRINKSREVENUE_MTOD_CY,isnull(y.SOFTDRINKSREVENUE_MTOD,0) AS SOFTDRINKSREVENUE_MTOD_PY
    ,isnull(t.SOFTDRINKSREVENUE_YTOD,0) AS SOFTDRINKSREVENUE_YTOD_CY,isnull(y.SOFTDRINKSREVENUE_YTOD,0) AS SOFTDRINKSREVENUE_YTOD_PY
    ,isnull(t.TOBACCOREVENUE_TODAY,0) AS TOBACCOREVENUE_TODAY_CY,isnull(y.TOBACCOREVENUE_TODAY,0) AS TOBACCOREVENUE_TODAY_PY
    ,isnull(t.TOBACCOREVENUE_MTOD,0) AS TOBACCOREVENUE_MTOD_CY,isnull(y.TOBACCOREVENUE_MTOD,0) AS TOBACCOREVENUE_MTOD_PY
    ,isnull(t.TOBACCOREVENUE_YTOD,0) AS TOBACCOREVENUE_YTOD_CY,isnull(y.TOBACCOREVENUE_YTOD,0) AS TOBACCOREVENUE_YTOD_PY
    ,isnull(t.OTHERREVENUE_TODAY,0) AS OTHERREVENUE_TODAY_CY,isnull(y.OTHERREVENUE_TODAY,0) AS OTHERREVENUE_TODAY_PY
    ,isnull(t.OtherRevenue_MTOD,0) AS OTHERREVENUE_MTOD_CY,isnull(y.OtherRevenue_MTOD,0) AS OTHERREVENUE_MTOD_PY
    ,isnull(t.OTHERREVENUE_YTOD,0) AS OTHERREVENUE_YTOD_CY,isnull(y.OTHERREVENUE_YTOD,0) AS OTHERREVENUE_YTOD_PY
    ,isnull(t.FOODCOVERS_TODAY,0) AS FOODCOVERS_TODAY_CY,isnull(y.FOODCOVERS_TODAY,0) AS FOODCOVERS_TODAY_PY
    ,isnull(t.FOODCOVERS_MTOD,0) AS FOODCOVERS_MTOD_CY,isnull(y.FOODCOVERS_MTOD,0) AS FOODCOVERS_MTOD_PY
    ,isnull(t.FOODCOVERS_YTOD,0) AS FOODCOVERS_YTOD_CY,isnull(y.FOODCOVERS_YTOD,0) AS FOODCOVERS_YTOD_PY
    ,isnull(t.LIQUORCOVERS_TODAY,0) AS LIQUORCOVERS_TODAY_CY,isnull(y.LIQUORCOVERS_TODAY,0) AS LIQUORCOVERS_TODAY_PY
    ,isnull(t.LIQUORCOVERS_MTOD,0) AS LIQUORCOVERS_MTOD_CY,isnull(y.LIQUORCOVERS_MTOD,0) AS LIQUORCOVERS_MTOD_PY
    ,isnull(t.LIQUORCOVERS_YTOD,0) AS LIQUORCOVERS_YTOD_CY,isnull(y.LIQUORCOVERS_YTOD,0) AS LIQUORCOVERS_YTOD_PY
    ,isnull(t.OTHERCOVERS_TODAY,0) AS OTHERCOVERS_TODAY_CY,isnull(y.OTHERCOVERS_TODAY,0) AS OTHERCOVERS_TODAY_PY
    ,isnull(t.OTHERCOVERS_MTOD,0) AS OTHERCOVERS_MTOD_CY,isnull(y.OTHERCOVERS_MTOD,0) AS OTHERCOVERS_MTOD_PY
    ,isnull(t.OTHERCOVERS_YTOD,0) AS OTHERCOVERS_YTOD_CY,isnull(y.OTHERCOVERS_YTOD,0) AS OTHERCOVERS_YTOD_PY
    ,isnull(t.TOBACCOCOVERS_TODAY,0) AS TOBACCOCOVERS_TODAY_CY,isnull(y.TOBACCOCOVERS_TODAY,0) AS TOBACCOCOVERS_TODAY_PY
    ,isnull(t.TOBACCOCOVERS_MTOD,0) AS TOBACCOCOVERS_MTOD_CY,isnull(y.TOBACCOCOVERS_MTOD,0) AS TOBACCOCOVERS_MTOD_PY
    ,isnull(t.TOBACCOCOVERS_YTOD,0) AS TOBACCOCOVERS_YTOD_CY,isnull(y.TOBACCOCOVERS_YTOD,0) AS TOBACCOCOVERS_YTOD_PY

 FROM (select B.FullDate AS ACCOUNTDATE
    ,C.PropertyName
    ,(A.FOODREVENUE) AS FOODREVENUE_TODAY,(D.FOODREVENUE_MTOD) AS FOODREVENUE_MTOD,(D.FOODREVENUE_YTOD) AS FOODREVENUE_YTOD
    ,(A.LIQUORREVENUE) AS LIQUORREVENUE_TODAY,(D.LIQUORREVENUE_MTOD) AS LIQUORREVENUE_MTOD,(D.LIQUORREVENUE_YTOD) AS LIQUORREVENUE_YTOD
    ,(A.SOFTDRINKSREVENUE) AS SOFTDRINKSREVENUE_TODAY,(D.SOFTDRINKSREVENUE_MTOD) AS SOFTDRINKSREVENUE_MTOD,(D.SOFTDRINKSREVENUE_YTOD) AS SOFTDRINKSREVENUE_YTOD
    ,(A.TOBACCOREVENUE) AS TOBACCOREVENUE_TODAY,(D.TOBACCOREVENUE_MTOD) AS TOBACCOREVENUE_MTOD,(D.TOBACCOREVENUE_YTOD) AS TOBACCOREVENUE_YTOD
    ,(A.OtherRevenue) AS OTHERREVENUE_TODAY,(D.OtherRevenue_MTOD) AS OTHERREVENUE_MTOD,(D.OTHERREVENUE_YTOD) AS OTHERREVENUE_YTOD
    ,(A.FOODCOVERS) AS FOODCOVERS_TODAY,(D.FOODCOVERS_MTOD) AS FOODCOVERS_MTOD,(D.FOODCOVERS_YTOD) AS FOODCOVERS_YTOD
    ,(A.LIQUORCOVERS) AS LIQUORCOVERS_TODAY,(D.LIQUORCOVERS_MTOD) AS LIQUORCOVERS_MTOD,(D.LIQUORCOVERS_YTOD) AS LIQUORCOVERS_YTOD
    ,(A.OTHERCOVERS) AS OTHERCOVERS_TODAY,(D.OTHERCOVERS_MTOD) AS OTHERCOVERS_MTOD,(D.OTHERCOVERS_YTOD) AS OTHERCOVERS_YTOD
    ,(A.TOBACCOCOVERS) AS TOBACCOCOVERS_TODAY,(D.TOBACCOCOVERS_MTOD) AS TOBACCOCOVERS_MTOD,(D.TOBACCOCOVERS_YTOD) AS TOBACCOCOVERS_YTOD
 FROM Fact_MisCovers A
INNER JOIN DimDate B ON A.Datekey=B.DateKey
INNER JOIN Dim_MisCovers C ON A.Fact_MisCovers_id=C.Dim_MisCovers_id
INNER JOIN Fact_MisCovers D ON a.Fact_MisCovers_id=d.Fact_MisCovers_id) as t
LEFT JOIN t as y
  ON DATEADD(YEAR,-1,t.ACCOUNTDATE) = y.ACCOUNTDATE
    order by t.ACCOUNTDATE

1 个答案:

答案 0 :(得分:1)

将您的查询放入CTE,然后从中选择:

;WITH cte AS (
(select B.FullDate AS ACCOUNTDATE
    ,C.PropertyName
    ,(A.FOODREVENUE) AS FOODREVENUE_TODAY,(D.FOODREVENUE_MTOD) AS FOODREVENUE_MTOD,(D.FOODREVENUE_YTOD) AS FOODREVENUE_YTOD
    ,(A.LIQUORREVENUE) AS LIQUORREVENUE_TODAY,(D.LIQUORREVENUE_MTOD) AS LIQUORREVENUE_MTOD,(D.LIQUORREVENUE_YTOD) AS LIQUORREVENUE_YTOD
    ,(A.SOFTDRINKSREVENUE) AS SOFTDRINKSREVENUE_TODAY,(D.SOFTDRINKSREVENUE_MTOD) AS SOFTDRINKSREVENUE_MTOD,(D.SOFTDRINKSREVENUE_YTOD) AS SOFTDRINKSREVENUE_YTOD
    ,(A.TOBACCOREVENUE) AS TOBACCOREVENUE_TODAY,(D.TOBACCOREVENUE_MTOD) AS TOBACCOREVENUE_MTOD,(D.TOBACCOREVENUE_YTOD) AS TOBACCOREVENUE_YTOD
    ,(A.OtherRevenue) AS OTHERREVENUE_TODAY,(D.OtherRevenue_MTOD) AS OTHERREVENUE_MTOD,(D.OTHERREVENUE_YTOD) AS OTHERREVENUE_YTOD
    ,(A.FOODCOVERS) AS FOODCOVERS_TODAY,(D.FOODCOVERS_MTOD) AS FOODCOVERS_MTOD,(D.FOODCOVERS_YTOD) AS FOODCOVERS_YTOD
    ,(A.LIQUORCOVERS) AS LIQUORCOVERS_TODAY,(D.LIQUORCOVERS_MTOD) AS LIQUORCOVERS_MTOD,(D.LIQUORCOVERS_YTOD) AS LIQUORCOVERS_YTOD
    ,(A.OTHERCOVERS) AS OTHERCOVERS_TODAY,(D.OTHERCOVERS_MTOD) AS OTHERCOVERS_MTOD,(D.OTHERCOVERS_YTOD) AS OTHERCOVERS_YTOD
    ,(A.TOBACCOCOVERS) AS TOBACCOCOVERS_TODAY,(D.TOBACCOCOVERS_MTOD) AS TOBACCOCOVERS_MTOD,(D.TOBACCOCOVERS_YTOD) AS TOBACCOCOVERS_YTOD
 FROM Fact_MisCovers A
INNER JOIN DimDate B ON A.Datekey=B.DateKey
INNER JOIN Dim_MisCovers C ON A.Fact_MisCovers_id=C.Dim_MisCovers_id
INNER JOIN Fact_MisCovers D ON a.Fact_MisCovers_id=d.Fact_MisCovers_id) 
)


SELECT 
     t.ACCOUNTDATE
    ,t.PropertyName
    ,isnull(t.FOODREVENUE_TODAY,0) AS FOODREVENUE_TODAY_CY,isnull(y.FOODREVENUE_TODAY,0) AS FOODREVENUE_TODAY_PY
    ,isnull(t.FOODREVENUE_MTOD,0) AS FOODREVENUE_MTOD_CY,isnull(y.FOODREVENUE_MTOD,0) AS FOODREVENUE_MTOD_PY
    ,isnull(t.FOODREVENUE_YTOD,0) AS FOODREVENUE_YTOD_CY,isnull(y.FOODREVENUE_YTOD,0) AS FOODREVENUE_YTOD_PY
    ,isnull(t.LIQUORREVENUE_TODAY,0) AS LIQUORREVENUE_TODAY_CY,isnull(y.LIQUORREVENUE_TODAY,0) AS LIQUORREVENUE_TODAY_PY
    ,isnull(t.LIQUORREVENUE_MTOD,0) AS LIQUORREVENUE_MTOD_CY,isnull(y.LIQUORREVENUE_MTOD,0) AS LIQUORREVENUE_MTOD_PY
    ,isnull(t.LIQUORREVENUE_YTOD,0) AS LIQUORREVENUE_YTOD_CY,isnull(y.LIQUORREVENUE_YTOD,0) AS LIQUORREVENUE_YTOD_PY
    ,isnull(t.SOFTDRINKSREVENUE_TODAY,0) AS SOFTDRINKSREVENUE_TODAY_CY,isnull(y.SOFTDRINKSREVENUE_TODAY,0) AS SOFTDRINKSREVENUE_TODAY_PY
    ,isnull(t.SOFTDRINKSREVENUE_MTOD,0) AS SOFTDRINKSREVENUE_MTOD_CY,isnull(y.SOFTDRINKSREVENUE_MTOD,0) AS SOFTDRINKSREVENUE_MTOD_PY
    ,isnull(t.SOFTDRINKSREVENUE_YTOD,0) AS SOFTDRINKSREVENUE_YTOD_CY,isnull(y.SOFTDRINKSREVENUE_YTOD,0) AS SOFTDRINKSREVENUE_YTOD_PY
    ,isnull(t.TOBACCOREVENUE_TODAY,0) AS TOBACCOREVENUE_TODAY_CY,isnull(y.TOBACCOREVENUE_TODAY,0) AS TOBACCOREVENUE_TODAY_PY
    ,isnull(t.TOBACCOREVENUE_MTOD,0) AS TOBACCOREVENUE_MTOD_CY,isnull(y.TOBACCOREVENUE_MTOD,0) AS TOBACCOREVENUE_MTOD_PY
    ,isnull(t.TOBACCOREVENUE_YTOD,0) AS TOBACCOREVENUE_YTOD_CY,isnull(y.TOBACCOREVENUE_YTOD,0) AS TOBACCOREVENUE_YTOD_PY
    ,isnull(t.OTHERREVENUE_TODAY,0) AS OTHERREVENUE_TODAY_CY,isnull(y.OTHERREVENUE_TODAY,0) AS OTHERREVENUE_TODAY_PY
    ,isnull(t.OtherRevenue_MTOD,0) AS OTHERREVENUE_MTOD_CY,isnull(y.OtherRevenue_MTOD,0) AS OTHERREVENUE_MTOD_PY
    ,isnull(t.OTHERREVENUE_YTOD,0) AS OTHERREVENUE_YTOD_CY,isnull(y.OTHERREVENUE_YTOD,0) AS OTHERREVENUE_YTOD_PY
    ,isnull(t.FOODCOVERS_TODAY,0) AS FOODCOVERS_TODAY_CY,isnull(y.FOODCOVERS_TODAY,0) AS FOODCOVERS_TODAY_PY
    ,isnull(t.FOODCOVERS_MTOD,0) AS FOODCOVERS_MTOD_CY,isnull(y.FOODCOVERS_MTOD,0) AS FOODCOVERS_MTOD_PY
    ,isnull(t.FOODCOVERS_YTOD,0) AS FOODCOVERS_YTOD_CY,isnull(y.FOODCOVERS_YTOD,0) AS FOODCOVERS_YTOD_PY
    ,isnull(t.LIQUORCOVERS_TODAY,0) AS LIQUORCOVERS_TODAY_CY,isnull(y.LIQUORCOVERS_TODAY,0) AS LIQUORCOVERS_TODAY_PY
    ,isnull(t.LIQUORCOVERS_MTOD,0) AS LIQUORCOVERS_MTOD_CY,isnull(y.LIQUORCOVERS_MTOD,0) AS LIQUORCOVERS_MTOD_PY
    ,isnull(t.LIQUORCOVERS_YTOD,0) AS LIQUORCOVERS_YTOD_CY,isnull(y.LIQUORCOVERS_YTOD,0) AS LIQUORCOVERS_YTOD_PY
    ,isnull(t.OTHERCOVERS_TODAY,0) AS OTHERCOVERS_TODAY_CY,isnull(y.OTHERCOVERS_TODAY,0) AS OTHERCOVERS_TODAY_PY
    ,isnull(t.OTHERCOVERS_MTOD,0) AS OTHERCOVERS_MTOD_CY,isnull(y.OTHERCOVERS_MTOD,0) AS OTHERCOVERS_MTOD_PY
    ,isnull(t.OTHERCOVERS_YTOD,0) AS OTHERCOVERS_YTOD_CY,isnull(y.OTHERCOVERS_YTOD,0) AS OTHERCOVERS_YTOD_PY
    ,isnull(t.TOBACCOCOVERS_TODAY,0) AS TOBACCOCOVERS_TODAY_CY,isnull(y.TOBACCOCOVERS_TODAY,0) AS TOBACCOCOVERS_TODAY_PY
    ,isnull(t.TOBACCOCOVERS_MTOD,0) AS TOBACCOCOVERS_MTOD_CY,isnull(y.TOBACCOCOVERS_MTOD,0) AS TOBACCOCOVERS_MTOD_PY
    ,isnull(t.TOBACCOCOVERS_YTOD,0) AS TOBACCOCOVERS_YTOD_CY,isnull(y.TOBACCOCOVERS_YTOD,0) AS TOBACCOCOVERS_YTOD_PY
FROM cte  t
LEFT JOIN cte as y
  ON DATEADD(YEAR,-1,t.ACCOUNTDATE) = y.ACCOUNTDATE
    order by t.ACCOUNTDATE