嗨,我有以下工作查询(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_userid
9。如果折扣为 18,它将转到 100 并返回fk_salesman_userid
21。
我基本上是想弄清楚fk_salesman_userid
可以批准折扣的maxlinediscount
所以期望的输出是:
我是否需要 case 语句,如果需要,如何将它与现有 select 语句中的 max 语句一起使用?
提前致谢