2015-09-21 187 views
0

我在數據庫中有一個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
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 ); 
+0

對不起,是卡emplyee_id –

+1

非常感謝你 –

0

試試下面的查詢..

刪除emp_test其中ledger_month中(選擇emp_testËledger_month其中(employee_card,ledger_month)不 (從emp_test E1其中e選擇employee_card,MAX(ledger_month) .employee_card = e1.employee_card group by employee_card));

感謝, 巴拉