使用子查询的查询是否可以返回多行?

时间:2011-02-26 03:47:12

标签: mysql sql

   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。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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