Sql 根据成本的最大总和,从两个表中的一个返回结果
我有两张收费的桌子。一组是实际记录的,一组是基于品牌的估计 我想做的是报告哪个更高 样本数据为: 父表:Sql 根据成本的最大总和,从两个表中的一个返回结果,sql,sql-server,group-by,Sql,Sql Server,Group By,我有两张收费的桌子。一组是实际记录的,一组是基于品牌的估计 我想做的是报告哪个更高 样本数据为: 父表: GroupId、TransactionId、Otherinfo。。。。。 123, 4444, ... 530, 2311, ... 201、1111,… 实际数据 TransactionId、产品、成本 4444, 3039, 100 4444, 3002, 4000 2311304693 估计数据 G
GroupId、TransactionId、Otherinfo。。。。。
123, 4444, ...
530, 2311, ...
201、1111,…
实际数据
TransactionId、产品、成本
4444, 3039, 100
4444, 3002, 4000
2311304693
估计数据
GroupId、品牌、成本
123, 33, 80
123, 42, 3000
530, 222, 1200
201、121、4040
在这种情况下,我要返回的是一个包含
GroupId、代码、成本
123、3039、100关系肯定是模糊的,但是预期结果是模糊的
| GROUPID | CODE | COST |
|---------|------|------|
| 123 | 3039 | 100 |
| 123 | 3002 | 4000 |
| 201 | 121 | 4040 |
| 530 | 222 | 1200 |
已通过此查询生成:
WITH
acte AS (
SELECT p.GroupId, ad.Product, ad.Cost
, SUM(ad.cost) OVER (PARTITION BY Groupid) AS grp_cost
FROM ActualData AS ad
INNER JOIN parenttable p ON ad.TransactionId = p.TransactionId
),
ecte AS (
SELECT GroupId, Brand, SUM(Cost) AS cost
FROM EstimateData
GROUP BY
GroupId
, Brand
)
SELECT acte.GroupId, acte.Product AS Code, acte.Cost
FROM acte
WHERE NOT EXISTS (
SELECT
NULL
FROM ecte
WHERE ecte.GroupId = acte.GroupId
AND ecte.cost > acte.grp_cost
)
UNION ALL
SELECT ecte.GroupId, ecte.Brand AS Code, ecte.Cost
FROM ecte
WHERE NOT EXISTS (
SELECT
NULL
FROM acte
WHERE acte.GroupId = ecte.GroupId
AND acte.grp_cost > ecte.cost
)
;
如果我理解正确,您希望从表中选择组的每一行,其中其总数最大。cte包含所有的groupId以及最大的总数来自哪个表。然后,union使用cte仅为每个表选择属于最大组的行
with cte as (
select * from (
select source, GroupId,
row_number() over (partition by GroupId order by total_cost desc) rn
from (
select 'ad' source, GroupId, sum(Cost) total_cost
from ActualData ad
group by GroupId
union all
select 'ed' source, GroupId, sum(Cost) total_cost
from EstimatedData ed
group by GroupId
) t1
) t1 where rn = 1
)
select GroupId, Product Code, Cost from ActualData ad
where GroupId in (select GroupId from cte where source = 'ad')
union all
select GroupId, Brand Code, Cost from EstimatedData ed
where GroupId in (select GroupId from cte where source = 'ed')
你的数据结构没有意义。实际和估算表是如何连接的?(即,您如何知道给定产品的品牌?)。我同意,这很模糊。基本上,我们有没有数据记录的情况,所以我们有另一个表,有估计,以弥补缺乏实际数据。在最后一个表中,代码列是品牌还是产品,取决于哪个表用于数据为什么1233039100
在所需结果表中?我猜这是因为成本高于80(估计数据中的第1行)-但您如何确定这是您需要比较的行?还是应该(123,33)
和(123,42)
也出现在输出表中,只是在示例输出中丢失了?因为实际数据中GroupId 123的总和更大。TransactionID4444=4100总计,而GroupID123的估计值仅为3080,因此在本例中我需要实际数据。GroupId 530的情况正好相反,其中估计总数更高,因此我需要估计结果。而且GroupId 201没有任何实际数据,所以我使用了估算值。CTE中的联合应该是union ALL
使用文字作为“源”消除了任何减少行数的能力,因此union
本身就是一种浪费。