我在名为CART的表中有以下值:
ID EMP_EMAIL PRODUCT ACTIVITY_TYPE
1 USER@EMAIL.COM Book Add
2 USER@EMAIL.COM Book Remove
3 USER@EMAIL.COM Kindle Add
4 USER@EMAIL.COM Kindle Remove
5 USER@EMAIL.COM Phone Add
6 USER@EMAIL.COM Phone Remove
7 USER@EMAIL.COM Book Add
最后,我需要保留一行,最后一行是Add Book。
行按顺序排序。这意味着用户选择了一本书。然后取出书并选择了Kindle。然后取出Kindle并选择了手机等等。
最后我只需要输出用户选择的书。
此示例也可能具有空输出:
ID EMP_EMAIL PRODUCT ACTIVITY_TYPE
1 USER@EMAIL.COM Book Add
2 USER@EMAIL.COM Book Remove
3 USER@EMAIL.COM Kindle Add
4 USER@EMAIL.COM Kindle Remove
5 USER@EMAIL.COM Phone Add
6 USER@EMAIL.COM Phone Remove
这不应该返回任何内容,因为最后一个操作是删除。
我可以在sql或pl / sql中找到任何建议。
答案 0 :(得分:0)
如果要选择已添加但未删除的所有内容。假设没有重复:
current is 1
如果要选择具有更多添加项的项目,请删除:
select c.*
from cart c
where c.activity_type = 'Add' and
not exists (select 1
from cart c2
where c2.emp_email = c.emp_email and
c2.product = c.product and
c2.activity_type = 'Remove'
);
答案 1 :(得分:0)
它将列出已添加但未删除的产品。
SELECT emp_email,product
FROM test
GROUP BY (emp_email,product)
HAVING CASE WHEN SUM(CASE activity_type WHEN 'Add' THEN 1 ELSE -1 END)!=0 THEN 1 ELSE 0 END =1;