ranjith23 Posted May 30, 2017 Report Posted May 30, 2017 SQL Experts, Need SQL help to achieve below task: SQL should pull only FIRST EX to BU PAY GROUP change date for each employee. If Employee don't come under this scenario, it should skip him INPUT: EMPLOYEE DATE PAY GROUP A186 01-21-2012 EX A186 01-22-2012 EX A186 01-23-2012 EX A186 01-23-2012 BU A186 01-24-2012 EX A186 01-24-2012 BU A186 01-25-2012 EX A187 01-21-2012 EX A187 01-21-2012 EX A187 01-21-2012 NE A188 01-28-2012 BU A188 01-29-2012 BU A188 01-30-2012 BU A189 03-18-2012 BU A189 03-19-2012 BU A189 03-20-2012 EX A190 04-18-2012 EX A190 04-18-2012 BU A190 04-28-2012 BU OUTPUT: EMPLOYEE DATE PAY GROUP A186 01-23-2012 BU A190 04-18-2012 BU Quote
BostonBullodu Posted May 30, 2017 Report Posted May 30, 2017 Select Top 1 * FROM Tablename where PAY GROUP = 'BU' Quote
ranjith23 Posted May 30, 2017 Author Report Posted May 30, 2017 13 minutes ago, BostonBullodu said: Select Top 1 * FROM Tablename where PAY GROUP = 'BU' Highlight unnav ga. Just graduated a? Quote
BostonBullodu Posted May 30, 2017 Report Posted May 30, 2017 16 minutes ago, ranjith23 said: Highlight unnav ga. Just graduated a? ledu baa paina adi chadavaledu input output choosaa koncham ivaranga chepparade rek... Quote
Popkatapetapotapulti Posted May 30, 2017 Report Posted May 30, 2017 epudu table create chesi input chesi query rase opika ledu...idea cheptha try cheyi 1. Partition chesi ranks create cheyi over employee and date.. after this it will look like A186 01-21-2012 EX 1A186 01-22-2012 EX 2A186 01-23-2012 EX 3A186 01-23-2012 BU 3A186 01-24-2012 EX 4A186 01-24-2012 BU 4A186 01-25-2012 EX 5 2. get the top row with pay group 'BU' ..(at this point you will know the rank as well) 3. Now get the rank of a record whose value is 'EX' which is <= rank from step 2. thats it Quote
Uppi Posted May 30, 2017 Report Posted May 30, 2017 with data as(select employee,date,paygroup from table where paygroup='BU') select b.employee,b.date,b.paygroup from table a,data b where a.employee=b.employee and a.paygroup='EX'; Quote
ATRI Posted May 30, 2017 Report Posted May 30, 2017 1 hour ago, Popkatapetapotapulti said: epudu table create chesi input chesi query rase opika ledu...idea cheptha try cheyi 1. Partition chesi ranks create cheyi over employee and date.. after this it will look like A186 01-21-2012 EX 1A186 01-22-2012 EX 2A186 01-23-2012 EX 3A186 01-23-2012 BU 3A186 01-24-2012 EX 4A186 01-24-2012 BU 4A186 01-25-2012 EX 5 2. get the top row with pay group 'BU' ..(at this point you will know the rank as well) 3. Now get the rank of a record whose value is 'EX' which is <= rank from step 2. thats it if you take "<=" in third step, it will pull all the records, the rank for A186 BU is "3" and it retrieves 1, 2, 3 records since the value for all three records is 'EX' if i am wrong I tried with this, let me know if it works for you, it might be complex.. working on it to simplify CREATE TABLE TEST1 AS SELECT EMPID, HIREDATE, PAY_GROUP, CASE WHEN PAY_GROUP='EX' AND LEAD(PAY_GROUP,1) OVER(PARTITION BY EMPID ORDER BY HIREDATE)='BU' THEN 'Y' ELSE 'N' END AS SKIP_REC FROM EMP; SELECT * FROM TEST1 WHERE SKIP_REC ='Y' AND (EMPID, HIREDATE)=(SELECT EMPID, MIN(HIREDATE) FROM TEST1 WHERE SKIP_REC='Y'GROUP BY EMPID); Quote
SUbba LIngam Posted May 30, 2017 Report Posted May 30, 2017 SELECT EMPLOYEE, MIN(DATE), PAYGROUP FROM ( select a.EMPLOYEE, a.DATE, b.paygroup from table_1 a, table_1 b where a.paygroup = 'EX' and b.paygroup = 'BU' and a.EMPLOYEE=b.EMPLOYEE and a.DATE=b.DATE ) GROUP BY EMPLOYEE, PAYGROUP Quote
ranku_mogudu Posted May 30, 2017 Report Posted May 30, 2017 23 minutes ago, SUbba LIngam said: SELECT EMPLOYEE, MIN(DATE), PAYGROUP FROM ( select a.EMPLOYEE, a.DATE, b.paygroup from table_1 a, table_1 b where a.paygroup = 'EX' and b.paygroup = 'BU' and a.EMPLOYEE=b.EMPLOYEE and a.DATE=b.DATE ) GROUP BY EMPLOYEE, PAYGROUP +1 Quote
kranthi111 Posted May 30, 2017 Report Posted May 30, 2017 Create table #temp (Employee nvarchar(5), Date date, PayGroup nvarchar(2)) insert #temp values ('A186','01-21-2012','EX') insert #temp values ('A186','01-22-2012','EX') insert #temp values ('A186','01-23-2012','EX') insert #temp values ('A186','01-23-2012','BU') insert #temp values ('A186','01-24-2012','EX') insert #temp values ('A186','01-24-2012','BU') insert #temp values ('A186','01-25-2012','EX') insert #temp values ('A187','01-21-2012','EX') insert #temp values ('A187','01-21-2012','EX') insert #temp values ('A187','01-21-2012','NE') insert #temp values ('A188','01-28-2012','BU') insert #temp values ('A188','01-29-2012','BU') insert #temp values ('A188','01-30-2012','BU') insert #temp values ('A189','03-18-2012','BU') insert #temp values ('A189','03-19-2012','BU') insert #temp values ('A189','03-28-2012','EX') insert #temp values ('A190','04-18-2012','EX') insert #temp values ('A190','04-18-2012','BU') insert #temp values ('A190','04-28-2012','BU') SELECT * FROM #TEMP with CTE AS ( Select Employee ,Date ,PayGroup ,case when PayGroup in ('BU','EX') then 1 else 0 end as flag ,Rank() over( partition by Employee,PayGroup order by date) as n from #temp ), CTE1 as( select *,LAG(PayGroup) over(Partition by employee order by date,paygroup desc ) as PRVgroup from CTE where n = 1 and flag = 1) select * from CTE1 where paygroup = 'BU' and prvgroup = 'ex' Quote
BostonBullodu Posted May 30, 2017 Report Posted May 30, 2017 @kranthi111 @ATRI baa what if he is still using 2008 version where no Lead and Lag functions exist... Quote
ranku_mogudu Posted May 30, 2017 Report Posted May 30, 2017 14 minutes ago, BostonBullodu said: @kranthi111 @ATRI baa what if he is still using 2008 version where no Lead and Lag functions exist... vallaki telisina answers they told vuuu.. equivalent dhi TS vethukkovali. anni spoon feed chesthey repu ee doubt vochina DB lo post chesthadu..nerchukodu Quote
Popkatapetapotapulti Posted May 30, 2017 Report Posted May 30, 2017 2 hours ago, ATRI said: if you take "<=" in third step, it will pull all the records, the rank for A186 BU is "3" and it retrieves 1, 2, 3 records since the value for all three records is 'EX' if i am wrong I tried with this, let me know if it works for you, it might be complex.. working on it to simplify CREATE TABLE TEST1 AS SELECT EMPID, HIREDATE, PAY_GROUP, CASE WHEN PAY_GROUP='EX' AND LEAD(PAY_GROUP,1) OVER(PARTITION BY EMPID ORDER BY HIREDATE)='BU' THEN 'Y' ELSE 'N' END AS SKIP_REC FROM EMP; SELECT * FROM TEST1 WHERE SKIP_REC ='Y' AND (EMPID, HIREDATE)=(SELECT EMPID, MIN(HIREDATE) FROM TEST1 WHERE SKIP_REC='Y'GROUP BY EMPID); thats correct but its obvious that he should pull lowest rank ani Quote
crazymata Posted May 30, 2017 Report Posted May 30, 2017 1 hour ago, kranthi111 said: Create table #temp (Employee nvarchar(5), Date date, PayGroup nvarchar(2)) insert #temp values ('A186','01-21-2012','EX') insert #temp values ('A186','01-22-2012','EX') insert #temp values ('A186','01-23-2012','EX') insert #temp values ('A186','01-23-2012','BU') insert #temp values ('A186','01-24-2012','EX') insert #temp values ('A186','01-24-2012','BU') insert #temp values ('A186','01-25-2012','EX') insert #temp values ('A187','01-21-2012','EX') insert #temp values ('A187','01-21-2012','EX') insert #temp values ('A187','01-21-2012','NE') insert #temp values ('A188','01-28-2012','BU') insert #temp values ('A188','01-29-2012','BU') insert #temp values ('A188','01-30-2012','BU') insert #temp values ('A189','03-18-2012','BU') insert #temp values ('A189','03-19-2012','BU') insert #temp values ('A189','03-28-2012','EX') insert #temp values ('A190','04-18-2012','EX') insert #temp values ('A190','04-18-2012','BU') insert #temp values ('A190','04-28-2012','BU') SELECT * FROM #TEMP with CTE AS ( Select Employee ,Date ,PayGroup ,case when PayGroup in ('BU','EX') then 1 else 0 end as flag ,Rank() over( partition by Employee,PayGroup order by date) as n from #temp ), CTE1 as( select *,LAG(PayGroup) over(Partition by employee order by date,paygroup desc ) as PRVgroup from CTE where n = 1 and flag = 1) select * from CTE1 where paygroup = 'BU' and prvgroup = 'ex' lol ... solution Quote
ranku_mogudu Posted May 30, 2017 Report Posted May 30, 2017 13 minutes ago, crazymata said: lol ... solution uko baa.. he tried his best no..encourage these people rather than who cannot google. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.