SELECT * FROM dbo.SBD_WAYBILL_GOODS WHERE WG_SW_ID IN (SELECT WG_SW_ID FROM dbo.SBD_WAYBILL_GOODS GROUP BY WG_SW_ID HAVING COUNT(*)>1 ) ORDER BY WG_ID SELECT * FROM dbo.SBD_WAYBILL WHERE SW_JOURNAL_NUMBER IN (SELECT SW_JOURNAL_NUMBER FROM dbo.SBD_WAYBILL GROUP BY SW_JOURNAL_NUMBER HAVING COUNT(*)>1) SELECT DISTINCT SW_JOURNAL_NUMBER FROM dbo.SBD_WAYBILL DELETE dbo.PDA_ERROR WHERE PE_ERRORVALUE IN(SELECT MAX(PW_ID) SELECT * FROM dbo.PDA_ERROR WHERE PE_ID IN (SELECT MAX(PE_ID) FROM dbo.PDA_ERROR GROUP BY PE_ERRORVALUE HAVING COUNT(PE_ERRORVALUE)>1) SELECT SW_ID,SW_CREATE_DATE,SW_NUMBER,SW_JOURNAL_NUMBER, (SELECT SS_NAME FROM dbo.SBD_STATION WHERE SS_ID=SW_DEPARTURE) AS DEPARTURE, (SELECT SS_NAME FROM dbo.SBD_STATION WHERE SS_ID=SW_DESTINATION) AS DESTINATION, (SELECT CI_NAME FROM dbo.SBD_CUSTOMER_INFO WHERE CI_ID=SW_FROM_ID) AS fromid, (SELECT CI_NAME FROM dbo.SBD_CUSTOMER_INFO WHERE CI_ID=SW_TO_ID) AS toid FROM dbo.SBD_WAYBILL WHERE SW_JOURNAL_NUMBER=@SW_JOURNAL_NUMBER SELECT COUNT(*) FROM dbo.SBD_WAYBILL WHERE SW_JOURNAL_NUMBER=@SW_JOURNAL_NUMBER SELECT COUNT(PE_ID) FROM dbo.PDA_ERROR WHERE PE_ERRORVALUE='2345'