2017-01-30 6 views
0

Ich habe eine Pivot-Abfrage auszuführen versucht, aber ich versagt hart, ich bin sehr neu mit all dies so bitte geduldigPivot und INNER JOIN

, was ich will, ist die Mengen Werte eines jeden Monats zurückzukehren, Januar, Februar ... Dezember, für jede Teilereferenz

ist das, was ich

SELECT PartRef 
     , Year 
     , fMonth 
     , sum(Quantity) as Quantity 

FROM(SELECT PartRef 
       , year(DateClosed) as Year 
       , month(DateClosed) as Month 
       , SUM(fldShipped) as Quantity 

    FROM PartsInvoice 
    INNER JOIN Requests ON PartsInvoice.fID = Requests.WorkItemRef 
    INNER JOIN PartsLine ON Requests.ID = PartsLine.RequestRef 

    WHERE Closed = 1 and DateClosed > DateAdd(mm, DateDiff(mm, 0, GetDate()) -12, 0) 
    GROUP BY PartRef, year(DateClosed), month(DateClosed) 

) as SalesHits 

PIVOT (

SUM(NOT SURE)FOR NOT SURE IN ([Jan],[Feb],[Mar],[Apr],[May],[June],[July],[Ago],[Sep],[Oct],[Nov],[Dec]) 
)AS Hits 

GROUP BY PartRef, Year, Month 

Antwort

0

Hier haben Sie ein Beispiel haben, wie es mit einem Tisch wie das Ihre funktioniert.

declare @table table(
    partref int, 
    year int, 
    month nvarchar(50), 
    quantity int 
) 

insert into @table values 
(1,2016,'jan',12), 
(1,2016,'feb',12), 
(2,2016,'jan',12), 
(2,2016,'feb',12), 
(1,2016,'jan',12) 

select PartRef 
     , year 
     , sum([jan]) 'Jan',sum([feb]) 'Feb' 
     ,sum([mar]),sum([apr]),sum([may]),sum([jun]),sum([jul]) 
     ,sum([aug]),sum([sep]),sum([oct]),sum([nov]),sum([dec]) 
     from(
     SELECT PartRef 
       , year 
       , [jan],[feb],[mar],[apr],[may],[jun],[jul],[aug],[sep],[oct],[nov],[dec] 
       from @table 
       PIVOT (
     SUM(quantity)FOR month IN ([jan],[feb],[mar],[apr],[may],[jun],[jul],[aug],[sep],[oct],[nov],[dec]) 
     )AS Hits 
) as t 
group by PartRef,year 
+0

danke für die Antwort zu arbeiten, ich habe bereits Daten in den Tabellen und Du hast den INNER JOIN gelöscht, ?? –

+0

Ich vereinfachte die Abfrage, damit sie mit einfachen Daten funktioniert. Sie können die Bedingungen hinzufügen, die Sie für Ihre Daten benötigen – AlbertoCh

0

hier ist der volle Umfang meiner Abfrage und ich glaube, ich habe es :)

SELECT *

FROM(SELECT PartRef 
    , year(DateClosed) as Year 
    , month(DateClosed) as Month 
    , SUM(Shipped) as Quantity 

    FROM PartsInvoice 
    INNER JOIN Requests ON PartsInvoice.ID = Requests.WorkItemRef 
    INNER JOIN PartsLine ON Requests.ID = PartsLine.RequestRef 

    WHERE HasClosed = 1 and DateClosed > DateAdd(mm, DateDiff(mm, 0, GetDate()) -13, 0) 
    GROUP BY PartRef, year(DateClosed), month(DateClosed) 

    UNION ALL 

    --RO 
    SELECT PartRef 
    , year(DateClosed) as Year 
    , month(DateClosed) as Month 
    , SUM(Shipped) as Quantity 

    FROM RepairOrder 
    INNER JOIN Requests ON RepairOrder.ID = Requests.WorkItemRef 
    INNER JOIN PartsLine ON Requests.ID = PartsLine.RequestRef 

    WHERE Status = 3 and DateClosed > DateAdd(mm, DateDiff(mm, 0, GetDate()) -13, 0) 
    GROUP BY PartRef, year(DateClosed), month(DateClosed) 

    UNION ALL 

    -- Historical Hits 
    SELECT PartRef 
    , year(date) as Year 
    , month(Date) as Month 
    , SUM(Quantity) as Quantity 

    FROM PartsHistoricalHits 

    WHERE Date > DateAdd(mm, DateDiff(mm, 0, GetDate()) -13, 0) 
    GROUP BY PartRef, year(Date), month(Date) 


) as SalesHits 

    PIVOT (

    COUNT (Quantity)FOR fldMonth IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13]) 
)AS Hits