Getting wrong result in SQL Server - sql-server
I have two tables:
Fee_Payable_to_Students:
f_co |S_Adm_No | apr | may | june | jul | aug | sep | oct | nov | dec | jan | feb | mar
1 |s_1 | 5 | 5 | 5 | 5 | 5 | 5 | 5 | 5 | 5 | 5 | 5 | 5
2 |s_1 | 5 | 5 | 5 | 5 | 5 | 5 | 5 | 5 | 5 | 5 | 5 | 5
Fee_Assign_Waiver_to_Students:
f_co|S_Adm_No | apr | may | june | jul | aug | sep | oct | nov | dec | jan | feb | mar
1 |s_1 | 5 | 5 | 5 | 5 | 5 | 5 | 5 | 5 | 5 | 5 | 5 | 5
I want to view my result as
S_Adm_No | Installment | Amount |Payable_Date
s_1 |Quarter-1 (April, May & June) | 5 |Apr 15, 2018
s_1 |Quarter-2 (July, August & September) | 5 |Jul 15, 2018
s_1 |Quarter-3 (October, November & December) | 5 |Oct 15, 2018
s_1 |Quarter-4 (January, February & March) | 5 |Jan 15, 2019
My SQL query is here:
SELECT
unPvt.S_Adm_No, Installment,
SUM(Amount) AS Amount,
CASE
WHEN Installment = 'Quarter-1 (April, May & June)'
THEN 'Apr 15, 2018'
WHEN Installment = 'Quarter-2 (July, August & September)'
THEN 'Jul 15, 2018'
WHEN Installment = 'Quarter-3 (October, November & December)'
THEN 'Oct 15, 2018'
WHEN Installment = 'Quarter-4 (January, February & March)'
THEN 'Jan 15, 2019'
END AS Payable_Date
FROM
(SELECT
pc.S_Adm_No,
(Apr + May + Jun)-COALESCE(CON.Qa1,0) AS [Quarter-1 (April, May & June)],
(Jul + Aug + Sep)-COALESCE(CON.Qa2,0) AS [Quarter-2 (July, August & September)],
(Oct + Nov + Dec)-COALESCE(CON.Qa3,0) AS [Quarter-3 (October, November & December)],
(Jan + Feb + Mar)-COALESCE(CON.Qa4,0) AS [Quarter-4 (January, February & March)]
FROM
Fee_Payable_to_Students pc
LEFT JOIN
(SELECT
S_Adm_no,
SUM(E_Apr + E_May + E_Jun) Qa1,
SUM(E_Jul + E_Aug + E_Sep) Qa2,
SUM(E_Oct + E_Nov + E_Dec) Qa3,
SUM(E_Jan + E_Feb + E_Mar) Qa4
FROM
Fee_Assign_Waiver_to_Students w
GROUP BY
S_Adm_No) AS CON ON pc.S_Adm_no = CON.S_Adm_no
WHERE
pc.S_Adm_No = s_1) AS Pvt
UNPIVOT
(Amount FOR Installment IN
([Quarter-1 (April, May & June)],
[Quarter-2 (July, August & September)],
[Quarter-3 (October, November & December)],
[Quarter-4 (January, February & March)])) AS unPvt
GROUP BY unPvt.S_Adm_No,unPvt.Installment
I guess your problem is that you have a many to one relationship which when joined results in a many to many relationship so all your installments calculate out as zero. You have partly solved the problem by grouping Fee_Assign_Waiver_to_Students and you just need to apply the same thinking to Fee_Payable_to_Students. I would for clarity use a cte to do this bit. Given
drop table Fee_Payable_to_Students
go
drop table Fee_Assign_Waiver_to_Students
go
create table Fee_Payable_to_Students
(
f_co int,S_Adm_No varchar(10), apr int, may int, jun int, jul int, aug int, sep int, oct int, nov int, dec int, jan int, feb int, mar int
)
go
create table Fee_Assign_Waiver_to_Students
(
f_co int,S_Adm_No varchar(10), apr int, may int, jun int, jul int, aug int, sep int, oct int, nov int, dec int, jan int, feb int, mar int
)
go
insert into Fee_Payable_to_Students values
(1 ,'s_1' , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5),
(2 ,'s_1' , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5),
(3 ,'s_2' , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5),
(4 ,'s_3' , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5),
(5 ,'s_3' , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5)
insert into Fee_Assign_Waiver_to_Students values
(1 ,'s_1' , null , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5),
(2 ,'s_3' , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5)
This code
;with cte as(
select pc.s_adm_no,
coalesce(pa1,0) - coalesce(qa1,0) AS [Quarter-1 (April, May & June)],
coalesce(pa2,0) - coalesce(qa2,0) AS [Quarter-2 (July, August & September)],
coalesce(pa3,0) - coalesce(qa3,0) AS [Quarter-3 (October, November & December)],
coalesce(pa4,0) - coalesce(qa4,0) AS [Quarter-4 (January, February & March)]
from
(
SELECT
p.S_Adm_no,
SUM(coalesce(Apr,0) + coalesce(May,0) + coalesce(Jun,0)) pa1,
SUM(coalesce(Jul,0) + coalesce(Aug,0) + coalesce(Sep,0)) pa2,
SUM(coalesce(Oct,0) + coalesce(Nov,0) + coalesce(Dec,0)) pa3,
SUM(coalesce(Jan,0) + coalesce(Feb,0) + coalesce(Mar,0)) pa4
FROM
Fee_Payable_to_Students p
GROUP BY
S_Adm_No
) pc
left join
(SELECT
S_Adm_no,
SUM(coalesce(Apr,0) + coalesce(May,0) + coalesce(Jun,0)) Qa1,
SUM(coalesce(Jul,0) + coalesce(Aug,0) + coalesce(Sep,0)) Qa2,
SUM(coalesce(Oct,0) + coalesce(Nov,0) + coalesce(Dec,0)) Qa3,
SUM(coalesce(Jan,0) + coalesce(Feb,0) + coalesce(Mar,0)) Qa4
FROM
Fee_Assign_Waiver_to_Students w
GROUP BY
S_Adm_No) AS CON ON pc.S_Adm_no = CON.S_Adm_no
)
select unpvt.s_adm_no,unpvt.installment,unpvt.amount,
CASE
WHEN Installment = 'Quarter-1 (April, May & June)'
THEN 'Apr 15, 2018'
WHEN Installment = 'Quarter-2 (July, August & September)'
THEN 'Jul 15, 2018'
WHEN Installment = 'Quarter-3 (October, November & December)'
THEN 'Oct 15, 2018'
WHEN Installment = 'Quarter-4 (January, February & March)'
THEN 'Jan 15, 2019'
END AS Payable_Date
from
(select * from cte) as pvt
unpivot (amount for installment in ([Quarter-1 (April, May & June)],
[Quarter-2 (July, August & September)],
[Quarter-3 (October, November & December)],
[Quarter-4 (January, February & March)])) AS unPvt
Result
s_adm_no installment amount Payable_Date
---------- ---------------------------------------- ----------- ------------
s_1 Quarter-1 (April, May & June) 20 Apr 15, 2018
s_1 Quarter-2 (July, August & September) 15 Jul 15, 2018
s_1 Quarter-3 (October, November & Decem 15 Oct 15, 2018
s_1 Quarter-4 (January, February & March 15 Jan 15, 2019
s_2 Quarter-1 (April, May & June) 15 Apr 15, 2018
s_2 Quarter-2 (July, August & September) 15 Jul 15, 2018
s_2 Quarter-3 (October, November & Decem 15 Oct 15, 2018
s_2 Quarter-4 (January, February & March 15 Jan 15, 2019
s_3 Quarter-1 (April, May & June) 15 Apr 15, 2018
s_3 Quarter-2 (July, August & September) 15 Jul 15, 2018
s_3 Quarter-3 (October, November & Decem 15 Oct 15, 2018
s_3 Quarter-4 (January, February & March 15 Jan 15, 2019
Related
Oracle - Cycle detected while executing recursive 'WITH' query
I'm doing a basic example of recursive query with oracle sql. I'm computing future months of the format MON-YY. I managed to have a seemingly correct query but I don't understand the break condition with a WITH query. I'm trying to break on the year value (for example stop when you reach 2020), but it detects a cycle while doing that. If I break on the month value (e.g. December), it works. Here's my query with a month based break: with prochains_mois(mois, annee) as ( select 'sep' as mois, 19 as annee from dual union all select case mois when 'jan' then 'fev' when 'fev' then 'mar' when 'mar' then 'avr' when 'avr' then 'mai' when 'mai' then 'jun' when 'jun' then 'jui' when 'jui' then 'aou' when 'aou' then 'sep' when 'sep' then 'oct' when 'oct' then 'nov' when 'nov' then 'dec' when 'dec' then 'jan' end, case mois when 'dec' then annee + 1 else annee end from prochains_mois r where mois <> 'dec' ) select * from prochains_mois; If I do this, it returns a consistent result. MOI ANNEE --- ---------- sep 19 oct 19 nov 19 dec 19 Now if I try to break the recursive query on the year, let's say 2020, so I change the where condition in the with clause to : where annee < 20 Then I get : ORA-32044: cycle detected while executing recursive WITH query I tried to break with a later month to see if my year addition works correctly, it seems to be the case. If I break on march, I get the January and February correctly : where mois <> 'mar' gives MOI ANNEE --- ---------- sep 19 oct 19 nov 19 dec 19 jan 20 fev 20 mar 20
Use DATEs: with prochains_mois( value ) as ( select DATE '2019-09-01' from dual union all select ADD_MONTHS( value, 1 ) FROM prochains_mois WHERE value < DATE '2020-12-01' ) select SUBSTR( TO_CHAR( value, 'mon', 'NLS_DATE_LANGUAGE=FRENCH' ), 1, 3 ) AS mois, TO_CHAR( value, 'RR' ) AS annee from prochains_mois; Output: MOIS | ANNEE :--- | :---- sep | 19 oct | 19 nov | 19 dec | 19 jan | 20 fev | 20 mar | 20 avr | 20 mai | 20 jui | 20 jui | 20 aou | 20 sep | 20 oct | 20 nov | 20 dec | 20 or use your query and check that the month and year do not match: with prochains_mois(mois, annee) as ( select 'sep' as mois, 19 as annee from dual union all select case mois when 'jan' then 'fev' when 'fev' then 'mar' when 'mar' then 'avr' when 'avr' then 'mai' when 'mai' then 'jun' when 'jun' then 'jui' when 'jui' then 'aou' when 'aou' then 'sep' when 'sep' then 'oct' when 'oct' then 'nov' when 'nov' then 'dec' when 'dec' then 'jan' end, case mois when 'dec' then annee + 1 else annee end from prochains_mois r where ( mois, annee ) NOT IN ( ( 'dec', 20 ) ) ) select * from prochains_mois; Output: MOIS | ANNEE :--- | ----: sep | 19 oct | 19 nov | 19 dec | 19 jan | 20 fev | 20 mar | 20 avr | 20 mai | 20 jun | 20 jui | 20 aou | 20 sep | 20 oct | 20 nov | 20 dec | 20 db<>fiddle here
Your main issue is that you're trying to manipulate dates using strings/numbers. Don't do that; if you're working with dates, use dates! E.g. you can do what you're after like so: WITH prochains_mois (mnth_dt) AS (SELECT TRUNC(sysdate, 'mm') mnth_dt FROM dual UNION ALL SELECT add_months(mnth_dt, 1) mnth_dt FROM prochains_mois WHERE add_months(mnth_dt, 1) < add_months(TRUNC(sysdate, 'yyyy'), 12)) SELECT mnth_dt, to_char(mnth_dt, 'mon') mois, to_char(mnth_dt, 'yy') annee FROM prochains_mois; MNTH_DT MOIS ANNEE ----------- ---- ----- 01/09/2019 sep 19 01/10/2019 oct 19 01/11/2019 nov 19 01/12/2019 dec 19 N.B. You could simplify the predicate in the recursive sub-factored query to mnth_dt < add_months(TRUNC(SYSDATE, 'yyyy'), 11). This works by taking the start date (here, I've used sysdate) and finding the first of the month (by using the optional second parameter of TRUNC to specify the level we're truncating it to). Then we simply add a month to each date until we hit the last month of the start date's year. Only after you've got the dates do you then output the data in the format you require using to_char.
Month start from user define
enter image description here The requirements says that each month starts from the 22nd and end on next month 21st. our company Month start from 22nd each month and last day of month is 21st so i need to calculation regard compnay months for example date output 22nd Oct 2017 to 21st Nov 2017 = Nov 2017 22nd Nov 2017 to 21st Dec 2017 = Dec 2017 22nd Dec 2017 to 21st Jan 2018 = Jan 2018 22nd Jan 2018 to 21st Feb 2018 = Feb 2018 so when i run the query so it will look on dates and give me month name with year
Changed my answer as per your comments. Try the code yourself here: http://sqlfiddle.com/#!6/4b3e5/5 declare #MyTable table (MyDate datetime) insert into #MyTable (MyDate) values ('20171022'), ('20171121'), ('20171122'), ('20171221'), ('20171222'), ('20180121'), ('20180221') select t.MyDate, case when datepart(day, t.MyDate) > 21 then datename(month, dateadd(month, 1, t.MyDate)) + ' ' + datename(year, dateadd(month, 1, t.MyDate)) else datename(month, t.MyDate) + ' ' + datename(year, t.MyDate) end from #MyTable t the result now is MyDate | COLUMN1 ------------|-------------- 22/10/2017 | November 2017 21/11/2017 | November 2017 22/11/2017 | December 2017 21/12/2017 | December 2017 22/12/2017 | January 2018 21/01/2018 | January 2018 21/02/2018 | February 2018 If you only need the first 3 chars from month, use the substring to get them like this select t.MyDate, case when datepart(day, t.MyDate) > 21 then substring(datename(month, dateadd(month, 1, t.MyDate)), 1, 3) + ' ' + datename(year, dateadd(month, 1, t.MyDate)) else substring(datename(month, t.MyDate), 1, 3) + ' ' + datename(year, t.MyDate) end from #MyTable t Now the result is MyDate | COLUMN1 ------------|-------------- 22/10/2017 | Nov 2017 21/11/2017 | Nov 2017 22/11/2017 | Dec 2017 21/12/2017 | Dec 2017 22/12/2017 | Jan 2018 21/01/2018 | Jan 2018 21/02/2018 | Feb 2018
T-SQL: combine current year and month from month column
I am trying to combine current year in the form of YYYY with the monthy from the month column. monthy monthsname ------------------------ 1 January 2 February 3 March 4 April 5 May 6 June 7 July 8 August 9 September 10 October 11 November 12 December Here is the output I am getting: monthy monthsname month_number ------------------------------------ 5 May NULL 6 June NULL 8 August NULL 9 September NULL 10 October NULL 11 November NULL 1 January 201701 2 February 201702 3 March 201703 4 April 201704 7 July 201707 12 December 201712 Instead of month_number being null, I was trying to do this: isnull(ss.month_number, cast(convert(varchar(4), DATEPART(year, getdate())) as int) + right('0-- ' + cast(convert(varchar(2), monthy) as int), 2)) I am not getting the right output: I want something like 201705 for the first row record for month_number
Depending on what your datatype is, you might want to cast this but it's simply the year * 100 plus the month number to get the integer representation of the number you are trying to create. ISNULL(month_number,YEAR(GETDATE())*100+monthy)
Can you try this: SELECT CONVERT(VARCHAR(4),DATEPART(YEAR, GETDATE())) + --year ( REPLICATE('0', 2 - LEN(CONVERT(VARCHAR(2), monthy)) ) + CONVERT(VARCHAR(2), monthy) --monthy )
How to subtract one table to another table in sqlserver
I have one table Fee_Payable_to_Students. To organize values of this table I have coded as create PROC [dbo].[Fee_Fee_Demand](#S_Adm_No NVARCHAR(50)) AS BEGIN SELECT cls_SecId, S_Adm_No, Installment, SUM(Amount) AS Amount, CASE WHEN Installment = 'Quarter-1 (April, May & June)' THEN 'Apr 15, 2017' WHEN Installment = 'Quarter-2 (July, August & September)' THEN 'Jul 15, 2017' WHEN Installment = 'Quarter-3 (October, November & December)' THEN 'Oct 15, 2017' WHEN Installment = 'Quarter-4 (January, February & March)' THEN 'Jan 15, 2018' END AS Payable_Date FROM ( SELECT cls_SecId, S_Adm_No, Apr + May + Jun AS [Quarter-1 (April, May & June)], Jul + Aug + Sep AS [Quarter-2 (July, August & September)], Oct + Nov + Dec AS [Quarter-3 (October, November & December)], Jan + Feb + Mar AS [Quarter-4 (January, February & March)] FROM Fee_Payable_to_Students where S_Adm_No=#S_Adm_No) AS Pvt UNPIVOT (Amount FOR Installment IN ([Quarter-1 (April, May & June)], [Quarter-2 (July, August & September)], [Quarter-3 (October, November & December)], [Quarter-4 (January, February & March)])) AS unPvt GROUP BY cls_SecId, S_Adm_No, Installment end the value displays like Installment | Amount | Payable_Date Quarter-1 (April, May & June) | | Apr 15, 2017 Quarter-2 (July, August & September) | | Jul 15, 2017 Quarter-3 (October, November & December)| | Oct 15, 2017 Quarter-4 (January, February & March) | | Jan 15, 2018 Besides this One more table is there in name of Fee_Receipt. Now My question is if Fee_receipt has values like S_Amn_No |Fhead | Apr | May |Jun | Jul | Aug | Sep | Oct | Nov | Dec | Jan | Feb | Mar 1001 |1 | 100 | 100 |100 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 1001 |2 | 100 | 100 |100 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 1001 |3 | 100 | 100 |100 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 Then the value should show like Installment | Amount | Status | Payable_Date Quarter-1 (April, May & June) | assumed(900) | FeePaid | Apr 15, 2017 Quarter-2 (July, August & September) | | Pending | Jul 15, 2017 Quarter-3 (October, November & December)| | Pending | Oct 15, 2017 Quarter-4 (January, February & March) | | Pending | Jan 15, 2018 if Fee_receipt has value like S_Amn_No |Fhead | Apr | May |Jun | Jul | Aug | Sep | Oct | Nov | Dec | Jan | Feb | Mar 1001 |1 | 100 | 100 |100 | 100 | 100 |100 | 0 | 0 | 0 | 0 | 0 | 0 1001 |2 | 100 | 100 |100 | 100 | 100 | 0 | 0 | 0 | 0 | 0 | 0 | 0 1001 |3 | 100 | 100 |100 | 100 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 Then the value should show like Installment | Amount | Status | Payable_Date Quarter-1 (April, May & June) | assumed(900) | Paid | Apr 15, 2017 Quarter-2 (July, August & September) | assumed(1200) | 600 | Jul 15, 2017 Quarter-3 (October, November & December)| assumed(1200) | 1200 | Oct 15, 2017 Quarter-4 (January, February & March) | assumed(1200) | 1200 | Jan 15, 2018 like wise for two more quarter.
sql server - help in query
I have a table called employee_salary_master in which we keep salary effective dates and entry dates of each employee in the company. For processing salary of an employee for a month, we need to fetch most recently entered record. If the effective date is less than that month then we pick the most recent entry. but if effective date is greater than the first date of the month, then there will be more than one effective dates for that month example: the data for an employee is as below. SAL_MATSER_ID EMPLOYEE_ID EFFECTIVE_DATE ENTRY_DATE ------------- ----------- -------------- ------------ 1 5814 Jan 6 2006 Jan 12 2006 2 5814 Jan 10 2006 Jul 17 2006 3 5814 Jan 20 2006 Dec 22 2006 4 5814 May 10 2007 Jul 18 2007 5 5814 Nov 1 2007 Dec 18 2007 6 5814 Aug 1 2008 Aug 20 2008 7 5814 May 1 2008 Sep 2 2008 8 5814 Sep 1 2009 Sep 18 2008 9 5814 Nov 1 2008 Apr 20 2009 10 5814 Nov 10 2009 Nov 25 2009 11 5814 Nov 5 2009 Nov 26 2009 If i need to get the record for Nov 2009, I write the query below select EMPLOYEE_SALARY_MASTER_ID, EMPLOYEE_ID, EFFECTIVE_DATE, ENTRY_DATE, ARREAR_PROCESS_FLAG from employee_salary_master esm where employee_id = 5814 and (esm.entry_date = (select max(entry_date) from employee_salary_master where employee_id = 5814 and effective_date <= #monthfirstdate) or (esm.effective_date between #monthfirstdate and #monthlastdate)) which gives the result below.. SAL_MATSER_ID EMPLOYEE_ID EFFECTIVE_DATE ENTRY_DATE ------------- ----------- -------------- ------------ 9 5814 Nov 1 2008 Apr 20 2009 10 5814 Nov 10 2009 Nov 25 2009 11 5814 Nov 5 2009 Nov 26 2009 What I need is as follows... For Nov 1 - Nov 4, salary should be processed as per employee_salary_masterId - 9 and Nov 5 - Nov 30, salary should be processed as per employee_salary_masterId - 11. SAL_MATSER_ID EMPLOYEE_ID EFFECTIVE_DATE ENTRY_DATE ------------- ----------- -------------- ------------ 9 5814 Nov 1 2008 Apr 20 2009 11 5814 Nov 5 2009 Nov 26 2009 Please help me build this query.
Not quite sure if I understand you completely correct, but have a look at this example DECLARE #employee_salary_master TABLE( EMPLOYEE_SALARY_MASTER_ID INT, EMPLOYEE_ID INT, EFFECTIVE_DATE DATETIME, ENTRY_DATE DATETIME ) INSERT INTO #employee_salary_master SELECT 1,5814,'Jan 6 2006','Jan 12 2006' INSERT INTO #employee_salary_master SELECT 2,5814,'Jan 10 2006','Jul 17 2006' INSERT INTO #employee_salary_master SELECT 3,5814,'Jan 20 2006','Dec 22 2006' INSERT INTO #employee_salary_master SELECT 4,5814,'May 10 2007','Jul 18 2007' INSERT INTO #employee_salary_master SELECT 5,5814,'Nov 1 2007','Dec 18 2007' INSERT INTO #employee_salary_master SELECT 6,5814,'Aug 1 2008','Aug 20 2008' INSERT INTO #employee_salary_master SELECT 7,5814,'May 1 2008','Sep 2 2008' INSERT INTO #employee_salary_master SELECT 8,5814,'Sep 1 2009','Sep 18 2008' INSERT INTO #employee_salary_master SELECT 9,5814,'Nov 1 2008','Apr 20 2009' INSERT INTO #employee_salary_master SELECT 10,5814,'Nov 10 2009','Nov 25 2009' INSERT INTO #employee_salary_master SELECT 11,5814,'Nov 5 2009','Nov 26 2009' DECLARE #monthfirstdate DATETIME, #monthlastdate DATETIME SELECT #monthfirstdate = '01 Nov 2009', #monthlastdate = '30 Nov 2009' SELECt * FROM ( SELECT TOP 1 * FROM #employee_salary_master esm WHERE esm.EFFECTIVE_DATE BETWEEN #monthfirstdate and #monthlastdate AND esm.EFFECTIVE_DATE < esm.ENTRY_DATE ORDER BY esm.ENTRY_DATE DESC ) sub UNION ALL SELECT * FROM ( SELECT TOP 1 * FROM #employee_salary_master esm WHERE esm.EFFECTIVE_DATE <= #monthfirstdate AND esm.EFFECTIVE_DATE < esm.ENTRY_DATE ORDER BY esm.EFFECTIVE_DATE DESC ) sub ORDER BY EFFECTIVE_DATE
The rule for excluding nov 10 record is that we need to filter out those records for the month of November in which entry_date is greater but effective_date is smaller. Let me explain you wrt data provided: As a rule, the latest/last entry will take precedence over the previous entries in a particular month which implies that the entrty date of Nov 26 2009 would take precedence over the entry date of Nov 25 2009. Now since effective date of SAL_MATSER_ID - 10 is greater than that of SAL_MATSER_ID - 11, hence Nov 10 record would be nullified. Had the data been like the below, all the 3 records would have been used for salary processing. SAL_MATSER_ID - 9 for salary of Nov 1 - Nov 9 SAL_MATSER_ID - 10 for salary of Nov 10 - Nov 14 SAL_MATSER_ID - 11 for salary of Nov 15 - Nov 30 SAL_MATSER_ID EMPLOYEE_ID EFFECTIVE_DATE ENTRY_DATE ------------- ----------- -------------- ------------ 9 5814 Nov 1 2008 Apr 20 2009 10 5814 Nov 10 2009 Nov 25 2009 11 5814 Nov 15 2009 Nov 26 2009 But since SAL_MATSER_ID - 11 is applicable from 5th Nov. onwards, the previous record is nullified. I hope this explains the situation. Thanks for your support, Sweta