我有大型数据集,我想比较数据。例如,预算和实际之间的比较。根据讨论的(管理)级别,我需要类似的查询。
数据集:
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上的select,但是我需要这样才能得到模糊的名称错误。
当我开始工作时,我希望将其复制到不同的级别,例如客户/文章/行等的分析,并且下一步在srss中生成报告,其中变量被声明并由用户用于在期间,年份上进行选择等。
答案 0 :(得分: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;
注意:将year
和period
存储为float
是愚蠢的。 INT
是有道理的。 DECIMAL(4)
是有道理的。即使是一根绳子。但是不要使用浮点表示,除非这是你真正想要的。
同样,不要在数字常量周围加上单引号。单引号只能用于日期和字符串常量。