multiple query same table but in different columns mysql

后端 未结 4 2129
误落风尘
误落风尘 2020-11-22 09:28

I\'m trying to get more columns summarizing the result from 2 different tables

SET @start_res = 20150301;
SET @finish_res= 20150501;
SET @finish_check= 20150         


        
相关标签:
4条回答
  • 2020-11-22 09:51

    If i understand you correctly, here's what you want.

    SET @start_res = 20150301;
    SET @finish_res= 20150501;
    SET @finish_check= 20150801;
    SET @start_check= 20150301;
    SET @daily_hos= 3;
    
    SELECT 
    
    (SELECT COUNT(DAY_IN) FROM t_hospital WHERE 
    DAY_IN between  @start_check and @finish_check and 
    RES_DATE between @start_res and @finish_res and 
    ID_daily_hos =@daily_hos) AS 'arr' ,
    
    (SELECT COUNT(PAT_STATUS) FROM t_hospital WHERE 
    PAT_STATUS  like '%ong%' and  
    DAY_IN between @start_check and @finish_check and 
    RES_DATE between @start_res and @finish_res  and 
    ID_daily_hos =@daily_hos) AS 'ONG1' ,
    
    (SELECT COUNT(PAT_STATUS) FROM t_hospital WHERE 
    PAT_STATUS  like '%rtde%' and  
    DAY_IN between  @start_check and @finish_check and 
    RES_DATE between @start_res and @finish_res  and 
    ID_daily_hos =@daily_hos) AS 'RTED' ,
    
    (SELECT COUNT(PAT_STATUS) FROM t_hospital WHERE 
    PAT_STATUS  like '%pol%' and 
    DAY_IN between @start_check and @finish_check and 
    RES_DATE between @start_res and @finish_res  and 
    ID_daily_hos =@daily_hos) AS 'POLI' ,
    
    (SELECT COUNT(PAT_STATUS) FROM t_hospital WHERE 
    PAT_STATUS  like '%para%' and  
    DAY_IN between @start_check and @finish_check and 
    RES_DATE between @start_res and @finish_res  and 
    ID_daily_hos =@daily_hos) AS 'para'
    
    0 讨论(0)
  • 2020-11-22 09:52

    This is a pretty common pattern:

    SELECT DAY_IN, COUNT(*) AS arr,
            SUM(IF(PAT_STATUS  like '%ong%', 1, 0)) AS ONG1,
            SUM(IF(PAT_STATUS  like '%rtde%', 1, 0)) AS RTED,
            SUM(IF(PAT_STATUS  like '%pol%', 1, 0)) AS POL1,
            SUM(IF(PAT_STATUS  like '%para%', 1, 0)) AS para
    FROM t_hospital 
    WHERE DAY_IN between @start_check and @finish_check 
        and RES_DATE between @start_res and @finish_res  
        and ID_daily_hos =@daily_hos
    GROUP BY DAY_IN
    
    0 讨论(0)
  • 2020-11-22 10:06

    In SQL Server 2005+, you can use Window functions. But it isn't supported in MySQL.

    In Subquery, select statements must have join operator. Like this:

    SELECT * FROM (
        (SELECT COUNT(..) ...) AS C1
           INNER JOIN (SELECT COUNT(...) ...) AS C2
                ON <condition>
           INNER JOIN ... )
    
    0 讨论(0)
  • 2020-11-22 10:09

    Try this:-

    SET @start_res = 20150301;
    SET @finish_res= 20150501;
    SET @finish_check= 20150801;
    SET @start_check= 20150301;
    SET @daily_hos= 3;
    
    SELECT 
    
    (SELECT COUNT(DAY_IN) AS arr FROM t_hospital WHERE 
    DAY_IN between  @start_check and @finish_check and 
    RES_DATE between @start_res and @finish_res and 
    ID_daily_hos =@daily_hos) e,
    
    (SELECT COUNT(PAT_STATUS) AS ONG1 FROM t_hospital WHERE 
    PAT_STATUS  like '%ong%' and  
    DAY_IN between @start_check and @finish_check and 
    RES_DATE between @start_res and @finish_res  and 
    ID_daily_hos =@daily_hos) a,
    
    (SELECT COUNT(PAT_STATUS) AS RTED FROM t_hospital WHERE 
    PAT_STATUS  like '%rtde%' and  
    DAY_IN between  @start_check and @finish_check and 
    RES_DATE between @start_res and @finish_res  and 
    ID_daily_hos =@daily_hos)b,
    
    (SELECT COUNT(PAT_STATUS) AS POLI FROM t_hospital WHERE 
    PAT_STATUS  like '%pol%' and 
    DAY_IN between @start_check and @finish_check and 
    RES_DATE between @start_res and @finish_res  and 
    ID_daily_hos =@daily_hos) c,
    
    (SELECT COUNT(PAT_STATUS) AS para FROM t_hospital WHERE 
    PAT_STATUS  like '%para%' and  
    DAY_IN between @start_check and @finish_check and 
    RES_DATE between @start_res and @finish_res  and 
    ID_daily_hos =@daily_hos) d
    

    Your question mentions about joining 2 tables but there appears to be only t_hospital in question. You might want to update your question with information about the other table.

    0 讨论(0)
提交回复
热议问题