Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Results dependant on effective date

Author  Topic 

n82109
Starting Member

1 Post

Posted - 2011-06-02 : 10:40:00
There are two tables involved SaleRep and RevenuePayers :-

RevenuePayers : All the financial data comes from this table
SalesRep : has the list of sales people along with the client ids that they handle.

Now there are client ids wherein a salesrep has changed. So there are effective dates from which the salesrep has changed, for eg

ClientID CompanyId EffectiveDate SalesRep
1056 1 8/15/2009 Brad
1056 1 4/25/2011 John

I have created a report that fetches the financial data for a particular month (based on date filter selected between: startDate and endDate).
So when you enquire payments for the month before April-2011 it shows business for Brad, which is correct. But even if you enquire about May-2011 or later months
The business is shown against Brad which is not correct. So I understand that I need to take into account the effective dates in the SalesRep table.

I reworked on my query and got results. But it works correctly only in case of May-2011 onwards. For months before that it now shows me ‘John’ as the SalesRep which is incorrect.

My query is :

SELECT
ClientSalesReps."ClientID" ,
SalesReps."Name" AS SalesRep,
Client."State" AS State,
Month(AccessionIndex."DOS"||'-'||Year(AccessionIndex."DOS") as Period,
RevenuePayers."Billed" AS Billed,
RevenuePayers."Paid" AS Paid
FROM
"AR"."ClientSalesReps" ClientSalesReps INNER JOIN "AR"."SalesReps" SalesReps ON ClientSalesReps."CompanyId" = SalesReps."CompanyId"
AND SalesReps."SalesId" = ClientSalesReps."SalesRep"
INNER JOIN "AR"."RevenuePayers" RevenuePayers ON ClientSalesReps."CompanyId" = RevenuePayers."CompanyId"
AND ClientSalesReps."ClientID" = RevenuePayers."ClientId"
INNER JOIN "AR"."AccessionIndex" AccessionIndex ON RevenuePayers."ClientId" = AccessionIndex."ClientID"
AND RevenuePayers."Accession" = AccessionIndex."Accession"
AND AccessionIndex."CompanyId" = RevenuePayers."CompanyId"
INNER JOIN "AR"."Client" Client ON AccessionIndex."ClientID" = Client."ClientId"
AND AccessionIndex."CompanyId" = Client."CompanyId"
Where (AccessionIndex."DOS") between '2011-05-01' and '2011-05-31' and
RevenuePayers."CompanyId" = 1 and ClientSalesReps."ClientID" = 1056
and ClientSalesReps."EffectiveDate" in
(select max(ClientSalesReps."EffectiveDate")
from "AR"."ClientSalesReps" ClientSalesReps where ClientSalesReps."ClientID" = 1056
group by ClientSalesReps."ClientID")
Group by ClientSalesReps."ClientID"

How do I tie up the effective dates with the startDate/endDate period? Can anyone suggest something?

Ifor
Aged Yak Warrior

700 Posts

Posted - 2011-06-02 : 11:39:12
You need to get the start and end dates for each SalesRep.
Assuming the data you have shown is from ClientSalesReps, which is far from clear, then do something like the following:

;WITH RepClientOrder
AS
(
SELECT CompanyId, ClientId, EffectiveDate, SalesRep
,ROW_NUMBER() OVER (PARTITION BY CompanyId, ClientID ORDER BY EffectiveDate) AS RowNum
FROM ClientSalesReps
)
, RepClient
AS
(
SELECT R1.CompanyId, R1.ClientId, R1.SalesRep
,R1.EffectiveDate AS StartDate
,COALESCE(R2.EffectiveDate, CAST('99991231' AS datetime)) AS EndDate
FROM RepClientOrder R1
LEFT JOIN RepClientOrder R2
ON R1.CompanyId = R2.CompanyId
AND R1.ClientId = R2.ClientId
AND R1.RowNum = R2.RowNum - 1
)
-- select * from RepClientOrder order by CompanyId, ClientId, RowNum
-- select * from RepClient order by CompanyId, ClientId, StartDate
SELECT *
FROM RevenuePayers RP
JOIN AccessionIndex AI
ON RP.CompanyId = AI.CompanyId
AND RP.ClientId = AI.ClientId
AND RP.Accession = AI.Accession
JOIN RepClient RC
ON RP.CompanyId = RC.CompanyId
AND RP.ClientId = RC.ClientId
AND AI.DOS >= RC.StartDate
AND AI.DOS < RC.EndDate
--etc

Go to Top of Page
   

- Advertisement -