免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 8638 | 回复: 1
打印 上一主题 下一主题

求助 sql 定义变量 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-12-18 10:35 |只看该作者 |倒序浏览
下面代码里的颜色部分希望设置成一个变量替代:
SELECT scd.标记1,scd.生产单号,cpbm.分类,scd.产品编码,scd.描述,scd.材料,scd.颜色,scd.下单数量,scd.生产分厂,scd.单价,scd.总金额 as 产值,
cpgzde.配料单价,
cpgzde.配料单价*case when cpbm.分类 =\'C\' or cpbm.分类 =\'C2\' then
        case when scd.下单数量<=40 then 2
                 when scd.下单数量>40 and scd.下单数量<=60 then 80
                 when scd.下单数量>60 and scd.下单数量<=80 then 1.3
                 when scd.下单数量>80 and scd.下单数量<=104 then 104
                 when scd.下单数量>104 and scd.下单数量<=200 then 1
                 when scd.下单数量>200 and scd.下单数量<=1000 then 200
                 when scd.下单数量>1000  then 0.95 end
else
        case when scd.下单数量<=10 then 2
                 when scd.下单数量>10 and scd.下单数量<=15 then 20
                 when scd.下单数量>15 and scd.下单数量<=20 then 1.3
                 when scd.下单数量>20 and scd.下单数量<=26 then 26
                 when scd.下单数量>26 and scd.下单数量<=50 then 1
                 when scd.下单数量>50 and scd.下单数量<=55 then 50
                 when scd.下单数量>55 and scd.下单数量<=85 then 0.9
                 when scd.下单数量>85 and scd.下单数量<=90 then 77
                 when scd.下单数量>90 and scd.下单数量<=150 then 0.85
                 when scd.下单数量>150 and scd.下单数量<=160 then 128
                 when scd.下单数量>160  then 0.8 end
end  
as 配料工资,
cpgzde.机加单价,
cpgzde.机加单价*case when cpbm.分类 =\'C\' or cpbm.分类 =\'C2\' then
        case when scd.下单数量<=40 then 2
                 when scd.下单数量>40 and scd.下单数量<=60 then 80
                 when scd.下单数量>60 and scd.下单数量<=80 then 1.3
                 when scd.下单数量>80 and scd.下单数量<=104 then 104
                 when scd.下单数量>104 and scd.下单数量<=200 then 1
                 when scd.下单数量>200 and scd.下单数量<=1000 then 200
                 when scd.下单数量>1000  then 0.95 end
else
        case when scd.下单数量<=10 then 2
                 when scd.下单数量>10 and scd.下单数量<=15 then 20
                 when scd.下单数量>15 and scd.下单数量<=20 then 1.3
                 when scd.下单数量>20 and scd.下单数量<=26 then 26
                 when scd.下单数量>26 and scd.下单数量<=50 then 1
                 when scd.下单数量>50 and scd.下单数量<=55 then 50
                 when scd.下单数量>55 and scd.下单数量<=85 then 0.9
                 when scd.下单数量>85 and scd.下单数量<=90 then 77
                 when scd.下单数量>90 and scd.下单数量<=150 then 0.85
                 when scd.下单数量>150 and scd.下单数量<=160 then 128
                 when scd.下单数量>160  then 0.8 end
end  
as 机加工资,
cpgzde.砂光单价,
cpgzde.砂光单价*case when cpbm.分类 =\'C\' or cpbm.分类 =\'C2\' then
        case when scd.下单数量<=40 then 2
                 when scd.下单数量>40 and scd.下单数量<=60 then 80
                 when scd.下单数量>60 and scd.下单数量<=80 then 1.3
                 when scd.下单数量>80 and scd.下单数量<=104 then 104
                 when scd.下单数量>104 and scd.下单数量<=200 then 1
                 when scd.下单数量>200 and scd.下单数量<=1000 then 200
                 when scd.下单数量>1000  then 0.95 end
