SQL 服务器查询:解析 Header 组的条目

SQL Server Query: Parse entry to Header groups

不确定如何表达我的问题,但我想知道如何编写一个 SQL 查询来复制下面的 table。

目前,table 是通过重新格式化 Epicor 销售积压报告生成的,我想稍微简化一下,让 SQL 查询直接提取相同的信息。

列 A-F 我没有问题,它们只是可以在销售订单 table 之一中找到的字段。但是,对于 G-K 列,它所做的是在 F 中获取到期日并将到期金额放入具有匹配月份的列中。列 G-K 可以考虑 "Value of Order Releases Due On or Before" ..month

因此,它不是只在 G 列中显示到期金额,而是根据 F 列中的月份将到期金额放在 G-K 列之一中。我猜有点古怪,但有任何想法如何在 SQL 语句中实现它?

我不确定您真正想要什么...但我认为这对您正在做的事情很有帮助...

select c.custid Customer, 'your string' [Order /Ln /Rel], od.PartNum,pg.ProdCode ProdGrup,st.TerritoryDesc,oh.OrderDate
,case when month(oh.OrderDate) = '1' then sum(oh.TotalCharges) end as 'Month 1'
,case when month(oh.orderDate) = '2' then sum(oh.TotalCharges) end as 'Month 3'
,case when month(oh.orderDate) = '3' then sum(oh.TotalCharges) end as 'Month 4'
,case when month(oh.orderDate) = '4' then sum(oh.TotalCharges) end as 'Month 5'
,case when month(oh.orderDate) = '5' then sum(oh.TotalCharges) end as 'Month 6'
from Erp.OrderHed oh
left join Erp.OrderRel orl on orl.Company = oh.Company and orl.OrderNum = oh.OrderNum
left join Orderdtl od on od.Company = orl.Company and od.OrderNum = orl.OrderNum and od.OrderLine = orl.OrderLine
left join Erp.Customer c on c.Company = od.Company and c.CustNum = od.CustNum 
left join Erp.Part p on p.Company = orl.Company and p.PartNum = orl.PartNum
left join Erp.ProdGrup pg on pg.Company = p.Company and pg.ProdCode = p.ProdCode
left join Erp.SalesTer st on st.Company = c.Company and st.TerritoryID = c.TerritoryID
group by c.custid, od.PartNum,pg.ProdCode,st.TerritoryDesc,oh.OrderDate
order by oh.OrderDate