2

我有这个查询,它基于一些与INNER JOIN.

目前我有一个WHERE条款,它一次提取一个 id。但是我怎样才能让它列出所有的ID呢?

GROUP BY在不同的地方尝试过,但无法弄清楚。

到目前为止,我的查询如下:

SELECT 
equipment_id, 
service_duration,
available_duration,
(available_duration / service_duration)*100 AS availability
FROM (
SELECT
SUM(service_end_time - service_start_time) AS service_duration
FROM(   
    SELECT equipment_id, 
    (CASE  
    END) AS service_start_time,
    (CASE  
    END) AS service_end_time
FROM t1 
WHERE equipment_id = 'EX123' 
  )AS A
) AS B
JOIN(
SELECT equipment_id, 
       SUM(available_end_time - available_start_time) AS available_duration
FROM (
    SELECT equipment_id,  
    (CASE 
    END) AS available_start_time,
    (CASE       
    END) AS available_end_time
FROM t2 
WHERE equipment_id = 'EX123'
  ) AS C
) AS D
ON equipment_id=D.equipment_id    

我想要做的是WHERE用 a 替换子句GROUP BY以列出所有 id 或类似的,但是让它工作超出了我的技能水平......非常感谢任何帮助:)

4

2 回答 2

2

试试下面:

    SELECT 
    equipment_id, service_duration, available_duration,
    (available_duration / service_duration)*100 AS availability
    FROM 
    (
            SELECT equipment_id,
            SUM(service_end_time - service_start_time) AS service_duration
            FROM
            (   
                    SELECT equipment_id, 
                        (CASE ... END) AS service_start_time,
                        (CASE  ...  END) AS service_end_time
                    FROM t1         
            ) AS A
            GROUP BY equipment_id
    ) AS B
    JOIN
    (
            SELECT equipment_id, 
            SUM(available_end_time - available_start_time) AS available_duration
            FROM 
            (
                SELECT equipment_id,  
                (CASE ... END) AS available_start_time,
                (CASE ... END) AS available_end_time
                FROM t2         
            ) AS C
            GROUP BY equipment_id
    ) AS D
    ON equipment_id=D.equipment_id 
于 2013-03-01T12:51:53.230 回答
1

试试这个(用你的字段名替换我的字段名):

SELECT 
    a.emp_id, 
    service_duration,
    available_duration 

FROM 
    (
        SELECT 
            emp_id,
            SUM(service_end_time - service_start_time) AS service_duration

        FROM 
            data 

        GROUP BY 
            emp_id 
    ) a

    JOIN
    (
        SELECT 
            emp_id, 
            SUM(available_end_time - available_start_time) AS available_duration

        FROM 
            data 

        GROUP BY
            emp_id
    ) b 
    ON a.emp_id = b.emp_id 

GROUP BY
    a.emp_id
于 2013-03-01T13:19:08.313 回答