简体   繁体   中英

SQL View - Fetching Unit Price, Default Price & Customer Price

I am hoping someone can help me with this SQL view.

I have a screen where you can add products to an order. This order is for a customer which may or may not have a special price for the item.

Essentially, I am after 3 figures:

  • The unit price for the product as input by the operator
  • The default price for the product (standard ratecard)
  • The special price for the client for this product (may not exist)

To simplify things, I have the following tables

OrderLine

  • OrderId
  • ProductId
  • ProductVariationId (can be null)

Ratecard

  • RatecardId
  • RatecardName

RatecardClient (Default master ratecard is ID 1)

  • RatecardClientId
  • RatecardId
  • ClientId

RatecardProduct

  • RatecardProductId
  • ProductId
  • ProductVariationId (can be null)

A product has an ID but can also have a product variation ID

I want to create a view for OrderLine which has the input price, the default ratecard price and the special price for that product.

I would like a view which gives me:

Product Id UnitPrice From "OrderLine" RatecardPrice From "RatecardProduct" relating to "Ratecard" with RatecardId = 1 (will always exist)

ClientRatecardPrice (Depends if there is an existing ratecard and entry in this ratecard for the client / product / variation )

I'm really hoping someone can get me started here as I'm really struggling!

Thank you in advance!

I have made some assumptions about where the price columns are, and also how the RateCardProduct joins to RateCard. It would be better if you included the actual table definitions with extra columns stripped out. But with the assumptions you can see in my table defs, this query should give you what you want.

create table ClientOrder(OrderID int, ClientID int)
create table OrderLine(OrderId int,ProductId int,ProductVariationId int, UnitPrice decimal(10,2))
create table Ratecard(RateCardId int,RatecardName varchar(50)) -- has default 1
create table RateCardClient(RatecardClientId int,RatecardId int,ClientId int) 
create table RateCardProduct(RatecardProductId int, RateCardId int, ProductId int, ProductVariationId int, Price decimal(10,2))

select Clientorder.ClientID, ClientOrder.OrderID, OrderLine.ProductID, OrderLine.ProductVariationId,
    OrderLine.UnitPrice as UnitPrice,
    P1.Price as RateCardPrice,
    P2.Price as ClientRateCardPrice
from ClientOrder
Join OrderLine on OrderLine.OrderID=ClientOrder.OrderID
-- Get the default price
join RateCardProduct P1 on P1.RateCardID=1 
    and P1.ProductId=OrderLine.ProductId
    and isnull(P1.ProductVariationID,0)=isnull(OrderLine.ProductVariationID,0)
-- Get the customer specific price
left join RateCardClient on RateCardClient.ClientID=ClientOrder.ClientID
left join RateCardProduct P2 on P2.RateCardID=RateCardClient.RateCardID 
    and P2.ProductId=OrderLine.ProductId
    and isnull(P2.ProductVariationID,0)=isnull(OrderLine.ProductVariationID,0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM