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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 query help

Author  Topic 

krish.vak
Starting Member

1 Post

Posted - 2012-01-26 : 02:35:02
i have 2 tables invoice , repcomm which has the following structure

invoice
invno invdate rep gtotal
001 06/01/2011 kumar 5000
002 08/01/2011 raj 2500
003 05/05/2011 kumar 3000


repcom
rep rdate commPer
kumar 01/01/2011 10
kumar 01/05/2011 15
raj 01/01/2011 5
raj 01/05/2011 10



the "repcom" table contains the percentage of commission which will be raised or lowered during the representative's service.

while calculating the rep commission for a particular invoice i need to use a query that fetch the "commPer" from the table "repcom" with respect to the invdate in "invoice" table



krish

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-01-26 : 09:04:01
quote:
Originally posted by krish.vak

i have 2 tables invoice , repcomm which has the following structure

invoice
invno invdate rep gtotal
001 06/01/2011 kumar 5000
002 08/01/2011 raj 2500
003 05/05/2011 kumar 3000


repcom
rep rdate commPer
kumar 01/01/2011 10
kumar 01/05/2011 15
raj 01/01/2011 5
raj 01/05/2011 10



the "repcom" table contains the percentage of commission which will be raised or lowered during the representative's service.

while calculating the rep commission for a particular invoice i need to use a query that fetch the "commPer" from the table "repcom" with respect to the invdate in "invoice" table



krish



SELECT i.*,r.commPer
FROM invoice i
CROSS APPLY (SELECT TOP 1 commPer
FROM repcom
WHERE rep = i.rep
AND rdate<=invdate
ORDER BY rdate DESC)r


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -