0

i have 2 query in one table t_push_member

SELECT
  DATE_FORMAT(`member_subscribe_at`, '%d-%m-%y') Tanggal,
  count(`member_msisdn`) 'total_reg'
FROM
  `t_push_member` 
WHERE
  `service_keycode`='nocan' 
GROUP BY
  Tanggal,
  'total_reg'
ORDER BY
  Tanggal

and

SELECT
  DATE_FORMAT(`member_unsubscribe_at`, '%d-%m-%Y') Tanggal,
  COUNT(`member_msisdn`) 'total_unreg'
FROM
  `t_push_member` 
WHERE
  `service_keycode`='nocan'
GROUP BY
  Tanggal, 'total_unreg'
ORDER BY
  Tanggal

how to make 2 queries into one query ?

3
  • You can use sub query to get it done in One query. Commented May 13, 2013 at 10:56
  • What do you want the result to look like? Commented May 13, 2013 at 11:01
  • i want tanggal, reg, unreg in one result Commented May 13, 2013 at 11:19

2 Answers 2

1
select * 
from 
( SELECT DATE_FORMAT(member_subscribe_at, '%d-%m-%y') Tanggal, count(member_msisdn),'REG' as Registered FROM t_push_member 
    where service_keycode='nocan' 
    group by Tanggal  

    UNION

    SELECT DATE_FORMAT(member_unsubscribe_at, '%d-%m-%Y') Tanggal, count(member_msisdn),'NOT_REG' as Registered FROM t_push_member 
    where service_keycode='nocan'
    group by Tanggal
)
order by  Tanggal

Learn more about UNION at http://www.w3schools.com/sql/sql_union.asp

Sign up to request clarification or add additional context in comments.

8 Comments

@Maliq : in UNION column names also have to be the same in all the queries that return columns. See the above updated answer. It will work.
this my query SELECT ( SELECT count( * ) FROM t_push_member WHERE service_keycode = 'NOCAN' AND member_unsubscribe_at <> '0000-00-00 00:00:00' ) AS 'Member UNREG', ( SELECT count( * ) FROM t_push_member WHERE service_keycode = 'NOCAN' AND member_unsubscribe_at = '0000-00-00 00:00:00' ) AS 'Member REG' result only member reg and member unreg and I want to add a group by date
@Maliq :Pl see the updated answer. There is already group by date in both of the queries and if you want to add any other group by then you can add it before order by. Hope this helps.
thank you very much, its work!!. but I had to modify again for other purposes
But it's not a very good answer ;-) ... although, to be fair, it's hard to provide a more authoritative one without seeing the actual DDLs.
|
0

use "union" between the two - Instead of and

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.