toksoladuti
IS-IT--Management
I have the following query which brings back special prices set for customers, however there are sometimes multiple special prices on the same product for the same customer. What I'd like to do is change the query so that it only returns the row with the latest expiry date (spr.validto), but I haven't managed to get anywhere with this so I could do with some help please. Here is the current query:
select
c.name as [Customer],
c.customercode as [Customer Code],
p.productcode as [Product Code],
p.description as [Product],
(spr.price * (1-(spr.discount1/100)) * (1-(spr.discount2/100))) as [Special Price],
spr.price as [Calculation Price] ,
spr.discount1 as [Calculation Discount 1],
spr.discount2 as [Calculation Discount 2],
p.standardbuyprice as [Buy Price],
sr.name as [Sales Rep],
b.name as [Branch Name],
spr.validto as [Expiry Date],
spr.datetimelastused [Last Used Date],
spr.usagecount as [Times Used]
from sellpricerule spr
left outer join customer c
on spr.customerid=c.customerid
left outer join product p
on spr.productid=p.productid
left outer join branch b
on c.homebranchid=b.branchid
left outer join salesrep sr
on c.salesrepid=sr.salesrepid
where spr.customerid IS NOT NULL
order by spr.validto, b.name, c.name
Many thanks.
select
c.name as [Customer],
c.customercode as [Customer Code],
p.productcode as [Product Code],
p.description as [Product],
(spr.price * (1-(spr.discount1/100)) * (1-(spr.discount2/100))) as [Special Price],
spr.price as [Calculation Price] ,
spr.discount1 as [Calculation Discount 1],
spr.discount2 as [Calculation Discount 2],
p.standardbuyprice as [Buy Price],
sr.name as [Sales Rep],
b.name as [Branch Name],
spr.validto as [Expiry Date],
spr.datetimelastused [Last Used Date],
spr.usagecount as [Times Used]
from sellpricerule spr
left outer join customer c
on spr.customerid=c.customerid
left outer join product p
on spr.productid=p.productid
left outer join branch b
on c.homebranchid=b.branchid
left outer join salesrep sr
on c.salesrepid=sr.salesrepid
where spr.customerid IS NOT NULL
order by spr.validto, b.name, c.name
Many thanks.