【问题标题】:mssql query join to work out esclation person based on discounts percentagemysql查询加入根据折扣百分比计算升级人
【发布时间】:2013-06-18 12:42:27
【问题描述】:

您好,我有以下工作查询(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 与表price_escalation_bandsdiscount 列进行比较,并返回表中下一个最大记录的用户ID (fk_salesman_userid)。所以折扣 11 将转到折扣 15 并返回 fk_salesman_userid 9。如果折扣为 18,它将转到 100 并返回 fk_salesman_userid 21。

我基本上是在尝试找出可以批准maxlinediscount 中的折扣的fk_salesman_userid

所以想要的输出是:

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

提前致谢

【问题讨论】:

    标签: sql-server join case


    【解决方案1】:

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

    【讨论】:

    • 感谢 Pradeeshnarayan,非常感谢。最后,如果没有临时表,我无法完成这项工作。
    【解决方案2】:

    最终的工作语法:

    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]
    )
    ;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-20
      • 2022-01-23
      • 1970-01-01
      • 1970-01-01
      • 2014-04-06
      • 2012-05-01
      相关资源
      最近更新 更多