mssql查询加入根据折扣百分比计算出esclation人

时间:2013-06-18 12:42:27

标签: sql-server join case

您好,我有以下工作查询(mssql)

select 
max(load_number) load_number,max(linediscount) maxlinediscount,max(CustomerBand) customer_band

  from [Linked_Order_lines] 
  join [Customer_Order_Summary]
  on [Customer_Order_Summary].[CustomerOrderID]=[Linked_Order_lines].[load_number]
  join [Customers]
  on Customers.CustomerName=Customer_Order_Summary.CustomerName
  join Customer_Order_lines
  on Customer_Order_Summary.CustomerOrderID=Customer_Order_lines.CustomerOrderID
  join price_escalation_bands
  on price_escalation_bands.band=Customer_Order_Summary.CustomerBand

  where [linked_order_id] in 
  (
    select [linkedorderid] from 
    [Linked_Order_Summary] join [Linked_Order_lines] on
    [Linked_Order_Summary].[linkedorderid] = [Linked_Order_lines].[linked_order_id]
    where [load_number]='7'
  ) 
  and Customer_Order_lines.linestatus='current'

    group by load_number

这会产生结果

enter image description here

在此查询中,我已加入表price_escalation_bands,如下所示:

enter image description here

我想要做的是将我的查询中的maxlinediscount与表discount的{​​{1}}列进行比较,并返回表中记录的用户ID(fk_salesman_userid)这是下一个最伟大的。因此折扣11将转到折扣15并返回price_escalation_bands 9.如果折扣是18,它将转到100并返回fk_salesman_userid 21。

我基本上试图找出可以批准fk_salesman_userid

折扣的fk_salesman_userid

所以期望的输出是:

enter image description here

我是否需要case语句?如果需要,我如何将其与现有select语句中的max语句一起使用?

提前感谢离开

2 个答案:

答案 0 :(得分:1)

在加入price_escalation_bands时,您可以再使用一个条件进行折扣。 检查FIDDLE。在这里,我尝试在临时表中使用结果。您可以通过join price_escalation_bandsmax(linediscount)一起尝试使用相同的子查询。 希望这会对你有所帮助。

答案 1 :(得分:0)

最终工作语法:

IF OBJECT_ID('tempdb..#linkloads') IS NOT NULL
BEGIN
DROP TABLE #linkloads
END

CREATE TABLE #linkloads
(
maxlinediscount [decimal](10,3),
customer_band [varchar](50),
load_number [int],
totalcost [decimal](10,3),
period [datetime],
CreditLimit[decimal](10,3),
CurrentBalance[decimal](10,3),
CustomerName [varchar](100),
TotalCubes[decimal](10,3),
TreatedCubes[decimal](10,3),
NormalCubes[decimal](10,3),
PricingIssue[bit]
);



insert into #linkloads

select max(linediscount) maxlinediscount,max(CustomerBand) customer_band,max(load_number) load_number,max(totalcost) totalcost,max(Customer_Order_Summary.PeriodStart) period,max(Customers.CreditLimit) CreditLimit ,max(Customers.CurrentBalance)CurrentBalance,max(Customer_Order_Summary.CustomerName)CustomerName,max(TotalCubes) TotalCubes,max(TreatedCubes)TreatedCubes ,max(TotalCubes-TreatedCubes) as NormalCubes,sum(case when pricingissue=1 THEN 1 ELSE 0 END) pricingissue

  from [Linked_Order_lines] 
  join [Customer_Order_Summary]
  on [Customer_Order_Summary].[CustomerOrderID]=[Linked_Order_lines].[load_number]
  join [Customers]
  on Customers.CustomerName=Customer_Order_Summary.CustomerName
  join Customer_Order_lines
  on Customer_Order_Summary.CustomerOrderID=Customer_Order_lines.CustomerOrderID
   where [linked_order_id] in 
  (
    select [linkedorderid] from 
    [Linked_Order_Summary] join [Linked_Order_lines] on
    [Linked_Order_Summary].[linkedorderid] = [Linked_Order_lines].[linked_order_id]
    where [load_number]='10'
  ) 
  and Customer_Order_lines.linestatus='current'
     group by load_number;

select * from #linkloads;

select load_number,maxlinediscount,customer_band,[Salesman_Discounts_per_band].fk_salesman_userid,totalcost,period,creditlimit,currentbalance,customername,totalcubes,treatedcubes,normalcubes,pricingissue from #linkloads
left outer JOIN [Salesman_Discounts_per_band] on [Salesman_Discounts_per_band].band=#linkloads.customer_band
AND [Salesman_Discounts_per_band].[discount_allowed] = (
  SELECT top 1 [Salesman_Discounts_per_band].[discount_allowed]
  FROM [Salesman_Discounts_per_band]
  WHERE [Salesman_Discounts_per_band].band=#linkloads.customer_band
  AND [Salesman_Discounts_per_band].[discount_allowed]<=#linkloads.maxlinediscount
  ORDER BY [Salesman_Discounts_per_band].[discount_allowed]
)
;