2017-01-30 7 views
0

に参加するには、各月の数量値を返すことです、 1月、2月... 12月には、各PartRefピボットとINNERので、私が欲しいもの</p> <p>お待ちください、私はピボットクエリを実行しようとしているが、私はハード障害が発生しています、私はこのすべてに非常に新しいです

のために、これは私が

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 

答えて

0

持っているものであるここであなたはそれがあなたのようなテーブルでどのように機能するかの例があります。ここ

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

感謝を選択:)動作するようになったと信じて、私はすでにテーブル内のデータを持っており、あなたはINNER JOINを削除しましたか? –

+0

シンプルなデータでクエリを簡単にするために、データに必要な条件を追加することができます – AlbertoCh

0

は私のクエリの完全な範囲であると、私はそれが

は返信用*

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 
関連する問題

 関連する問題