Sql server mssql查询连接以根据折扣百分比计算折扣人员
嗨,我有下面的工作查询msqlSql server mssql查询连接以根据折扣百分比计算折扣人员,sql-server,join,case,Sql Server,Join,Case,嗨,我有下面的工作查询msql 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].
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与表price\u escalation\u bands的折扣列进行比较,并返回表中下一个最大记录的用户id fk\u Salesian\u userid。因此,折扣11将转到折扣15,并返回fk_销售人员_用户ID 9。如果折扣是18,它将变为100,并返回fk_Sales_userid 21
我基本上是想计算出可以在maxlinediscount中批准折扣的fk_推销员_用户ID
因此,期望的输出是:
我是否需要case语句?如果需要,如何将其与现有select语句中的max语句一起使用
提前感谢,因为当您加入价格升级时,您可以使用另一个折扣条件。 检查一下。在这里,我在临时表中尝试了结果。通过传递maxlinediscount,您可以对加入价格\升级\带尝试相同的子查询。 希望这将对您有所帮助。最终工作语法:
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]
)
;
感谢Pradeeshnarayan,非常感谢。最后,如果没有临时表,我无法完成这项工作。