sql - Finding Customers Last Price Paid -
i'm trying find way customers last price paid code have @ moment is:
select product.productcode, count(product.productcode) [qty baught], product.description, customer.customercode, customer.name, max(orderheader.datetimecreated) [date], orderline.unitsellpriceincurrency sell customer inner join orderheader on customer.customerid = orderheader.customerid inner join orderline on orderheader.orderid = orderline.orderid inner join product on orderline.productid = product.productid group product.description, product.productcode, customer.customercode, customer.name, orderline.unitsellpriceincurrency having (product.productcode = 'bcem002') , (customer.customercode = '1000') order max(orderheader.datetimecreated) desc this code shows every time price changed want see last price, datecreated , price paid (unitsellpriceincurrency) on different tables.
is there way group (unitsellpriceincurrency) (datecreated) or alternative way of doing it.
i'm new @ if there's obvious way of doing sorry.
edit: i'm getting @ moment new code, of prices i'm getting unrelated products

what want last price paid showing in way can change customer , product i'm searching for.
one option might use sub-select utilizing top specify want retrieve 1 record, , make sure "latest" using order by:
select product.productcode, count(product.productcode) [qty baught], product.description, customer.customercode, customer.name, max(orderheader.datetimecreated) [date], (select top 1 o.unitsellpriceincurrency orderline o inner join orderheader oh on o.orderid = oh.orderid oh.customerid = customer.customerid , o.productid = product.productid order oh.datetimecreated desc) latestprice customer inner join orderheader on customer.customerid = orderheader.customerid inner join orderline on orderheader.orderid = orderline.orderid inner join product on orderline.productid = product.productid (customer.customercode = '1000') , (product.productcode = 'bcem002') group product.description, product.productcode, product.productid, customer.customercode, customer.name, customer.customerid order [date] desc in example latestprice contain last inserted unitsellpriceincurrency each customer , product. i'm not sure if query makes sense data (why last price) it's how interpreted request. query still return orderheaders though.
Comments
Post a Comment