SQL创建定价表
目前我有一个由价格组成的表格 列PriceList、ItemCode、折扣行、金额、价格 例如SQL创建定价表,sql,sql-server,Sql,Sql Server,目前我有一个由价格组成的表格 列PriceList、ItemCode、折扣行、金额、价格 例如 问题是,PriceList可以是PriceList(前面有*)或客户编号 我需要一行的结果 所以 为此,我使用以下代码: select Customer.CardCode ,Customer.CardName,SP.* from Customer inner join ( select ItemCode,CardCode, max(case when DPNum=0 then Amount e
问题是,PriceList可以是PriceList(前面有*)或客户编号 我需要一行的结果 所以 为此,我使用以下代码:
select Customer.CardCode ,Customer.CardName,SP.* from Customer inner join (
select ItemCode,CardCode,
max(case when DPNum=0 then Amount end) Amount1,
max(case when DPNum=0 then Price end) Price1,
max(case when DPNum=1 then Amount end) Amount2,
max(case when DPNum=1 then Price end) Price2,
max(case when DPNum=2 then Amount end) Amount3,
max(case when DPNum=2 then Price end) Price3,
max(case when DPNum=3 then Amount end) Amount4,
max(case when DPNum=3 then Price end) Price4,
max(case when DPNum=4 then Amount end) Amount5,
max(case when DPNum=4 then Price end) Price5,
max(case when DPNum=5 then Amount end) Amount6,
max(case when DPNum=5 then Price end) Price6,
max(case when DPNum=6 then Amount end) Amount7,
max(case when DPNum=6 then Price end) Price7,
max(case when DPNum=7 then Amount end) Amount8,
max(case when DPNum=7 then Price end) Price8,
max(case when DPNum=8 then Amount end) Amount9,
max(case when DPNum=8 then Price end) Price9,
max(case when DPNum=9 then Amount end) Amount10,
max(case when DPNum=9 then Price end) Price10
from SPP2 group by ItemCode,CardCode
) sp
on sp.CardCode = Customer.CardCode or sp.CardCode = '*'+cast(ListNum as varchar(1))
where Customer.Cardcode='1234'
order by Customer.CardCode,ItemCode,sp.CardCode DESC
这只适用于一个小细节;
如果客户有价格表(所有客户都有),并且他们有“特殊”价格;我每件商品有两行
因此,我的结果是:
CUSTOMER NAME ITEM PLCODE AMOUNT0 PRICE0 AMOUNT1 PRICE1
1234 DUMMY ITEM1 1234 100 0.45 200 0.40
1234 DUMMY ITEM1 *1 100 0.50 200 0.45
如果客户有特殊价格,则只应退还此价格。
有没有办法在一个查询中实现这一点?(或者可能有一种更有效的方法来完成我想要完成的任务)您可以使用
行数()
为每个客户获得一行。以下是想法:
select csp.*
from (select c.*, sp.*,
row_number() over (partition by customer
order by (case when plcode like '*_' then 1 else 2 end)
) as seqnum
from customer c join
(select ItemCode, CardCode,
max(case when DPNum=0 then Amount end) Amount1,
max(case when DPNum=0 then Price end) Price1,
. . .
from SPP2
group by ItemCode, CardCode
) sp
on sp.CardCode = Customer.CardCode or sp.CardCode = '*'+cast(ListNum as varchar(1))
where c.Cardcode = '1234'
) csp
where seqnum = 1;
“问题是,PriceList可以是PriceList(前面有*)或客户编号。”-是的,这确实是一个设计错误。是的,你是对的。。不幸的是,SAP的好人犯了这个设计错误。是的,当您以错误的方式设计表时,您会遇到这种低效的查询。请务必告诉SAP B1设计团队:)tt通过一些小的调整,我让它正常工作了,谢谢Gordon Linoff@tt。这可能是丑陋的(这是一个意见问题),但它可能不是特别低效。聚合时窗口函数会有开销,但可能比聚合本身小得多。
CUSTOMER NAME ITEM PLCODE AMOUNT0 PRICE0 AMOUNT1 PRICE1
1234 DUMMY ITEM1 1234 100 0.45 200 0.40
1234 DUMMY ITEM1 *1 100 0.50 200 0.45
select csp.*
from (select c.*, sp.*,
row_number() over (partition by customer
order by (case when plcode like '*_' then 1 else 2 end)
) as seqnum
from customer c join
(select ItemCode, CardCode,
max(case when DPNum=0 then Amount end) Amount1,
max(case when DPNum=0 then Price end) Price1,
. . .
from SPP2
group by ItemCode, CardCode
) sp
on sp.CardCode = Customer.CardCode or sp.CardCode = '*'+cast(ListNum as varchar(1))
where c.Cardcode = '1234'
) csp
where seqnum = 1;