2017-10-04 48 views
0

我有大數據集,我想比較數據。比如預算和實際的比較。根據討論哪個(管理)級別,我需要類似的查詢。Full Outer將兩個選擇合併爲一個數據集進行分析

數據集:

Year    float 
Period    float 
Weeknumber   float 
Account    nvarchar(255) 
Customer   nvarchar(255) 
Account manager  nvarchar(255) 
Market    nvarchar(255) 
Country    nvarchar(255) 
Customercode  float 
Customername  nvarchar(255) 
Article    float 
Description   nvarchar(255) 
Productgroup  nvarchar(255) 
Line    nvarchar(255) 
Fresh    nvarchar(255) 
OwnProduction  nvarchar(255) 
NNNRevenue   decimal(10, 2) 
CM     decimal(10, 2) 
Pieces    decimal(10, 2) 
KG     decimal(10, 2) 
TriggerId   nvarchar(255) 
YearWeek   nvarchar(255) 

的最基本的查詢:

SELECT [Article] 
     ,sum([NNNRevenue]) as Revenue 
     ,sum([Pieces]) as Pieces 
     ,sum([KG]) as KG 

    FROM [PP_Test].[dbo].[history] 

    where year = '2018' 
    and period = '2' 

    group by Article 
    order by Article 

結果將類似於:

Article Revenue Pieces KG 
6852  123548,12 654813 13248,61 
10031 489642,15 4687896 56478,54 
4477  4698,78 54846 46,15 

我有[PP_Test]完全相同的查詢[DBO] 。[預算]。我需要的是一個(完整的?)加入,我的新結果將是預算或歷史文件中的任何文章。

結果舉例想:

Article H.Revenue H.Pieces H.KG  B.Revenue B.Pieces B.KG 
6852  123548,12 654813 13248,61 51346,12 321558 87156,12 
10031 489642,15 4687896 56478,54 541326  21314  13215,15 
4477  4698,78 54846 46,15  321564,74 14548  132147,87 
16531 0   0  0   1278,15 1348  1348,15 
55555 123151,15 13234 154884 0   0   0 

我的代碼

SELECT h.[Article] 
     ,sum([NNNRevenue]) as RevenueHis 
     ,sum([Pieces]) as PiecesHis 
     ,sum([KG]) as KGHis 
    ,RevenueBud 
    ,PiecesBud 
    ,KgBud 

    FROM 
    (
SELECT [Article] 
     ,sum([NNNrevenue]) as RevenueBud 
     ,sum([Pieces]) as PiecesBud 
     ,sum([KG]) as KGBud 

    FROM [PP_Test].[dbo].[budget] 

    Where year = '2018' 
    and Period = '2' 
    group by Article 
) as B 

    Full outer join [PP_Test].[dbo].[history] as H 
    on B.Article = H.Article 

    where year = '2018' 
    and period = '2' 

    group by h.Article, RevenueBud, PiecesBud, KGBud 
    order by h.Article 

這將導致表我想,但我想它存在於預算,但沒有在歷史上(實際值)的項目。這似乎是由於h.article上的選擇,但我需要這樣做,以避免模糊的名稱錯誤。

當我得到這個工作,我想複製它到不同的層次,如客戶/文章/行等的分析,並作爲下一步在srss報告變量聲明和用戶使用期間進行選擇,年等。

回答

0

full outer join上過濾很棘手。一種方法是使用子查詢:

SELECT COALESCE(B.Article, h.Article) as Article, 
     RevenueHis, PiecesHis, KGHis, 
     RevenueBud, PiecesBud, KgBud 
FROM (SELECT [Article], sum([NNNrevenue]) as RevenueBud, 
      sum([Pieces]) as PiecesBud, sum([KG]) as KGBud 
     FROM [PP_Test].[dbo].[budget] 
     WHERE year = 2018 AND Period = 2 
     GROUP BY Article 
    ) B FULL JOIN 
    (SELECT Article, sum([NNNRevenue]) as RevenueHis, 
      sum([Pieces]) as PiecesHis, sum([KG]) as KGHis 
     FROM [PP_Test].[dbo].[history] as H 
     WHERE year = 2018 AND period = 2 
     GROUP BY Article 
    ) H 
    ON B.Article = H.Article 
ORDER BY h.Article; 

注:存儲yearperiod作爲float是愚蠢的。 INT是有道理的。 DECIMAL(4)是有道理的。即使是一串。但不要使用浮點表示,除非這是你真正想要的。

同樣,不要在數字常量周圍放置單引號。單引號只能用於日期和字符串常量。

+0

就數據類型達成一致,這只是我「繼承」它的方式。如果我重新創建數據可能會做不同。 –

+0

當我在Microsoft SQL Server上嘗試您的代碼時,出現錯誤:Msg 156,Level 15,State 1,Line 15 關鍵字'ON'附近的語法錯誤。 –

+0

感謝這真的接近我需要的東西,現在我有幾行null作爲值,這我不介意,但也有一些行作爲文章號爲null,這是進一步分析的問題。 –