2011-02-26 66 views
0
SELECT p.value AS __color__, 
      milestone AS __group__, 
      milestone, 
      priority, 
      time AS created, 
      COUNT(t.id) as 'total open tickets', 
      SUM(c.value) as 'Total Dev LOE', 
      SUM(d.value) as 'Total QALOE'  
    FROM ticket t 
LEFT JOIN ticket_custom c ON (t.id = c.ticket AND c.name = 'devloe') 
LEFT JOIN ticket_custom d ON (t.id = d.ticket AND d.name = 'qaloe') 
LEFT JOIN enum p ON p.name = t.priority AND p.type = 'priority' 
    WHERE t.milestone = '$MILESTONE' 
     AND status <> 'closed' 
GROUP BY milestone, priority, DATE(FROM_UNIXTIME(time/1000000)) DESC 

然後添加子查詢以返回每個優先級的總和devloe和qaloe。是否有可能使用子查詢返回多行的查詢?

+3

目前還不清楚你想要什麼。請發佈樣本期望的輸出。 – 2011-02-26 04:03:33

回答

0
Select p.value AS __color__ 
    , milestone AS __group__ 
    , milestone 
    , priority 
    , time AS created 
    , Count(t.id) as 'total open tickets' 
    , Sum(Case When c.name = 'devloe' Then c.value End) As 'Total Dev LOE' 
    , Sum(Case When c.name = 'qaloe' Then c.value End) As 'Total QALOE' 
    , PriorityCounts.Total_Dev_LOE As Priority_Total_QALOE 
    , PriorityCounts.Total_QALOE As Priority_Total_QALOE 
From ticket As t 
    Left Join ticket_custom As c 
     On t.id = c.ticket 
      And c.name In('devloe', 'qaloe') 
    Left Join enum p 
     On p.name = t.priority 
      AND p.type = 'priority' 
    Join (
      Select t1.priority, 
       , Sum(Case When c1.name = 'devloe' Then c1.value End) As Total_Dev_LOE 
       , Sum(Case When c1.name = 'qaloe' Then c1.value End) As Total_QALOE 
      From ticket As t1 
       Left Join ticket_custom As c1 
        On t1.id = c1.ticket 
         And c1.name In('devloe', 'qaloe') 
      Group By t1.priority 
      ) As PriorityCounts 
     On PriorityCounts.priority = t.priority 
Where t.milestone = '$MILESTONE' 
     And status <> 'closed' 
Group By milestone, priority, DATE(FROM_UNIXTIME(time/1000000)) DESC 
0

不回答你的問題,但你可以簡化查詢:

SELECT p.value AS __color__, 
      milestone AS __group__, 
      milestone, 
      priority, 
      time AS created, 
      COUNT(t.id) as 'total open tickets', 
      SUM(CASE WHEN c.name = 'devloe' THEN c.value ELSE 0 END) as 'Total Dev LOE', 
      SUM(CASE WHEN c.name = 'qaloe' THEN c.value ELSE 0 END) as 'Total QALOE'  
    FROM TICKET t 
LEFT JOIN TICKET_CUSTOM c ON c.ticket = t.id 
         AND c.name IN ('devloe', 'qaloe') 
LEFT JOIN ENUM p ON p.name = t.priority 
       AND p.type = 'priority' 
    WHERE t.milestone = '$MILESTONE' 
     AND status <> 'closed' 
GROUP BY milestone, priority, DATE(FROM_UNIXTIME(time/1000000)) DESC