可能是一个措辞糟糕的问题,所以很抱歉。
我有3张表,我想要连接在一起。
我已经创建了一个SQLFiddle here
我想要的是,将linkloads表中的MAXLINEDISCOUNT与price_escalation_bands表中允许的折扣进行比较。
因此,在数据中,40的maxlinediscount必须match price_escalation_bands表中的下一个最高折扣,其中customer_band相同。
因此,我希望结果与第1行匹配,其中它是铜色的,折扣是45。如果我的MAXLINEDISCOUNT大于45,则转到下一个最高值,在本例中可能是50。
如果匹配,则返回fk_salesman_userid字段,并将其与users表中的用户名匹配。
显然,所有这些数据都是动态的,所以需要查看下一个最高...
目前,它是作为空白返回的,所以我的语法不太正确。
我的问题是:
select price_authorized,load_number,maxlinediscount,customer_band,[price_escalation_bands].fk_salesman_userid,Users.firstname firstname,totalcost,period,creditlimit,currentbalance,customername,totalcubes,treatedcubes,normalcubes,pricingissue from #linkloads
left outer JOIN [price_escalation_bands] on [price_escalation_bands].band=#linkloads.customer_band
AND price_escalation_bands.discount = (
SELECT top 1 [price_escalation_bands].[discount]
FROM [price_escalation_bands]
WHERE [price_escalation_bands].band=#linkloads.customer_band
AND [price_escalation_bands].[discount]<=#linkloads.maxlinediscount
ORDER BY [price_escalation_bands].[discount]
)
left outer join Users
on Users.userid=[price_escalation_bands].fk_salesman_userid帮帮忙,一如既往的感激。
发布于 2013-07-01 21:57:49
这将列出linkloads中超过匹配限制的所有price_escalation_bands条目
select u.username
, peb.band
, peb.discount
, ll.maxlinediscount
from price_escalation_bands peb
join Users u
on peb.fk_salesman_userid = u.UserID
join linkloads ll
on ll.customer_band = peb.band
where ll.maxlinediscount < peb.discountYour SQL Fiddle example with this query.
https://stackoverflow.com/questions/17406080
复制相似问题