else
        case when scd.下单数量<=10 then 2
                 when scd.下单数量>10 and scd.下单数量<=15 then 20
                 when scd.下单数量>15 and scd.下单数量<=20 then 1.3
                 when scd.下单数量>20 and scd.下单数量<=26 then 26
                 when scd.下单数量>26 and scd.下单数量<=50 then 1
                 when scd.下单数量>50 and scd.下单数量<=55 then 50
                 when scd.下单数量>55 and scd.下单数量<=85 then 0.9
                 when scd.下单数量>85 and scd.下单数量<=90 then 77
                 when scd.下单数量>90 and scd.下单数量<=150 then 0.85
                 when scd.下单数量>150 and scd.下单数量<=160 then 128
                 when scd.下单数量>160  then 0.8 end
end  
as 砂光工资,
cpgzde.组装单价,
cpgzde.组装单价*case when cpbm.分类 =\'C\' or cpbm.分类 =\'C2\' then
        case when scd.下单数量<=40 then 2
                 when scd.下单数量>40 and scd.下单数量<=60 then 80
                 when scd.下单数量>60 and scd.下单数量<=80 then 1.3
                 when scd.下单数量>80 and scd.下单数量<=104 then 104
                 when scd.下单数量>104 and scd.下单数量<=200 then 1
                 when scd.下单数量>200 and scd.下单数量<=1000 then 200
                 when scd.下单数量>1000  then 0.95 end
else
        case when scd.下单数量<=10 then 2
                 when scd.下单数量>10 and scd.下单数量<=15 then 20
                 when scd.下单数量>15 and scd.下单数量<=20 then 1.3
                 when scd.下单数量>20 and scd.下单数量<=26 then 26
                 when scd.下单数量>26 and scd.下单数量<=50 then 1
                 when scd.下单数量>50 and scd.下单数量<=55 then 50
                 when scd.下单数量>55 and scd.下单数量<=85 then 0.9
                 when scd.下单数量>85 and scd.下单数量<=90 then 77
                 when scd.下单数量>90 and scd.下单数量<=150 then 0.85
                 when scd.下单数量>150 and scd.下单数量<=160 then 128
                 when scd.下单数量>160  then 0.8 end
end  
as 组装工资,
cpgzde.油漆单价,case when scd.生产分厂 = \'N2\' then 1.5 when scd.生产分厂 = \'N3\' and cpbm.分类 = \'T\' then 1.5 else 1 end as 油漆系数,
cpgzde.油漆单价*case when cpbm.分类 =\'C\' or cpbm.分类 =\'C2\' then
        case when scd.下单数量<=40 then 2
                 when scd.下单数量>40 and scd.下单数量<=60 then 80
                 when scd.下单数量>60 and scd.下单数量<=80 then 1.3
                 when scd.下单数量>80 and scd.下单数量<=104 then 104
                 when scd.下单数量>104 and scd.下单数量<=200 then 1
                 when scd.下单数量>200 and scd.下单数量<=1000 then 200
                 when scd.下单数量>1000  then 0.95 end
else
        case when scd.下单数量<=10 then 2
                 when scd.下单数量>10 and scd.下单数量<=15 then 20
                 when scd.下单数量>15 and scd.下单数量<=20 then 1.3
                 when scd.下单数量>20 and scd.下单数量<=26 then 26
                 when scd.下单数量>26 and scd.下单数量<=50 then 1
                 when scd.下单数量>50 and scd.下单数量<=55 then 50
                 when scd.下单数量>55 and scd.下单数量<=85 then 0.9
                 when scd.下单数量>85 and scd.下单数量<=90 then 77
                 when scd.下单数量>90 and scd.下单数量<=150 then 0.85
                 when scd.下单数量>150 and scd.下单数量<=160 then 128
                 when scd.下单数量>160  then 0.8 end
end
*
case when scd.生产分厂 = \'N2\' then 1.5 when scd.生产分厂 = \'N3\' and cpbm.分类 = \'T\' then 1.5 else 1 end
as 油漆工资,
cpgzde.装配单价,
cpgzde.装配单价*case when cpbm.分类 =\'C\' or cpbm.分类 =\'C2\' then
        case when scd.下单数量<=40 then 2
                 when scd.下单数量>40 and scd.下单数量<=60 then 80
                 when scd.下单数量>60 and scd.下单数量<=80 then 1.3
                 when scd.下单数量>80 and scd.下单数量<=104 then 104
                 when scd.下单数量>104 and scd.下单数量<=200 then 1
                 when scd.下单数量>200 and scd.下单数量<=1000 then 200
                 when scd.下单数量>1000  then 0.95 end
