简单的SQL数据求百分率不对 --郁闷
各位:
我SQL统计数据表t_injPOhis(系统是客户设计的,injQty未完成就为null)分组产品、日期数据:计划数量(int类型)、完成数量(int类型)、差额、完成率
出现完成率总是1或0请情况,但完成数 和 计划数量都是对的:
Select pno,planDate,SUM(planQty) [planQty],SUM(Isnull(injQty,0)) [injQty],
SUM(planQty-Isnull(injQty,0)) [DiffQty],
SUM(Isnull(injQty,0))/SUM(planQty) [injRate]
From t_injPOhis
WHERE planDate between '2013-7-1' AND '2013-7-31'
Group By pno,planDate
运行结果:
J001RJ8006101 2013-07-01 00:00:00 45000 43200 1800 0
J001SJ80F5402 2013-07-01 00:00:00 45000 43200 1800 0
J4J9PG0033G02 2013-07-01 00:00:00 20000 16322 3678 0
S3027S00D1601 2013-07-01 00:00:00 8000 8000 0 1
S6118S0033G02 2013-07-01 00:00:00 1400 1320 80 0
------解决方案-------------------- SELECT pno ,
planDate ,
SUM(planQty) [planQty] ,
SUM(ISNULL(injQty, 0)) [injQty] ,
SUM(planQty - ISNULL(injQty, 0)) [DiffQty] ,
SUM(ISNULL(injQty, 0))*1.00 / SUM(planQty) [injRate] --这里修改一下,隐式转换了
FROM t_injPOhis
WHERE planDate BETWEEN '2013-7-1' AND '2013-7-31'
GROUP BY pno ,
planDate
------解决方案----------------------試試以下:
Select pno,planDate,SUM(planQty) [planQty],SUM(Isnull(injQty,0)) [injQty],
SUM(planQty-Isnull(injQty,0)) [DiffQty],
SUM(1.0*Isnull(injQty,0))/SUM(planQty) [injRate]
From t_injPOhis
WHERE planDate between '2013-7-1' AND '2013-7-31'
Group By pno,planDate