Arebhy Sridaran
Arebhy Sridaran

Reputation: 586

How to combine two query which is derive from database

i have two queries

$sql1="SELECT MDN, AVG(RSRP),count(case when RSRP !=0 THEN RSRP END) as total from CPE where (month(LAST_UPDATED_TIME)=1) group by MDN";
$sql2="SELECT MDN, AVG(RSRP),count(case when RSRP !=0 THEN RSRP END) as total from CPE where (month(LAST_UPDATED_TIME)=2) group by MDN";

my question is how to combine these two queries in one table with 6 columns

I tried this code but it gives error

select
(SELECT MDN, AVG(RSRP),count(case when RSRP !=0 THEN RSRP END) as total from CPE where (month(LAST_UPDATED_TIME)=1) group by MDN) as 1,
(SELECT MDN, AVG(RSRP),count(case when RSRP !=0 THEN RSRP END) as total from CPE where (month(LAST_UPDATED_TIME)=2) group by MDN) as 2

please help me to resolve this problem..

Upvotes: 1

Views: 52

Answers (1)

ScaisEdge
ScaisEdge

Reputation: 133370

You could JOIN

  SELECT t1.MDN
      , AVG(t1.RSRP)
      , count(case when t1.RSRP !=0 THEN t1.RSRP END) as total 
      , t2.avg2 
      , t2.total2
  from CPE t1
  LEFT JOIN(
      SELECT MDN
      , AVG(RSRP) avg2
      ,count(case when RSRP !=0 THEN RSRP END) as total2 
      from CPE 
      where (month(LAST_UPDATED_TIME)=2) 
      group by MDN
  ) t2 on t1.MDN = t2.MDN
  where (month(t1.LAST_UPDATED_TIME)=1) 
  group by t1.MDN

Upvotes: 3

Related Questions