将查询保存在isq的临时表中

时间:2018-06-29 14:43:19

标签: sql sql-server ssms

我有查询

WITH cte AS 
(
    SELECT 
        *, 
        DATEPART(WEEKDAY, Dt) AS WeekDay,
        PERCENTILE_CONT(0.75) WITHIN GROUP (ORDER BY SaleCount) 
                OVER (PARTITION BY ItemRelation, DocumentNum, DocumentYear) as PERCENTILE,
        AVG(SaleCount) OVER (PARTITION BY ItemRelation, DocumentNum, DocumentYear, DATEPART(WEEKDAY, Dt), IsPromo) AS AVG_WeekDay
    FROM 
        [Action].[dbo].[promo_data_copy]
)
UPDATE a 
SET SaleCount = cte.AVG_WeekDay
FROM CTE
JOIN [Action].[dbo].[promo_data_copy] a ON a.Dt = cte.dt
                                        AND a.ItemRelation = cte.ItemRelation 
                                        AND a.DocumentNum = cte.DocumentNum 
                                        AND a.DocumentYear = cte.DocumentYear 
                                        AND a.ispromo = cte.ispromo
WHERE CTE.PERCENTILE < CTE.SaleCount
  AND DATEPART(WEEKDAY, CTE.Dt) < 5
  AND CTE.ispromo = 0 ;

当我运行它时,我的表promo_data_copy被更新。不对。

请告诉我,该怎么做,最终结果将返回到临时表中,让“ temp_table”出现?

编辑:

WITH cte AS 
(
    SELECT 
        *,
        DATEPART(WEEKDAY, Dt) AS WeekDay,
        PERCENTILE_CONT(0.75) WITHIN GROUP (ORDER BY SaleCount) 
              OVER (PARTITION BY ItemRelation, DocumentNum, DocumentYear) as PERCENTILE,
        AVG(SaleCount) OVER (PARTITION BY ItemRelation, DocumentNum, DocumentYear, DATEPART(WEEKDAY, Dt), IsPromo) AS AVG_WeekDay
    FROM 
        [Action].[dbo].[promo_data_copy]
)
SELECT * 
INTO #temp_table 
FROM cte 

SET SaleCount = cte.AVG_WeekDay
FROM CTE
JOIN #temp_table a ON a.Dt = cte.dt
                   AND a.ItemRelation = cte.ItemRelation 
                   AND a.DocumentNum = cte.DocumentNum 
                   AND a.DocumentYear = cte.DocumentYear 
                   AND a.ispromo = cte.ispromo
WHERE CTE.PERCENTILE < CTE.SaleCount
  AND DATEPART(WEEKDAY, CTE.Dt) < 5
  AND CTE.ispromo = 0 ;

错误

  

信息102,第15级,状态1,第12行
  “ =”构造附近的语法不正确。

编辑#2:

WITH cte AS 
(
    SELECT 
        *,
        DATEPART(WEEKDAY, Dt) AS WeekDay,
        PERCENTILE_CONT(0.75) WITHIN GROUP (ORDER BY SaleCount) 
              OVER (PARTITION BY ItemRelation, DocumentNum, DocumentYear) as PERCENTILE,
        AVG(SaleCount) OVER (PARTITION BY ItemRelation, DocumentNum, DocumentYear, DATEPART(WEEKDAY, Dt), IsPromo) AS AVG_WeekDay
    FROM 
        [Action].[dbo].[promo_data_copy]
)
SELECT * 
INTO #temp_table 
FROM cte 

UPDATE a
SET SaleCount = cte.AVG_WeekDay
FROM #temp_table
JOIN #temp_table a ON a.Dt = cte.dt
                   AND a.ItemRelation = cte.ItemRelation 
                   AND a.DocumentNum = cte.DocumentNum 
                   AND a.DocumentYear = cte.DocumentYear 
                   AND a.ispromo = cte.ispromo
WHERE CTE.PERCENTILE < CTE.SaleCount
  AND DATEPART(WEEKDAY, CTE.Dt) < 5
  AND CTE.ispromo = 0 ;

然后我得到许多相同类型的错误

  

信息4104,级别16,状态1,第11行
  无法绑定复合标识符“ cte.dt”

     

信息4104,级别16,状态1,第11行
  无法绑定复合标识符“ cte.ItemRelation”

     

信息4104,级别16,状态1,第11行
  无法绑定复合标识符“ cte.DocumentNum”

如何更正查询?

1 个答案:

答案 0 :(得分:1)

如果我理解正确,那么您希望CTE进入临时表,可以通过以下方式完成:

 with cte as (
    Select *,datePart(WEEKDAY,Dt) as WeekDay,
    PERCENTILE_CONT(0.75) WITHIN GROUP (ORDER BY SaleCount) Over (partition by ItemRelation,
    DocumentNum, DocumentYear) as PERCENTILE,
    avg(SaleCount) over (Partition by ItemRelation, 
    DocumentNum, DocumentYear,datePart(WEEKDAY,Dt), IsPromo) as AVG_WeekDay
    From [Action].[dbo].[promo_data_copy])

select * into #temp_table from cte 

编辑

如果要将输出的记录输出到临时表中,则需要创建一个临时表并将输出子句添加到update语句中。

create #temp_table
(
...
)

with cte as (
Select *,datePart(WEEKDAY,Dt) as WeekDay,
PERCENTILE_CONT(0.75) WITHIN GROUP (ORDER BY SaleCount) Over (partition by ItemRelation,
DocumentNum, DocumentYear) as PERCENTILE,
avg(SaleCount) over (Partition by ItemRelation, 
DocumentNum, DocumentYear,datePart(WEEKDAY,Dt), IsPromo) as AVG_WeekDay
From [Action].[dbo].[promo_data_copy])
Update a 
Set SaleCount = cte.AVG_WeekDay
output inserted.* into #temp_table
From CTE
join [Action].[dbo].[promo_data_copy] a 
  on a.Dt = cte.dt
 and a.ItemRelation=cte.ItemRelation 
 and a.DocumentNum = cte.DocumentNum 
 and a.DocumentYear = cte.DocumentYear 
 and a.ispromo = cte.ispromo
Where CTE.PERCENTILE < CTE.SaleCount
and datePart(WEEKDAY,CTE.Dt) < 5
and CTE.ispromo = 0