我有一个包含以下部分的查询:
UNION ALL
------------------------ Stock from last monday ---------------------------
SELECT
[Local SKU code] as [Item No_]
,0 as [qty forecast]
,0 as [qty sold]
,0 as [qty sold py]
,sum(Quantity) as [Stock from last monday]
,0 as [actual production (kg)]
,0 as [actual production (pcs)]
,'' as [Customer Group]
,navision
,year(rep_date) as 'year'
,month(rep_date) as 'month'
,Datepart(iso_week,rep_date) as 'week'
,0 as [Bill-to Customer No_]
FROM dw_current_stock_mon as curr_stock
group by
[Local SKU code]
--here i would need curr_stock.[Customer Group]
,navision
,year(curr_stock.rep_date)
,month(curr_stock.rep_date)
,Datepart(iso_week,curr_stock.rep_date)
我的问题是,如何在group by子句中包含curr_stock。[Customer Group]?它不在原始表中,而是在select子句中创建的。 这是必需的,因为我要使用工会全部,而我的所有其他表都具有此列。 还是有其他解决方法?
谢谢!
答案 0 :(得分:1)
with cte as (SELECT
[Local SKU code] as [Item No_]
,0 as [qty forecast]
,0 as [qty sold]
,0 as [qty sold py]
,sum(Quantity) as [Stock from last monday]
,0 as [actual production (kg)]
,0 as [actual production (pcs)]
,'' as [Customer Group]
,navision
,year(rep_date) as 'year'
,month(rep_date) as 'month'
,Datepart(iso_week,rep_date) as 'week'
,0 as [Bill-to Customer No_]
FROM dw_current_stock_mon as curr_stock)
select --new field names
from cte
group by --new fields name