sqlsql-serverepicorerp

SQL Server Query: Parse entry to Header groups


wasn't sure how to word my question but I'm trying to figure out how I would write a SQL query that would replicate the table below.

Currently that table is generated from a reformatting of an Epicor Sales Backlog report, and I want to streamline it a bit and just have a SQL query pull the same info directly.

Columns A-F I have no problems with, they're just fields that can be found in one of the Sales Order tables. However for columns G-K, what it does is take the due date in F and puts the amount due in the column with the matching month. Columns G-K can be considered "Value of Order Releases Due On or Before" ..month

So instead of just having the amount due in column G, it puts the amount due in one of the columns G-K based on the month in column F. A little querky I guess, but any ideas on how to implement this in a SQL statement?

Table output


Solution

  • Im not sure of what you really want... but i think this can be helpful for what you're doing..

    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