查询删除
Query for delete
我在数据库中有一个员工 table,它有两列 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)
但它显示这样的错误 "too many value"。我该怎么做?
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 );
试试下面的查询..
delete from emp_test where ledger_month in (select ledger_month from emp_test e where (employee_card, ledger_month ) 不在
(select employee_card, max(ledger_month) from emp_test e1 where e.employee_card=e1.employee_card group by employee_card));
谢谢,
巴拉
我在数据库中有一个员工 table,它有两列 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)
但它显示这样的错误 "too many value"。我该怎么做?
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 );
试试下面的查询..
delete from emp_test where ledger_month in (select ledger_month from emp_test e where (employee_card, ledger_month ) 不在 (select employee_card, max(ledger_month) from emp_test e1 where e.employee_card=e1.employee_card group by employee_card));
谢谢, 巴拉