两个表的联合,但显示数据来自哪个表

时间:2012-08-29 18:49:46

标签: sql union highest

我有两张桌子:

           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   

然后为了使其更难,我想知道是否有一种方法可以判断数据来自哪个表。当记录A和记录B相等时,表A总是获胜 寻找这个作为答案:

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

我知道无法完成UNION以获得此结果。

5 个答案:

答案 0 :(得分:1)

在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没有QUALIFY子句,则使用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;

答案 1 :(得分:0)

这会有用吗?

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的观点是正确的,min(src)将在全局集上,而不是相关的max()

我认为你必须将源和表值组合成一个最大的列。在您的示例中,只需在值中添加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

答案 2 :(得分:0)

我想提供的是,您可以使用标准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)

答案 3 :(得分:0)

如果Amend仅具有值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

答案 4 :(得分:0)

如果您在选择中添加了一个字符串并将其作为列别名,该怎么办?

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