4

我有两张桌子:

           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 来获得此结果。

4

5 回答 5

2

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

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
于 2019-01-23T17:35:52.763 回答
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;
于 2012-08-29T18:55:52.697 回答
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
于 2012-08-29T18:56:00.043 回答
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)
于 2012-08-29T19:00:34.897 回答
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
于 2012-08-29T19:06:22.783 回答