Saturday 15 March 2014

Refactoring SQL code -


I'm looking for suggestions on reacting the code shown below, you will see that 'DATAPOINT2' and 'DATAPOINT3 The codes identified as' are very similar and mature enough for simplicity. But I'm walking in a wall trying how I can beautifully combine 'DATAPOINT2' and 'DATAPOINT3'.

  CREATE TABLE employee (EmployeeID integer, BonusPaidOn date of VARCHAR (50)); Include employee prices (1, '01 -JAN-2013 ',' full payment '); Include employee prices (2, '01 -FEB-2013 ',' full payment '); Include employee prices (2, '01-MAR-2013 ',' partial payment '); Include employee prices (2, '01-APR-2013 ',' partial payment '); Include employee prices (3, '01-APR-2013 ',' disapproved '); COMMIT; In the form of DATAPOINT1 (Employee Separate EmployeeID Selection), as DATAPOINT2 (Selection EmployeeID, Status, Max (BonusPaidOn) In the form of BonusPaidOn where in the position (EmployeeID 'completed payment', 'partial payment', ' Refused (with 'Group', status), DATAPOINT3 as BonusPaidOn (EmployeeID, Max (Select BonusPaidOn) DATAPOINT2 Temporary where in the situation ('paid', 'partial payment') by EmployeeID Group) Select A.EmployeeId, B.BonusPaidOn LastBonusPaidInFullOn, C Sol. The Last Bonus Paid On Bonus Paddle, D. A outstanding octa from Bonuspadon AS LastBoneus Refoan Datapiniti 1 and DATAPNIT2B. Ampliid = B. EmpireID and Bistatus = 'paid in full' left over at dotpoint 3c. EmployID = C. EmpireID on the left JOE DATAPOINT2D A.EmployeeId = D.EmployeeId and D. Status = 'refused';   

A request was made for the expected result:

  EmployeeID LastBonusPaidInFullOn LastBonusPaidOn LastBonusRefusedOn ---------- ----- - --------------- -------------------------------------- 3 01-APR-13 1 01-Jan-13 01-Jan-13 2 01 Feb, 13 01-Apr 13    

The query looks very complex. I think the following does what you want - geting the last date for each of the many types of situations:

  Select EmployeeID, max (in case when the situation is completed ('paid' ) In the case of LastBonusPaidInFullOn max (when in case ('partial payment') then the last bonus pad over max) (when in case the situation ('Disapproved') in the form of Bonuspad passenger) from the employee WHERE status ('in full Paid Security ') "partial payment" refused' ') group by employee ID    

No comments:

Post a Comment