需要计算查询帮助

时间:2010-12-30 18:15:11

标签: sql-server sql-server-2005 sql-server-2008

我有用户使用修复28 CheckList项目,每天用户必须输入每个清单项目的价值。

用户有时会部分填写列表或保持不完整,所以我需要告诉他们你已经离开了清单INCOMPLETE或填充了PARTIALLY因此保持一个名为“trans_status”的字段,默认情况下在数据库中用0表示不完整和一次用户填写数据值为1,其中28个条目混合1和0表示PARTIAL,如果所有28个enties都表示INCOMPLETE,如果所有1表示已完成。

这是结构

CREATE TABLE [dbo].[VTRCheckListDetails](
    [userid] [int] NULL,
    [branchid] [int] NULL,
    [vtrRespDate] [date] NULL,
    [CLid] [int] NULL,
    [VtrValue] [varchar](5) NULL,
    [trans_status] [int] NULL,
    [last_updated] [int] NULL
 ) ON [PRIMARY]

以下是样本数据

INSERT VTRCheckListDetails(userid,branchid,vtrRespDate,CLid,VtrValue,trans_status,last_updated)       VALUES('144','14','2010-12-30','1','1','1','0')

INSERT VTRCheckListDetails(userid,branchid,vtrRespDate,CLid,VtrValue,trans_status,last_updated)       VALUES('144','14','2010-12-30','2','2','0','0')    

INSERT VTRCheckListDetails(userid,branchid,vtrRespDate,CLid,VtrValue,trans_status,last_updated)       VALUES('144','14','2010-12-30','3','3','0','0')      

INSERT VTRCheckListDetails(userid,branchid,vtrRespDate,CLid,VtrValue,trans_status,last_updated)       VALUES('144','14','2010-12-30','4','4','0','0')   

INSERT VTRCheckListDetails(userid,branchid,vtrRespDate,CLid,VtrValue,trans_status,last_updated)       VALUES('144','14','2010-12-30','5','5','0','0')     

INSERT VTRCheckListDetails(userid,branchid,vtrRespDate,CLid,VtrValue,trans_status,last_updated)       VALUES('144','14','2010-12-30','6','6','0','0')  

 INSERT VTRCheckListDetails(userid,branchid,vtrRespDate,CLid,VtrValue,trans_status,last_updated)       VALUES('144','14','2010-12-30','7','7','0','0')  

INSERT VTRCheckListDetails(userid,branchid,vtrRespDate,CLid,VtrValue,trans_status,last_updated)       VALUES('144','14','2010-12-30','8','8','0','0')   

INSERT VTRCheckListDetails(userid,branchid,vtrRespDate,CLid,VtrValue,trans_status,last_updated)       VALUES('144','14','2010-12-30','9','9','0','0')  

INSERT VTRCheckListDetails(userid,branchid,vtrRespDate,CLid,VtrValue,trans_status,last_updated)       VALUES('144','14','2010-12-30','10','10','0','0')  

INSERT VTRCheckListDetails(userid,branchid,vtrRespDate,CLid,VtrValue,trans_status,last_updated)       VALUES('144','14','2010-12-30','11','11','0','0')   

INSERT VTRCheckListDetails(userid,branchid,vtrRespDate,CLid,VtrValue,trans_status,last_updated)       VALUES('144','14','2010-12-30','12','12','0','0')   

INSERT VTRCheckListDetails(userid,branchid,vtrRespDate,CLid,VtrValue,trans_status,last_updated)       VALUES('144','14','2010-12-30','13','13','0','0')   

INSERT VTRCheckListDetails(userid,branchid,vtrRespDate,CLid,VtrValue,trans_status,last_updated)       VALUES('144','14','2010-12-30','14','14','0','0')   

INSERT VTRCheckListDetails(userid,branchid,vtrRespDate,CLid,VtrValue,trans_status,last_updated)       VALUES('144','14','2010-12-30','15','15','0','0')   

INSERT VTRCheckListDetails(userid,branchid,vtrRespDate,CLid,VtrValue,trans_status,last_updated)       VALUES('144','14','2010-12-30','16','16','0','0')  

INSERT VTRCheckListDetails(userid,branchid,vtrRespDate,CLid,VtrValue,trans_status,last_updated)       VALUES('144','14','2010-12-30','17','17','0','0')   

INSERT VTRCheckListDetails(userid,branchid,vtrRespDate,CLid,VtrValue,trans_status,last_updated)       VALUES('144','14','2010-12-30','18','18','0','0')  

INSERT VTRCheckListDetails(userid,branchid,vtrRespDate,CLid,VtrValue,trans_status,last_updated)       VALUES('144','14','2010-12-30','19','19','0','0')  

INSERT VTRCheckListDetails(userid,branchid,vtrRespDate,CLid,VtrValue,trans_status,last_updated)       VALUES('144','14','2010-12-30','20','20','0','0')  

INSERT VTRCheckListDetails(userid,branchid,vtrRespDate,CLid,VtrValue,trans_status,last_updated)       VALUES('144','14','2010-12-30','21','21','0','0')   

INSERT VTRCheckListDetails(userid,branchid,vtrRespDate,CLid,VtrValue,trans_status,last_updated)       VALUES('144','14','2010-12-30','22','22','0','0')   

INSERT VTRCheckListDetails(userid,branchid,vtrRespDate,CLid,VtrValue,trans_status,last_updated)       VALUES('144','14','2010-12-30','23','23','0','0')   

INSERT VTRCheckListDetails(userid,branchid,vtrRespDate,CLid,VtrValue,trans_status,last_updated)       VALUES('144','14','2010-12-30','24','24','0','0')   

INSERT VTRCheckListDetails(userid,branchid,vtrRespDate,CLid,VtrValue,trans_status,last_updated)       VALUES('144','14','2010-12-30','25','25','0','0')   

INSERT VTRCheckListDetails(userid,branchid,vtrRespDate,CLid,VtrValue,trans_status,last_updated)       VALUES('144','14','2010-12-30','26','26','0','0')   

INSERT VTRCheckListDetails(userid,branchid,vtrRespDate,CLid,VtrValue,trans_status,last_updated)       VALUES('144','14','2010-12-30','27','27','0','0')   

INSERT VTRCheckListDetails(userid,branchid,vtrRespDate,CLid,VtrValue,trans_status,last_updated)       VALUES('144','14','2010-12-30','28','28','0','0')   

我想要的是当我运行查询时它应该在日期上分组并根据trans_status显示这样的数据

Date.............Status
30-12-12..........Partial (can be complete, incomplete)

2 个答案:

答案 0 :(得分:3)

SELECT   userid     ,
         vtrRespDate,
         CASE
                  WHEN MAX(trans_status) = 0
                  THEN 'InComplete'
                  WHEN MIN(trans_status)=1
                  THEN 'Complete'
                  ELSE 'Partial'
         END AS status
FROM     VTRCheckListDetails
GROUP BY userid,
         vtrRespDate

答案 1 :(得分:2)

SELECT  vtrRespDate,
        CASE COUNT(CASE trans_status WHEN 1 THEN 1 END)
        WHEN 0 THEN
                'Incomplete'
        WHEN COUNT(*) THEN
                'Complete'
        ELSE
                'Partial'
        END AS Status
FROM    VTRCheckListDetails
GROUP BY
        vtrRespDate