日期:2014-05-17  浏览次数:20869 次

关于数据统计,总数量,上下限内数量,及其他(急!!!!!)
大家好,本人有一个表,
表结构如下:
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[a_WeightTab]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[a_WeightTab]
GO
CREATE TABLE [dbo].[a_WeightTab] (
[c_ID] [int] IDENTITY (1, 1) NOT NULL ,
[c_MachineId] [varchar] (5) COLLATE Chinese_PRC_CI_AS NULL ,
[c_TypeNo] [varchar] (5) COLLATE Chinese_PRC_CI_AS NULL ,
[c_TypeName] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[c_Tyre] [varchar] (10) COLLATE Chinese_PRC_CI_AS NULL ,
[c_StandWeight] [decimal](18, 3) NULL ,
[c_UpWeight] [decimal](18, 3) NULL ,
[c_DoWeight] [decimal](18, 3) NULL ,
[c_Weight] [decimal](18, 3) NULL ,
[c_Status] [varchar] (6) COLLATE Chinese_PRC_CI_AS NULL ,
[c_date] [datetime] NULL 
) ON [PRIMARY]
GO

其中 c_UpWeight=(1+0.015)*StandWeight, c_DoWeight=(1-0.015)*StandWeight,c_Weight为实际重量
现在统计 c_Weight的各个区间范围,SQL 如下
SELECT c_MachineId,c_TypeNo,c_TypeName,c_Tyre,c_StandWeight,AllCount=count(*),OkCount=sum(case when c_Weight>=c_DoWeight and c_Weight<=c_UpWeight then 1 else 0 end),c_OkPer=cast(sum(case when c_Weight>=c_DoWeight and c_Weight<=c_UpWeight then 1 else 0 end)*100 as float) / count(*),OverMidWeight=sum(case when c_Weight>=c_standWeight then c_Weight else 0 end),OverMidCount=sum(case when c_Weight>=c_standWeight then 1 else 0 end),OverMidOkCount=sum(case when c_Weight>=c_standWeight and c_Weight<c_UpWeight then 1 else 0 end),OverMidOkPer=cast(sum(case when c_Weight>c_standWeight and c_Weight<c_UpWeight then 1 else 0 end)*100 as float) / count(*),DownMidWeight=sum(case when c_Weight<c_standWeight then c_Weight else 0 end),DownMidCount=sum(case when c_Weight<c_standWeight then 1 else 0 end),DownMidOkCount=sum(case when c_Weight<c_standWeight and c_Weight>=c_DoWeight then 1 else 0 end),DownMidOkPer=cast(sum(case when c_Weight<c_standWeight and c_Weight>=c_DoWeight then 1 else 0 end) *100 as float) /count(*),AllWeight=sum(c_Weight),c_WeightOff=sum(c_Weight)-c_standWeight*count(*),c_UpLimitWeight=sum(case when c_Weight>=c_standWeight then c_Weight else 0 end)-c_standWeight*sum(case when c_Weight>=c_standWeight then 1 else 0 end),c_DoLimitWeight=sum(case when c_Weight<=c_standWeight then c_Weight else 0 end)-c_standWeight*sum(case when c_Weight<=c_standWeight then 1 else 0 end) FROM a_WeightTab where c_Date between '2012-08-11 06:59:46' and '2012-08-14 06:59:46' group by c_TypeNo,c_MachineId,c_Tyre,c_TypeName,c_StandWeight

但是出现如下情况:上述SQL得到结果:总数量=中值以上数量+中值以下数量,合格数量<>中值以上合格数+中值以下合格数,总重量=中值以上重量+中值以下重量,找了半天没找到原因,不知为啥

另,为啥
SELECT --COUNT(*) AS ALLs, --总数量
  SUM(CASE WHEN (c_Weight>=c_StandWeight) THEN 1 ELSE 0 END) AS UPCount, --中值以上数量 ,包含中值
  SUM(CASE WHEN (c_Weight<=c_UpWeight) and (c_Weight >= c_StandWeight) THEN 1 ELSE 0 END) AS upMidOKCount , --中值以上合格数量 ,包含中值
  SUM(CASE WHEN (c_Weight>c_UpWeight) THEN 1 ELSE 0 END) AS upMidErrorCount --中值以上不合格数量
FROM a_WeightTab

得到
UPCount upMidOKCount upMidErrorCount  
3625 1889 1802

------解决方案--------------------
你给出数据和期望结果,这样会明确直观一些
------解决方案--------------------
请散来。
------解决方案--------------------
见者有份。