else
        case when scd.下单数量<=10 then 2
                 when scd.下单数量>10 and scd.下单数量<=15 then 20
                 when scd.下单数量>15 and scd.下单数量<=20 then 1.3
                 when scd.下单数量>20 and scd.下单数量<=26 then 26
                 when scd.下单数量>26 and scd.下单数量<=50 then 1
                 when scd.下单数量>50 and scd.下单数量<=55 then 50
                 when scd.下单数量>55 and scd.下单数量<=85 then 0.9
                 when scd.下单数量>85 and scd.下单数量<=90 then 77
                 when scd.下单数量>90 and scd.下单数量<=150 then 0.85
                 when scd.下单数量>150 and scd.下单数量<=160 then 128
                 when scd.下单数量>160  then 0.8 end
end
as 装配工资,
cpgzde.包装单价,
cpgzde.包装单价*case when cpbm.分类 =\'C\' or cpbm.分类 =\'C2\' then
        case when scd.下单数量<=40 then 2
                 when scd.下单数量>40 and scd.下单数量<=60 then 80
                 when scd.下单数量>60 and scd.下单数量<=80 then 1.3
                 when scd.下单数量>80 and scd.下单数量<=104 then 104
                 when scd.下单数量>104 and scd.下单数量<=200 then 1
                 when scd.下单数量>200 and scd.下单数量<=1000 then 200
                 when scd.下单数量>1000  then 0.95 end
else
        case when scd.下单数量<=10 then 2
                 when scd.下单数量>10 and scd.下单数量<=15 then 20
                 when scd.下单数量>15 and scd.下单数量<=20 then 1.3
                 when scd.下单数量>20 and scd.下单数量<=26 then 26
                 when scd.下单数量>26 and scd.下单数量<=50 then 1
                 when scd.下单数量>50 and scd.下单数量<=55 then 50
                 when scd.下单数量>55 and scd.下单数量<=85 then 0.9
                 when scd.下单数量>85 and scd.下单数量<=90 then 77
                 when scd.下单数量>90 and scd.下单数量<=150 then 0.85
                 when scd.下单数量>150 and scd.下单数量<=160 then 128
                 when scd.下单数量>160  then 0.8 end
end
as 包装工资,
case when cpbm.分类 =\'C\' or cpbm.分类 =\'C2\' then
        case when scd.下单数量<=40 then 2
                 when scd.下单数量>40 and scd.下单数量<=60 then 80
                 when scd.下单数量>60 and scd.下单数量<=80 then 1.3
                 when scd.下单数量>80 and scd.下单数量<=104 then 104
                 when scd.下单数量>104 and scd.下单数量<=200 then 1
                 when scd.下单数量>200 and scd.下单数量<=1000 then 200
                 when scd.下单数量>1000  then 0.95 end
else
        case when scd.下单数量<=10 then 2
                 when scd.下单数量>10 and scd.下单数量<=15 then 20
                 when scd.下单数量>15 and scd.下单数量<=20 then 1.3
                 when scd.下单数量>20 and scd.下单数量<=26 then 26
                 when scd.下单数量>26 and scd.下单数量<=50 then 1
                 when scd.下单数量>50 and scd.下单数量<=55 then 50
                 when scd.下单数量>55 and scd.下单数量<=85 then 0.9
                 when scd.下单数量>85 and scd.下单数量<=90 then 77
                 when scd.下单数量>90 and scd.下单数量<=150 then 0.85
                 when scd.下单数量>150 and scd.下单数量<=160 then 128
                 when scd.下单数量>160  then 0.8 end
end  as 批量系数

FROM scd,cpgzde,cpbm
WHERE cpbm.产品编码 = scd.产品编码
and scd.产品编码 = cpgzde.产品编码

order by scd.生产单号,scd.顺号

论坛徽章:
0
2 [报告]
发表于 2009-03-14 23:51 |只看该作者
declare @SqlStr varchar(8000)
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP