Teradata场景

问题描述 投票:0回答:3

鉴于: -

当DML OPeration插入“A”然后B = 0然后DML OPeration是删除“D”然后B将保持插入记录的A值

条件: -

  1. 如果COUNT OF'我'='D'的计数,那么我们不需要那些记录。例如:ID = 111
  2. 找到最新的插入('I')DML_operation
    ID  A   B   DML_Operation
1   111 1   0   I
2   111 2   1   D
3   111 3   0   I
4   111 4   3   D
5   111 5   0   I
6   111 6   5   D
7   111 7   0   I
8   222 8   0   I
9   333 9   0   I
10  333 10  9   D
11  444 11  0   I
12  444 12  11  D
13  444 13  0   I
14  111 14  7   D
15  333 15  0   I
16  444 16  0   I
17  444 17  13  D

欲望输出

ID  A   B   DML_Operation
-------------
222 8   0   I
333 15  0   I
444 16  0   I

我的逻辑不起作用

sel ID, Max(A) from xyz
group by ID
having count(c='I') <> COUNT(c='D')
sql teradata rdbms teradata-sql-assistant
3个回答
0
投票

你觉得如下吗?

    select ID, Max(A) from xyz
    group by ID
    having sum(case when c='I' then 1 else 0 end) <> sum(case when c='D' then 1 else 0 end)

0
投票

使用case怎么样?

select ID, Max(A)
from xyz
group by ID
having sum( case when c = 'I' then 1 else 0 end) <> sum(case when c = 'D' then 1 else 0 end)

要么:

having sum(case when c = 'I' then 1
                when c = 'D' then -1
                else 0
           end) <> 0

0
投票

这将找到所有'I'行而不匹配'D'行:

SELECT *
FROM mytab AS t1
WHERE DML_Operation = 'I'
AND NOT EXISTS
 ( SELECT *
   FROM mytab AS t2
   WHERE t2.id = t1.id
     AND t2.b = t1.a 
     AND DML_Operation = 'D'
 )
© www.soinside.com 2019 - 2024. All rights reserved.