我在数据库中有一个Employee表,它有两列employee_card和ledger_month。员工可以与多个分类帐月份有关系。现在我想让员工保持最高的分类帐月,其余的将被删除。
Input:
Employee_card Ledger_Month
1 111112
1 111114
2 111112
2 111114
Output :
Employee_card Ledger_Month
1 111114
2 111114
我试过像这样的查询
delete from v2titas.EMPLOYEE_COPY_UPGRADED where card
not in(select card,max(ledger_month) from v2titas.EMPLOYEE_COPY_UPGRADED group by card)
or
ledger_month not in (select card,max(ledger_month) from
v2titas.EMPLOYEE_COPY_UPGRADED group by card)
但它显示了这样的错误"太多的价值"。我怎么能这样做?
答案 0 :(得分:0)
DELETE FROM v2titas.EMPLOYEE_COPY_UPGRADED et
WHERE EXISTS (
SELECT *
FROM v2titas.EMPLOYEE_COPY_UPGRADED it
WHERE et.card = it.card
AND et.Ledger_Month < it.Ledger_Month );
答案 1 :(得分:0)
尝试以下查询..
从emp_test中删除ledger_month in(从emp_test e中选择ledger_month,其中(employee_card,ledger_month)不在 (从emp_test e1中选择employee_card,max(ledger_month),其中e.employee_card = e1.employee_card group by employee_card));
谢谢, 巴拉