2012-08-02 40 views
0

我有一個用戶活動表。我有HWA,活動日期和組ID。我得到的季度基礎上的日期範圍即:消除來自分組用戶使用的重疊宿舍

日期範圍內的用戶有活動:2011-08-01 - 2012-07-31

Q1 - Aug-Oct 
Q2 - Nov-Jan 
Q3 - Feb-Apr 
Q4 - May-Jul 

一些HWAS有其他的替代品。在這些情況下,它們具有相同的組ID以顯示它們已鏈接。在這些情況下,我需要取消重疊宿舍。爲了澄清,如果兩個HWA在第二季度有使用並且他們具有相同的組ID,那麼我只需要對第二季度中的一個進行計數(這並不重要)。下面是我現在用的與輸出的查詢,我得到:

SELECT act.HWA, count(act.HWA), m.Status, m.GroupID 
    FROM (
     SELECT 
      a.HWA, a.Date, 
      CASE 
      WHEN MONTH(a.Date) BETWEEN 8 AND 10 THEN 'Q1' 
      WHEN (MONTH(a.Date) BETWEEN 11 AND 12 OR MONTH(a.Date) = 1) THEN 'Q2' 
      WHEN MONTH(a.Date) BETWEEN 2 AND 4 THEN 'Q3' 
      WHEN MONTH(a.Date) BETWEEN 5 AND 7 THEN 'Q4' 
      END AS quarter, 
      CASE 
      WHEN MONTH(a.Date) <= 7 THEN YEAR(a.Date) - 1 
      ELSE EXTRACT(YEAR_MONTH from a.Date) 
      END AS quarteryear, 
      COUNT(*) AS num_activites 
     FROM activity a 
     WHERE a.InstitutionNumber = '000000000075' 
      AND (a.HWA = '001372EADBC4' 
      OR a.HWA = '180373E241DB' 
      OR a.HWA = '180373E23DE7') 
      AND (a.Date between '2011-08-01' and '2012-07-31') 
     GROUP BY 
      quarter, 
      a.HWA 
    ) act, machine m 
    where act.HWA = m.HWA 
    group by act.HWA 

它返回這樣的:

HWA    quarters  status  groupid 

001372EADBC4  3  deleted  59970 
180373E23DE7  2   online  59970 
180373E241DB  1  deleted  59970 

預期的結果將是:

HWA季度狀態組識別符號

001372EADBC4  2  deleted  59970 
180373E23DE7  2   online  59970 
180373E241DB  0  deleted  59970 

下面是我通過稍微改變上述查詢生成的每個HWA的宿舍列表。你可以看到三個HWAS有股3季度,我想只是其中之一,有四分之一3:

001372EADBC4 1 deleted 59970 
001372EADBC4 2 deleted 59970 
001372EADBC4 3 deleted 59970 
180373E23DE7 3 online 59970 
180373E241DB 3 deleted 59970 
180373E23DE7 4 online 59970 
+0

什麼是您目前的結果和通緝的結果之間的difrences,不能看到你當前結果 – 2012-08-02 21:31:39

+0

任何dublicate值這將是很好的補充一些示例表數據(與相應的預期結果)讓我們共同努力。 – 2012-08-02 21:38:52

+0

你可以用數學來計算一個大案例的四分之一,'CONCAT('Q',1 +(FLOOR((MONTH(a.Date)+ 4)/ 3)%4)'AS quarter,where'+ 4'是新年前第一季度開始前的月數 – 2012-08-02 21:40:36

回答

0

所以我想通了通過僅改變周圍的分組大部分:

SELECT act.HWA, count(act.HWA), m.Status, m.GroupID 
    FROM (
    SELECT 
     a.HWA, a.Date, 
     CASE 
     WHEN MONTH(a.Date) BETWEEN 8 AND 10 THEN '1' 
     WHEN (MONTH(a.Date) BETWEEN 11 AND 12 OR MONTH(a.Date) = 1) THEN '2' 
     WHEN MONTH(a.Date) BETWEEN 2 AND 4 THEN '3' 
     WHEN MONTH(a.Date) BETWEEN 5 AND 7 THEN '4' 
     END AS quarter, 
     CASE 
      WHEN MONTH(a.Date) <= 7 THEN YEAR(a.Date) - 1 
      ELSE EXTRACT(YEAR_MONTH from a.Date) 
     END AS quarteryear, 
     COUNT(*) AS num_activites 
     FROM activity a, machine m 
     WHERE 
     a.InstitutionNumber = '000000000075' 
     AND m.InstitutionID = 14 
     AND (a.HWA = '001372EADBC4' 
      OR a.HWA = '180373E241DB' 
      OR a.HWA = '180373E23DE7') 
     AND a.HWA = m.HWA 
     AND (a.Date between '2011-08-01' and '2012-07-31') 
     AND a.EngineRequests > 0 
     AND a.FilesAnalyzed > 0 
     GROUP BY 
     quarter, 
     m.GroupID 
) act, machine m 
WHERE act.HWA = m.HWA 
AND m.InstitutionID = 14 
GROUP BY act.HWA 

我在SELECT動作如下加入:

AND m.InstitutionID = 14 組按m.GroupID

和我刪除從下述SELECT行爲:

集團通過a.HWA