Sql 两个表的并集,但显示数据来自哪个表
我有两张桌子:Sql 两个表的并集,但显示数据来自哪个表,sql,union,highest,Sql,Union,Highest,我有两张桌子: TABLE_A TABLE_B Fields: Trans Amend Trans Amend data: 100 0 100 0 100 1 110 0 120 0
TABLE_A TABLE_B
Fields: Trans Amend Trans Amend
data: 100 0 100 0
100 1
110 0
120 0
120 1
130 0 130 0
130 1
140 0 140 0
150 0 150 0
150 1 150 1
150 2
我想要的是一个表(视图),它将这些表合并(并集),但只显示每个Trans的最高修正值
寻找这个答案:
Fields: Trans Amend
data: 100 1
110 0
120 1
130 1
140 0
150 2
Fields: Trans Amend WhichTBL
data: 100 1 Table_A
110 0 Table_A
120 1 Table_B
130 1 Table_B
140 0 Table_A
150 2 Table_A
然后,更难的是,我想知道是否有一种方法可以从哪个表中判断数据来自哪个表。当记录A和记录B相等时,表A总是获胜
寻找这个答案:
Fields: Trans Amend
data: 100 1
110 0
120 1
130 1
140 0
150 2
Fields: Trans Amend WhichTBL
data: 100 1 Table_A
110 0 Table_A
120 1 Table_B
130 1 Table_B
140 0 Table_A
150 2 Table_A
我知道不能通过联合来获得此结果。在Teradata SQL中,您可以执行以下操作,但不确定SQL Server:
select trans,amend,WhichTBL from
(
select trans,amend,'Table_A' WhichTBL from Table_A
union
select trans,amend,'Table_B' WhichTBL from Table_B
) X
qualify row_number() over(partition by trans order by amend desc, WhichTBL) = 1
order by trans;
如果您的SQL没有限定子句,则使用Lucero建议的版本:
select trans,amend,WhichTBL from
(
select x.*,row_number() over(partition by trans order by amend desc, WhichTBL) as rn
(
select trans,amend,'Table_A' as WhichTBL from Table_A
union
select trans,amend,'Table_B' as WhichTBL from Table_B
) Derived1 as X
) Derived2
where rn = 1
order by trans;
这样行吗
SELECT
trans, MAX(max_amend) as max_max_amend
FROM
(SELECT
'a' AS src, trans, MAX(amend) AS max_amend
FROM
table_a
GROUP BY
trans
UNION ALL
SELECT
'b' AS src, trans, MAX(amend) AS max_amend
FROM
table_b
GROUP BY
trans) m
GROUP BY
trans
Lucero下面的观点是正确的,最小值(src)应该在全局集合上,而不是相关的最大值() 我认为您必须将源值和表值合并到一个列中,以便最大化。在您的示例中,只需在值中添加1即可区分源,如:
SELECT trans, Max(amend) AS MaxOfamend, 1+[amend] AS isa, 0 AS isb
FROM TableA
GROUP BY trans
但是你可以加上100,或者乘以一个大的值,或者任何与你的数据相关的东西。其思想是将修正值和来源这两条信息合并到一列中
然后,在合并信息后,获得该值的最大值,然后通过取消组合来去除源标志(减去1,除以100,随便什么)
好的,这是我得到的:
CREATE VIEW [dbo].[viewA] AS
SELECT trans, MAX(amend + .20) AS srcIsA, 0 AS srcIsb
FROM dbo.tableA
GROUP BY trans
CREATE VIEW [dbo].[viewB] AS
SELECT trans, 0 AS srcIsA, MAX(amend + .10) AS srcIsB
FROM dbo.tableB
GROUP BY trans
CREATE VIEW [dbo].[viewU] AS
SELECT * from viewA
union all
select *
FROM viewb
CREATE VIEW [dbo].[viewv] AS
SELECT trans, srcIsA, srcIsb, srcIsA + srcIsb AS total
FROM dbo.viewU
CREATE VIEW [dbo].[vieww] AS
SELECT trans, MAX(total) AS max_total
FROM dbo.viewv
GROUP BY trans
CREATE VIEW [dbo].[viewx] AS
SELECT trans,
max_total,
CAST(max_total AS int) AS maxval,
CASE WHEN (max_total - CAST(max_total AS int)) = .1 THEN 'a' ELSE 'b' END AS src
FROM dbo.vieww
我想告诉您,您可以使用标准SQL通过连接和聚合实现这一点:
select coalesce(a.trans, b.trans) as trans,
(case when coalesce(max(b.amend), -1) > coalesce(max(a.amend), -1)
then max(b.amend)
else max(a.amend)
end) as amend,
(case when coalesce(max(b.amend), -1) > coalesce(max(a.amend) , -1)
then 'B' else 'A'
end) as whichTable
from Table_A a full outer join
Table_B b
on a.trans = b.trans
group by coalesce(a.trans, b.trans)
如果修正值仅为1和0 那么第一个问题就可以解决了
select Trans,sum(Amend) AmendMax from (select Trans,Amend from TABLE_A
union select Trans,Amend from TABLE_B) C group by Trans
第二个问题是
select Trans,max(Amend) Amend,case when sum(s)=1 or sum(s)=2 or sum(s)=21
then 'Table-A' when sum(s)=10 or sum(s)=12 or sum(s)=20 then 'Table-B'
when sum(s)=11 or sum(s)=22 then 'Table-A and B' end s from
(select case when max(Amend)=1 then 1 else 2 end s,Trans,max(Amend) Amend from TABLE_A
group by Trans union select case when max(Amend)=1
then 10 else 20 end s,Trans,max(Amend) Amend from TABLE_B group by Trans) C group by Trans
如果在select中添加一个字符串并将其别名为列,该怎么办
SELECT Trans, Amend, 'Table_A' as WhichTBL
FROM (your 1st select query)
UNION
SELECT Trans, Amend, 'Table_B' as WhichTBL
FROM (your 2nd select query)
ORDER BY Trans
GROUP BY
不会将其剪切-MAX(MAX\u amend)
可能来自与MIN(src)
不同的行(从而来自不同的源表),因此该行的(真实)原点将丢失(例如,在示例中,您将使用amend 1将trans 130属性化为表a)。并非所有数据库系统中都存在QUALIFY
子句,但可以通过使用嵌套查询来模拟,该查询公开行号()
,然后使用WHERE
子句筛选此查询。这应标记为正确答案。但是,最好将union
更改为union all
(因为行从来都不是相同的,所以没有必要对它们进行重复数据消除)。错误:每当a.amend
为NULL时(由只找到ab.trans
的完整外部联接引起),您的MAX(a.amend)
也将为NULL,因此,条件max(b.amend)>max(a.amend)
将返回false,这将选择“a”表值和标识符,尽管它为NULL(哦,还要注意'a“
字符串文本上的语法错误)。@Lucero..准确地说,max(a.amend)或max(b.amend)需要为NULL才能发生故障(当amend在数据中为NULL时略有不同)。在任何情况下,我修复了这个问题,假设amend在数据中总是非负的(就像在示例数据中一样)。在max(b.amend)
为NULL的情况下(无意中?)之所以有效,是因为案例的ELSE
部分是要使用的正确答案-这就是为什么我只指出了另一部分。接受的答案可能会返回错误的结果。应该使用Carlos A.Ibarra的解决方案。我在实际解决了一个解决方案后更新了我的答案。请重新考虑。