Mysql Query返回记录计数多于返回行,我想要所有行

时间:2017-06-21 05:49:49

标签: mysql mysqli

select

 (select sum(calls) from(select count(distinct callid_app) as calls,parent_id as area_id from fp_data as f inner join data_entities as d on d.unique_id=f.from_branch where f.type='Ext/In' and (date_time BETWEEN '2017-05-08 00:00:00' AND '2017-05-09 23:59:59') and (weekday(date_time)<=4) and (hour(date_time) between 9 and 16) and d.parent_id in(6,13,21,27) and ignore_call!=1 and d.unique_id>0 group by d.unique_id)as total_calls group by area_id) as t, 

(select sum(calls) from (select count(distinct callid_app) as calls,parent_id as col from fp_data as f inner join data_entities as d on d.unique_id=f.from_branch where f.type='Ext/In' and (date_time BETWEEN '2017-05-08 00:00:00' AND '2017-05-09 23:59:59') and (weekday(date_time)<=4) and (hour(date_time) between 9 and 16) and d.parent_id in (6,13,21,27)  and  seg_other_value='1' and start_reason ='[New Call]' and d.unique_id>0 group by unique_id) as calls group by col) as t2                                   from fp_data as f inner join data_entities as d on d.unique_id=f.from_branch

给我错误:Subquery returns more than 1 row

id的总和是group by,所以它给出了多行,我想要所有这些行,所以请告诉我解决方案是什么

我希望结果应该像

         t, t2
         94, 56
         42  43
         15  76
         75  78

2 个答案:

答案 0 :(得分:0)

    SELECT *
FROM
    (SELECT 
        SUM(calls) t, NULL t2
    FROM
        (SELECT 
        COUNT(DISTINCT callid_app) AS calls, parent_id AS area_id
    FROM
        fp_data AS f
    INNER JOIN data_entities AS d ON d.unique_id = f.from_branch
    WHERE
        f.type = 'Ext/In'
            AND (date_time BETWEEN '2017-05-08 00:00:00' AND '2017-05-09 23:59:59')
            AND (WEEKDAY(date_time) <= 4)
            AND (HOUR(date_time) BETWEEN 9 AND 16)
            AND d.parent_id IN (6 , 13, 21, 27)
            AND ignore_call != 1
            AND d.unique_id > 0
    GROUP BY d.unique_id) AS total_calls
    GROUP BY area_id 
    UNION ALL 
    SELECT 
        NULL t, SUM(calls) t2
    FROM
        (SELECT 
        COUNT(DISTINCT callid_app) AS calls, parent_id AS col
    FROM
        fp_data AS f
    INNER JOIN data_entities AS d ON d.unique_id = f.from_branch
    WHERE
        f.type = 'Ext/In'
            AND (date_time BETWEEN '2017-05-08 00:00:00' AND '2017-05-09 23:59:59')
            AND (WEEKDAY(date_time) <= 4)
            AND (HOUR(date_time) BETWEEN 9 AND 16)
            AND d.parent_id IN (6 , 13, 21, 27)
            AND seg_other_value = '1'
            AND start_reason = '[New Call]'
            AND d.unique_id > 0
    GROUP BY unique_id) AS calls
    GROUP BY col)z

您可以尝试以上查询。

希望这会对你有所帮助。

答案 1 :(得分:0)

得到它

中选择t,t2

(SELECT

    SUM(calls) t, area_id
FROM
    (SELECT 
    COUNT(DISTINCT callid_app) AS calls, parent_id AS area_id
FROM
    fp_data AS f
INNER JOIN data_entities AS d ON d.unique_id = f.from_branch
WHERE
    f.type = 'Ext/In'
        AND (date_time BETWEEN '2017-05-08 00:00:00' AND '2017-05-09 23:59:59')
        AND (WEEKDAY(date_time) <= 4)
        AND (HOUR(date_time) BETWEEN 9 AND 16)
        AND d.parent_id IN (6 , 13, 21, 27)
        AND ignore_call != 1
        AND d.unique_id > 0
GROUP BY d.unique_id) AS total_calls
GROUP BY area_id) as total_calls inner join 
(SELECT 
    area_id, SUM(calls) t2
FROM
    (SELECT 
    COUNT(DISTINCT callid_app) AS calls, parent_id AS area_id
FROM
    fp_data AS f
INNER JOIN data_entities AS d ON d.unique_id = f.from_branch
WHERE
    f.type = 'Ext/In'
        AND (date_time BETWEEN '2017-05-08 00:00:00' AND '2017-05-09 23:59:59')
        AND (WEEKDAY(date_time) <= 4)
        AND (HOUR(date_time) BETWEEN 9 AND 16)
        AND d.parent_id IN (6 , 13, 21, 27)
        AND seg_other_value = '1'
        AND start_reason = '[New Call]'
        AND d.unique_id > 0
GROUP BY unique_id) AS calls
GROUP BY area_id) as branch_total_calls on total_calls.area_id=branch_total_calls.area_id