如何收集两个SQL查询的结果?

时间:2010-03-04 09:59:46

标签: sql

SELECT * FROM(
        (SELECT 
            count(DISTINCT RECEPIENT_ID) as NoOfUsers,
            TO_CHAR(ACTN_TAKE_DATA_TM,'YYYY-MM-DD') as accDate 
        FROM 
            ALRT_PLATFORM_ALRT_HSTRY  
        where 
            APPL_CD like 'EBP' and 
            ALRT_RSPNS_FROM_CLIENT_ID like 'BB' 
            group by TO_CHAR(ACTN_TAKE_DATA_TM,'YYYY-MM-DD')
        ) b,

        (SELECT 
            count(DISTINCT RECEPIENT_ID) as NoOfUsers, 
            TO_CHAR(ACTN_TAKE_DATA_TM,'YYYY-MM-DD') as accDate 
        FROM 
            ALRT_PLATFORM_ALRT 
        where 
            APPL_CD like 'EBP' and 
            ALRT_RSPNS_FROM_CLIENT_ID like 'BB' 
            group by TO_CHAR(ACTN_TAKE_DATA_TM,'YYYY-MM-DD')
        ) f   
        )

此查询以以下格式返回数据:

NOOFUSERS              ACCDATE    NOOFUSERS              ACCDATE    
---------------------- ---------- ---------------------- ---------- 
2                      2009-12-21 1                      2010-03-01 
2                      2009-12-21 2                      2010-03-02 
2                      2009-12-21 1                      2010-03-03 
1                      2009-12-23 1                      2010-03-01 

是否有可能将两张桌子重新组合起来: 我希望数据采用这种格式:

NOOFUSERS              ACCDATE    
---------------------- ---------- 
2                      2009-12-21 
1                      2009-12-23 
1                      2010-01-02 
1                      2010-01-04 //till here its table one data
1                      2010-03-01 //from here its table TWO data
2                      2010-03-02 
1                      2010-03-03 

谢谢:)

6 个答案:

答案 0 :(得分:2)

使用UNION ALL而不是subselected列会将后续UNION ALL语句附加到原始select。要小心,每个select语句中需要相同数量的列。

UNION ALL确保组合结果未排序/混合(第一个查询在第二个之前返回)。常规UNION可以/将混合/混合/排序结果。

SELECT 
            count(DISTINCT RECEPIENT_ID) as NoOfUsers,
            TO_CHAR(ACTN_TAKE_DATA_TM,'YYYY-MM-DD') as accDate 
        FROM 
            ALRT_PLATFORM_ALRT_HSTRY  
        where 
            APPL_CD like 'EBP' and 
            ALRT_RSPNS_FROM_CLIENT_ID like 'BB' 
            group by TO_CHAR(ACTN_TAKE_DATA_TM,'YYYY-MM-DD')


UNION ALL

       SELECT 
            count(DISTINCT RECEPIENT_ID) as NoOfUsers, 
            TO_CHAR(ACTN_TAKE_DATA_TM,'YYYY-MM-DD') as accDate 
        FROM 
            ALRT_PLATFORM_ALRT 
        where 
            APPL_CD like 'EBP' and 
            ALRT_RSPNS_FROM_CLIENT_ID like 'BB' 
            group by TO_CHAR(ACTN_TAKE_DATA_TM,'YYYY-MM-DD')

答案 1 :(得分:2)

如果您需要经常一起查询这些表,那么您可以考虑使用视图。

create view alrt_platform_alrt_all as
  select * from alrt_platform_alrt
union all
  select * from alrt_platform_alrt_hstry;

这样就可以像这样查询数据:

select 
  count(distinct recepient_id), 
  TO_CHAR(ACTN_TAKE_DATA_TM,'YYYY-MM-DD') as accDate
from alrt_platform_alrt_all
where appl_cd like 'EBP'
  and alrt_rspns_from_client_id like 'BB'
group by TO_CHAR(ACTN_TAKE_DATA_TM,'YYYY-MM-DD');

答案 2 :(得分:1)

也许您必须使用UNION运算符? http://dev.mysql.com/doc/refman/5.5/en/union.html

SELECT count(DISTINCT RECEPIENT_ID) as NoOfUsers,
            TO_CHAR(ACTN_TAKE_DATA_TM,'YYYY-MM-DD') as accDate 
        FROM 
            ALRT_PLATFORM_ALRT_HSTRY  
        where 
            APPL_CD like 'EBP' and 
            ALRT_RSPNS_FROM_CLIENT_ID like 'BB' 
            group by TO_CHAR(ACTN_TAKE_DATA_TM,'YYYY-MM-DD')

UNION      
    SELECT count(DISTINCT RECEPIENT_ID) as NoOfUsers, 
          TO_CHAR(ACTN_TAKE_DATA_TM,'YYYY-MM-DD') as accDate 
        FROM 
            ALRT_PLATFORM_ALRT 
        where 
            APPL_CD like 'EBP' and 
            ALRT_RSPNS_FROM_CLIENT_ID like 'BB' 
            group by TO_CHAR(ACTN_TAKE_DATA_TM,'YYYY-MM-DD')

答案 3 :(得分:0)

尝试联合声明

(select1)union(select2)

    SELECT 
        count(DISTINCT RECEPIENT_ID) as NoOfUsers,
        TO_CHAR(ACTN_TAKE_DATA_TM,'YYYY-MM-DD') as accDate 
    FROM 
        ALRT_PLATFORM_ALRT_HSTRY  
    where 
        APPL_CD like 'EBP' and 
        ALRT_RSPNS_FROM_CLIENT_ID like 'BB' 
        group by TO_CHAR(ACTN_TAKE_DATA_TM,'YYYY-MM-DD') 

UNION

   SELECT 
        count(DISTINCT RECEPIENT_ID) as NoOfUsers, 
        TO_CHAR(ACTN_TAKE_DATA_TM,'YYYY-MM-DD') as accDate 
    FROM 
        ALRT_PLATFORM_ALRT 
    where 
        APPL_CD like 'EBP' and 
        ALRT_RSPNS_FROM_CLIENT_ID like 'BB' 
        group by TO_CHAR(ACTN_TAKE_DATA_TM,'YYYY-MM-DD')

答案 4 :(得分:0)

您可以使用UNION

组合这两个结果

示例:

select col1, col2 
from table1 
where...

UNION

select col1, col2 
from table2
where...

答案 5 :(得分:0)

如我所见,您在两个集合之间没有任何连接条件(bf)。因此,请使用UNIONUNION ALLRefer here