SELECT ls_inv_back.cli_no,
client.cli_name,
sum( case when LS_INV_BACK.sell_price<=0.0 or LS_INV_BACK.sell_price is null or LS_INV_BACK.retail_price<=0 or LS_INV_BACK.retail_price is null
then 0.0
case when left(product.prod_no,1)='3' and lower(product.monad)='kg' and (isnull(LS_INV_BACK.sell_price,0) - isnull(LS_INV_BACK.retail_price,0))>286 and (isnull(LS_INV_BACK.sell_price,0) - isnull(LS_INV_BACK.retail_price,0))<=1000
then ls_inv_back.inv_num *100*convert(numeric(12,3),right(rtrim(ltrim(convert(varchar(50),(Square(CEILING(((isnull(LS_INV_BACK.sell_price,0) - isnull(LS_INV_BACK.retail_price,0))/100- 0.01)/0.5)+1)+10000)))),4))
case when left(product.prod_no,1)='3' and lower(product.monad)='kg' and (isnull(LS_INV_BACK.sell_price,0) - isnull(LS_INV_BACK.retail_price,0))>1000 and (isnull(LS_INV_BACK.sell_price,0) - isnull(LS_INV_BACK.retail_price,0))<=2000
then ls_inv_back.inv_num * 100*convert(numeric(12,3), right(rtrim(ltrim(convert(varchar(50),(Square(CEILING((isnull(LS_INV_BACK.sell_price,0) - isnull(LS_INV_BACK.retail_price,0))/100- 10.01)+21)+10000)))),4))
case when (isnull(LS_INV_BACK.sell_price,0) - isnull(LS_INV_BACK.retail_price,0))=0
then 0.0
case when (isnull(LS_INV_BACK.sell_price,0) - isnull(LS_INV_BACK.retail_price,0))>0.1 and (isnull(LS_INV_BACK.sell_price,0) - isnull(LS_INV_BACK.retail_price,0))<=10
then ls_inv_back.inv_num *convert(numeric(12,3),right(rtrim(ltrim(convert(char(50),Square(CEILING(((isnull(LS_INV_BACK.sell_price,0) - isnull(LS_INV_BACK.retail_price,0) ) - 0.01)/0.5)+1)+10000))),4))
case when (isnull(LS_INV_BACK.sell_price,0) - isnull(LS_INV_BACK.retail_price,0))>10 and (isnull(LS_INV_BACK.sell_price,0) - isnull(LS_INV_BACK.retail_price,0))<=20
then ls_inv_back.inv_num *convert(numeric(12,3),right(rtrim(ltrim(convert(char(50),Square(CEILING((isnull(LS_INV_BACK.sell_price,0) - isnull(LS_INV_BACK.retail_price,0) ) - 10.01)+21)+10000))),4))
case when (isnull(LS_INV_BACK.sell_price,0) - isnull(LS_INV_BACK.retail_price,0))>20 and (isnull(LS_INV_BACK.sell_price,0) - isnull(LS_INV_BACK.retail_price,0))<=50
then ls_inv_back.inv_num *convert(numeric(12,3),right(rtrim(ltrim(convert(char(50),(Square(CEILING((isnull(LS_INV_BACK.sell_price,0) - isnull(LS_INV_BACK.retail_price,0) ) - 20.01)/2)+31)+10000)))),4))
case when (isnull(LS_INV_BACK.sell_price,0) - isnull(LS_INV_BACK.retail_price,0))>50 and (isnull(LS_INV_BACK.sell_price,0) - isnull(LS_INV_BACK.retail_price,0))<=100
then ls_inv_back.inv_num *convert(numeric(12,3),right(rtrim(ltrim(convert(char(50),(Square(CEILING((isnull(LS_INV_BACK.sell_price,0) - isnull(LS_INV_BACK.retail_price,0) ) - 50.01)/3)+46)+10000)))),4))
case when (isnull(LS_INV_BACK.sell_price,0) - isnull(LS_INV_BACK.retail_price,0))>100 and (isnull(LS_INV_BACK.sell_price,0) - isnull(LS_INV_BACK.retail_price,0))<=286
then ls_inv_back.inv_num *convert(numeric(12,3),right(rtrim(ltrim(convert(char(50),(Square(CEILING((isnull(LS_INV_BACK.sell_price,0) - isnull(LS_INV_BACK.retail_price,0) ) - 101.01)/5)+63)+10000)))),4))
case when (isnull(LS_INV_BACK.sell_price,0) - isnull(LS_INV_BACK.retail_price,0))>286 and (isnull(LS_INV_BACK.sell_price,0) - isnull(LS_INV_BACK.retail_price,0))<=1366
then ls_inv_back.inv_num *convert(numeric(12,3),right(rtrim(ltrim(convert(char(50),(Square(CEILING((isnull(LS_INV_BACK.sell_price,0) - isnull(LS_INV_BACK.retail_price,0) ) - 101.01)/5)+63)+100000)))),5))
case when (isnull(LS_INV_BACK.sell_price,0) - isnull(LS_INV_BACK.retail_price,0))> 1366 and (isnull(LS_INV_BACK.sell_price,0) - isnull(LS_INV_BACK.retail_price,0))<=2000
then ls_inv_back.inv_num *convert(numeric(12,3),right(rtrim(ltrim(convert(char(50),(Square(CEILING((isnull(LS_INV_BACK.sell_price,0) - isnull(LS_INV_BACK.retail_price,0) ) - 101.01)/5)+63)+1000000)))),6))
else ls_inv_back.inv_num *isnull(LS_INV_BACK.sell_price,0) - isnull(LS_INV_BACK.retail_price,0)*60 end)
FROM ls_inv_back,
client,
product
WHERE ( ls_inv_back.cli_no = client.cli_no ) and
( ls_inv_back.prod_no = product.prod_no )
group by ls_inv_back.cli_no,
client.cli_name
错误是:
MSG 156,等级15,状态1,第5行关键字&#39; case&#39;
附近的语法不正确
在第二种情况下语法有错误,但我无法弄清楚我做错了什么 提前谢谢
答案 0 :(得分:2)
这就像我可以将其格式化为更清晰的东西一样接近。我转换为ANSI-92样式的连接语法。我还简化了几件。这些计算是一项严峻的挑战。那里有很多功能。当然,这可以变得更简单,但我无法在没有认真努力的情况下解开那里发生的所有事情。这绝不是一个完美的解决方案,但至少它更清洁,语法现在至少是有效的。
SELECT ib.cli_no
, c.cli_name
, sum
(
case
when isnull(ib.sell_price, 0) <= 0.0
or isnull(ib.retail_price, 0) <= 0
or (isnull(ib.sell_price,0) - isnull(ib.retail_price,0)) = 0
then 0.0
when left(p.prod_no, 1) + lower(p.monad) = '3kg'
and (isnull(ib.sell_price, 0) - isnull(ib.retail_price, 0)) > 286
and (isnull(ib.sell_price, 0) - isnull(ib.retail_price, 0)) <= 1000
then ib.inv_num * 100 * convert(numeric(12,3), right(rtrim(ltrim(convert(varchar(50),(Square(CEILING(((isnull(ib.sell_price,0) - isnull(ib.retail_price,0))/100- 0.01)/0.5)+1)+10000)))),4))
when left(p.prod_no,1)='3' and lower(p.monad)='kg' and (isnull(ib.sell_price,0) - isnull(ib.retail_price,0))>1000 and (isnull(ib.sell_price,0) - isnull(ib.retail_price,0))<=2000
then ib.inv_num * 100*convert(numeric(12,3), right(rtrim(ltrim(convert(varchar(50),(Square(CEILING((isnull(ib.sell_price,0) - isnull(ib.retail_price,0))/100- 10.01)+21)+10000)))),4))
when (isnull(ib.sell_price,0) - isnull(ib.retail_price,0)) > 0.1
and (isnull(ib.sell_price,0) - isnull(ib.retail_price,0)) <= 10
then ib.inv_num * convert(numeric(12,3),right(rtrim(ltrim(convert(char(50),Square(CEILING(((isnull(ib.sell_price,0) - isnull(ib.retail_price,0) ) - 0.01)/0.5)+1)+10000))),4))
when (isnull(ib.sell_price,0) - isnull(ib.retail_price,0))>10 and (isnull(ib.sell_price,0) - isnull(ib.retail_price,0))<=20
then ib.inv_num *convert(numeric(12,3),right(rtrim(ltrim(convert(char(50),Square(CEILING((isnull(ib.sell_price,0) - isnull(ib.retail_price,0) ) - 10.01)+21)+10000))),4))
when (isnull(ib.sell_price,0) - isnull(ib.retail_price,0))>20 and (isnull(ib.sell_price,0) - isnull(ib.retail_price,0))<=50
then ib.inv_num *convert(numeric(12,3),right(rtrim(ltrim(convert(char(50),(Square(CEILING((isnull(ib.sell_price,0) - isnull(ib.retail_price,0) ) - 20.01)/2)+31)+10000))),4))
when (isnull(ib.sell_price,0) - isnull(ib.retail_price,0))>50 and (isnull(ib.sell_price,0) - isnull(ib.retail_price,0))<=100
then ib.inv_num *convert(numeric(12,3),right(rtrim(ltrim(convert(char(50),(Square(CEILING((isnull(ib.sell_price,0) - isnull(ib.retail_price,0) ) - 50.01)/3)+46)+10000))),4))
when (isnull(ib.sell_price,0) - isnull(ib.retail_price,0))>100 and (isnull(ib.sell_price,0) - isnull(ib.retail_price,0))<=286
then ib.inv_num *convert(numeric(12,3),right(rtrim(ltrim(convert(char(50),(Square(CEILING((isnull(ib.sell_price,0) - isnull(ib.retail_price,0) ) - 101.01)/5)+63)+10000))),4))
when (isnull(ib.sell_price,0) - isnull(ib.retail_price,0))>286 and (isnull(ib.sell_price,0) - isnull(ib.retail_price,0))<=1366
then ib.inv_num *convert(numeric(12,3),right(rtrim(ltrim(convert(char(50),(Square(CEILING((isnull(ib.sell_price,0) - isnull(ib.retail_price,0) ) - 101.01)/5)+63)+100000))),5))
when (isnull(ib.sell_price,0) - isnull(ib.retail_price,0))> 1366 and (isnull(ib.sell_price,0) - isnull(ib.retail_price,0))<=2000
then ib.inv_num *convert(numeric(12,3),right(rtrim(ltrim(convert(char(50),(Square(CEILING((isnull(ib.sell_price,0) - isnull(ib.retail_price,0) ) - 101.01)/5)+63)+1000000))),6))
else ib.inv_num *isnull(ib.sell_price,0) - isnull(ib.retail_price,0)*60
end
)
FROM ls_inv_back ib
join client c on ib.cli_no = c.cli_no
join product p on ib.prod_no = p.prod_no
group by ib.cli_no
, c.cli_name