怎么算没有重复?

时间:2016-10-12 15:22:07

标签: sql fastreport

我得到了以下查询,我想在EAMATTNSTATUS = 7或8为0.5时计算EAMDATE,当EAMATTNSTATUS = 6为1时计算EAMDATE。

  SELECT EAMEMID,
           SUM(CASE WHEN EAMATTNSTATUS in (7) OR EAMATTNSTATUS in (8) THEN 1 ELSE 0 END) / 2 + COUNT(DISTINCT CASE WHEN EAMATTNSTATUS in (6) THEN EAMDATE END) as ABSENTDAYS  
    FROM viewDAILYSUM  
    WHERE (EAMEMID = :EAMEMID) AND
          (EAMDATE between :FDate and :TDate)
    GROUP BY EAMEMID                                                                                                     
    ORDER BY EAMEMID
对我来说困难的部分是,在某种情况下,一个EAMDATE可能有EAMATTNSTATUS = 7或8状态和EAMATTNSTATUS = 6。这意味着它将复制该EAMDATE的计数两次,因为它具有2状态。

我怎么能修复重复?

我试图使用Distinct但它没有用......

任何想法??!

由于

SAMPLE DATA 
    EAMEID | EAMDATE | EAMATTNSTATUS| 
    ------ | --------| -------------|
    161    |20/7/2016| 6            | 
    161    |20/7/2016| 7            |   
    161    |21/7/2016| 6            |   
    161    |22/7/2016| 5            |   
    161    |23/7/2016| 8            |  
CURRENT OUTPUT

EAMEID | EAMDATE | ABSENTDAYS   |  
------ | --------| -------------|
161    |20/7/2016| 1            | 
161    |20/7/2016| 0.5          |   
161    |21/7/2016| 1            |   
161    |22/7/2016| 0            |   
161    |23/7/2016| 0.5          | 

WANTED RESULT 

EAMEID | EAMDATE | ABSENTDAYS   |  
------ | --------| -------------|
161    |20/7/2016| 0.5          |   
161    |21/7/2016| 1            |   
161    |22/7/2016| 0            |   
161    |23/7/2016| 0.5          | 

2 个答案:

答案 0 :(得分:0)

很抱歉,不清楚(对我来说)你的需求是什么。 例如,您需要申请的逻辑

web.xml

开始
EAMEID | EAMDATE | ABSENTDAYS   |  
------ | --------| -------------|
161    |20/7/2016| 0.5          |   

我的意思是......为什么ABSENTDAYS是0.5而不是1?您需要应用于查询的逻辑是什么?

答案 1 :(得分:0)

您可以将查询用作子查询,然后分组。

SELECT EAMEMID, EAMDATE, MIN(ABSENTDAYS) MINABSENT FROM  (SELECT EAMEMID,EAMDATE,
       SUM(CASE WHEN EAMATTNSTATUS in (7) OR EAMATTNSTATUS in (8) THEN 1 ELSE 0 END) / 2 + COUNT(DISTINCT CASE WHEN EAMATTNSTATUS in (6) THEN EAMDATE END) as ABSENTDAYS  
FROM viewDAILYSUM  
WHERE (EAMEMID = :EAMEMID) AND
      (EAMDATE between :FDate and :TDate)
GROUP BY EAMEMID                                                                                                 
ORDER BY EAMEMID)
GROUP BY EAMEMID, EAMDATE