0

嗨,我有以下工作查询(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

这会产生结果

在此处输入图像描述

在这个查询中,我已经加入了如下所示的表price_escalation_bands

在此处输入图像描述

我想要做的是将maxlinediscount我的查询与discount表的列进行比较,price_escalation_bands并返回表中下一个最大记录的用户 ID (fk_salesman_userid)。所以折扣 11 将转到折扣 15 并返回fk_salesman_userid9。如果折扣为 18,它将转到 100 并返回fk_salesman_userid21。

我基本上是想弄清楚fk_salesman_userid可以批准折扣的maxlinediscount

所以期望的输出是:

在此处输入图像描述

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

提前致谢

4

2 回答 2

1

在加入 price_escalation_bands 时,您可以使用另外一种条件进行折扣。检查小提琴。在这里,我尝试将结果放在临时表中。您可以join price_escalation_bands通过传递max(linediscount). 希望这会帮助你。

于 2013-06-18T13:43:41.517 回答
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]
)
;
于 2013-06-19T12:45:54.123 回答