Sql server 空列上的SQL分组依据
我有一个包含以下部分的查询:Sql server 空列上的SQL分组依据,sql-server,group-by,null,Sql Server,Group By,Null,我有一个包含以下部分的查询: 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
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)
我的问题是如何将curr_stock.[Customer Group]包含在Group by子句中?它不在原始表中,它只是在select子句中创建的。
它是必需的,因为我想使用UNIONALL,并且我的所有其他表都有这个列。
或者有没有其他办法来解决这个问题
谢谢 谢谢!这很好用,但我怎么能把它用在联合体中呢?我没有问题。请您提供更多的细节。顺便说一句,你可以在cte中使用union all。好的,我现在解决了所有问题。你的答案是关键!再次感谢!
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