Hi I have below 5 mysql queries, i want to see output of all queries in by executing a single query.
please let me know how can i join all these queries.
1.
select sum(msu)
from wgr_raw
where dt
between '2013-11-01' and '2013-11-07';
2.
select *
from wgr_raw
where dt
between '2013-11-01' and '2013-11-07'
order by dt;
3.
select admin,sum(msu)
from wgr_raw
where dt
between '2013-11-01' and '2013-11-07'
group by admin;
4.
SELECT admin, sum(msu)
from wgr_raw
where dt
between '2013-11-01' and '2013-11-07'
group by admin
order by msu desc
limit 25;
Select ADMIN1,WEEK1,WEEK2,WEEK3,
(((WEEK3-WEEK2)/WEEK2)*100) as percentage_change,
(WEEK3-WEEK2) as MSU_Difference
from
((select admin as ADMIN1, sum(msu) as WEEK1
from wgr_raw
where dt >= date_sub(date('2013-11-01'),
INTERVAL 25 DAY) and dt < date_sub(date('2013-11-07'),
INTERVAL 18 DAY)
group by admin
order by WEEK1) as q1,
(select admin as ADMIN2, sum(msu) as WEEK2
from wgr_raw
where dt >= date_sub(date('2013-11-01'),
INTERVAL 18 DAY) and dt < date_sub(date('2013-11-07'),
INTERVAL 11 DAY)
group by admin
order by WEEK2) as q2,
(select admin as ADMIN3, sum(msu) as WEEK3
from wgr_raw where dt >= date_sub(date('2013-11-01'),
INTERVAL 11 DAY) and dt < date_sub(date('2013-11-07'),
INTERVAL 4 DAY)
group by admin
order by WEEK3) as q3)
where ADMIN1=ADMIN3 and ADMIN2=ADMIN3
group by admin1
order by WEEK1 desc
limit 25;
5.
select dt,sum(msu)
from wgr_raw
where dt >= date_sub(date('2013-11-01'),
INTERVAL 11 DAY)
group by dt
limit 7;
You can merge n number of select queries using UNION but there is one condition in UNION. Your all select fields should be same and order by will be common.
(
(SELECT a, b FROM tbl WHERE id >= 50 and id <= 100)
UNION
(SELECT a, b FROM tbl WHERE id >= 150 and id <= 200)
UNION
(SELECT a, b FROM tbl WHERE id >= 250 and id <= 300)
UNION
(SELECT a, b FROM tbl WHERE id >= 350 and id <= 400)
)
ORDER BY a
Related
I have Query 1
SELECT COUNT(DISTINCT user_id) total_daily_active_user_group_month FROM (SELECT user_id , MONTHNAME(time) mon , COUNT(*) cnt FROM ACTIVITIES
WHERE MONTH(time) = MONTH(NOW() - INTERVAL 1 MONTH) GROUP by user_id, MONTH(time) ) as x
Returns 18
Query 2
SELECT COUNT(DISTINCT user_id) total_daily_active_user_group_month FROM (SELECT user_id , MONTHNAME(time) mon , COUNT(*) cnt FROM ACTIVITIES
WHERE MONTH(time) = MONTH(NOW() - INTERVAL 1 MONTH) GROUP by user_id, MONTH(time) having cnt=31) as x
Return 6
I want the ratio of query 1 and two. Means
18/6 . I am using MySQL
If you use both queries as CTEs, then it becomes relatively simple:
WITH q1
AS (SELECT Count(DISTINCT user_id) total_daily_active_user_group_month
FROM (SELECT user_id,
Monthname(TIME) mon,
Count(*) cnt
FROM activities
WHERE Month(TIME) = Month(Now() - interval 1 month)
GROUP BY user_id,
Month(TIME))),
q2
AS (SELECT Count(DISTINCT user_id) total_daily_active_user_group_month
FROM (SELECT user_id,
Monthname(TIME) mon,
Count(*) cnt
FROM activities
WHERE Month(TIME) = Month(Now() - interval 1 month)
GROUP BY user_id,
Month(TIME)
HAVING cnt = 31))
SELECT q1.total_daily_active_user_group_month /
q2.total_daily_active_user_group_month
AS result
FROM dual;
You commented that you got an error pointing to the WITH keyword; switch to two subqueries, then; simplified:
select a.value / b.value as result
from (select count(distinct user_id) value
from ... your 1st query goes here
) a,
(select count(distinct user_id) value
from ... your 2nd query goes here
) b;
I have table called user_info
It has two columns:
User_id
Date
How do I get to the table like the one mentioned below:
----------------------------------------------------------------
Date |total_number_of_users | D2 |D5 | D7 | D14|
--------------------------------------------------------------
2020-07-01 1000 700 500 200 150
2020-07-02 400 300 250 200 100
For example consider the first row in the table I am trying to achieve:
total_number_of_users = Total number of users who have visited the site on 2020-07- 01
D2=Out of total users who visited the site on 2020-07-01, visited on 2020-07-2
D7=Out of total users who visited the site on 2020-07-01, visited on 2020-07-7
I have tried the following, how do I get the exact the solution:
SELECT user_id, week(login_date) AS login_week
FROM user_info
GROUP BY user_id,week(login_date);
SELECT user_id, min(week(login_date)) AS first_week
FROM user_info
GROUP BY user_id;
select a.user_id,a.login_week,b.first_week as first_week from
(SELECT
user_id,
week(login_date) AS login_week
FROM user_info
GROUP BY user_id,week(login_date)) a,
(SELECT
user_id,
min(week(login_date)) AS first_week
FROM user_info
GROUP BY user_id) b
where a.user_id=b.user_id;
This seems painful, but you can use a self-join and aggregation:
select t.date,
sum( t2.date = t.date) as total_number_of_users,
sum( t2.date = t.date + interval 1 day ) as d2,
sum( t2.date = t.date + interval 4 day ) as d5,
sum( t2.date = t.date + interval 6 day ) as d7,
sum( t2.date = t.date + interval 13 day ) as d14
from (select distinct date, user_id
from t
) t1 left join
(select distinct date, user_id
from t
) t2
on t1.user_id = t2.user_id and
t2.date in (t1.date, t1.date + interval 1 day, t1.date + interval 4 day, t1.day + interval 6 day, t1.day + interval 13 day)
group by t.date;
The below mysql question returns only the 10 first rows. How can I limit the them to 10% of all?
SELECT page,
poso,
diff
FROM (SELECT page,
Count(*) AS poso,
( Sum(Date(timestamp) = Curdate()) - Sum(
Date(timestamp) = Date_sub(Curdate(),
INTERVAL 1 day)) )
diff
FROM `behaviour`
WHERE Date(timestamp) >= Date_sub(Curdate(), INTERVAL 1 day)
GROUP BY page
ORDER BY ( Sum(Date(timestamp) = Curdate()) - Sum(
Date(timestamp) = Date_sub(Curdate(),
INTERVAL 1 day))
) DESC
LIMIT 10) AS u
ORDER BY diff DESC
Adapted from the answer to the duplicate question:
SELECT page,
poso,
diff
FROM (
SELECT *,
#counter := #counter + 1 AS counter
FROM (select #counter:=0) AS initvar,
(SELECT page,
Count(*) AS poso,
( Sum(Date(timestamp) = Curdate()) - Sum(
Date(timestamp) = Date_sub(Curdate(),
INTERVAL 1 day)) )
diff
FROM `behaviour`
WHERE Date(timestamp) >= Date_sub(Curdate(), INTERVAL 1 day)
GROUP BY page
ORDER BY ( Sum(Date(timestamp) = Curdate()) - Sum(
Date(timestamp) = Date_sub(Curdate(),
INTERVAL 1 day))
) DESC) AS u
) AS v
WHERE counter <= 10/100 * #counter
ORDER BY diff DESC;
Demo here: http://rextester.com/JKMBZR62923
I have a problem that i cant solve with a SQL query for MySQL.
First table "content"
ID / Title / Author / Curated / Created
Next table "whitelist"
ID / Author / Weight
What i want the SQL to do is SELECT all content WHERE content.created >= UNIX_TIMESTAMP(DATE_SUB(now(), INTERVAL 14 day)) Also i what to order the Author random of the weight so the author with a high weight will show up more than a lower. As for now i am using this function ORDER BY -LOG(RAND(1337)) / whitelist.weight ASC. And finally i want every other row to be curated and the next one not.
So the result would be something like this.
ID Title Author Curated Created
3 My Home1 Krister 1 2015-01-20
13 My Home14 Krister 0 2015-01-20
33 My Home8 Eva 1 2015-01-15
34 My Home11 Krister 0 2015-01-01
43 My Home18 Eva 1 2015-01-01
What i have tried...
SELECT *
FROM (
SELECT `content`.*, IF(`content`.`curated`=0, #mr:=#mr+1, #fr:=#fr+1) AS cur
FROM `content` INNER JOIN `whitelist` ON `content`.`author` = `whitelist`.`author` , (SELECT #mr:=0, #fr:=0) initvars
WHERE
content.is_deleted = 0
AND content.created >= UNIX_TIMESTAMP(DATE_SUB(now(), INTERVAL 14 day))
) tmp
INNER JOIN whitelist ON tmp.author = whitelist.author
ORDER BY cur ASC LIMIT 5 OFFSET 0;
Try this:
SELECT * FROM content
JOIN whitelist ON content.Author = whitelist.Author
WHERE content.created >= UNIX_TIMESTAMP(DATE_SUB(now(), INTERVAL 14 day))
ORDER BY (RAND() * (SELECT MAX(Weight) FROM whitelist)) + Weight DESC
Now with curated:
SET #pos1=0;
SET #pos2=0;
SELECT * FROM
(
SELECT *, #pos1 := #pos1 + 1 pos FROM
(
SELECT Content.* FROM content
JOIN whitelist ON content.Author = whitelist.Author
WHERE curated = 0
AND content.created >= UNIX_TIMESTAMP(DATE_SUB(now(), INTERVAL 14 day))
ORDER BY (RAND() * (SELECT MAX(Weight) FROM whitelist)) + Weight
) s1
UNION
SELECT *, #pos2 := #pos2 + 1 pos FROM
(
SELECT Content.* FROM content
JOIN whitelist ON content.Author = whitelist.Author
WHERE curated = 1
AND content.created >= UNIX_TIMESTAMP(DATE_SUB(now(), INTERVAL 14 day))
ORDER BY (RAND() * (SELECT MAX(Weight) FROM whitelist)) + Weight
) s2
) t1
ORDER BY pos DESC, Curated
This my Query
SELECT COUNT(*) as total, toys, date FROM T1
WHERE (date >= '2012-06-26'AND date < '2012-06-30') AND (Avail > '0')
UNION
SELECT COUNT(*) as total, toys, date FROM T2
WHERE (date >= '2012-06-26'AND date < '2012-06-30') AND (Avail > '0')
UNION
SELECT COUNT(*) as total, toys, date FROM T3
WHERE (date >= '2012-06-26'AND date < '2012-06-30') AND (Avail > '0')
GROUP BY RoomType
HAVING COUNT( total ) = 4
Output result
count Toys date
3 Bibi 2012-06-26
4 Baba 2012-06-26
How can i get MYSQL to show results only for count=4
SELECT * FROM (
SELECT COUNT(*) as total, toys, date FROM T1
WHERE (date >= '2012-06-26' AND date < '2012-06-30') AND (Avail > '0')
GROUP BY RoomType
UNION
SELECT COUNT(*) as total, toys, date FROM T2
WHERE (date >= '2012-06-26' AND date < '2012-06-30') AND (Avail > '0')
GROUP BY RoomType
UNION
SELECT COUNT(*) as total, toys, date FROM T3
WHERE (date >= '2012-06-26'AND date < '2012-06-30') AND (Avail > '0')
GROUP BY RoomType) AS src
WHERE total = 4;
Please, note, that for proper data groupping you must have all columns either in the GROUP BY clause or as arguments to the aggregate functions. It is MySQL feature to allow you to avoid this restriction, but it might lead you to the unexpected results.
You need group by first then union the result.
SELECT COUNT(*) as total, toys, date FROM T1
WHERE (date >= '2012-06-26'AND date < '2012-06-30') AND (Avail > '0')
GROUP BY RoomType HAVING COUNT( *) = 4
UNION
SELECT COUNT(*) as total, toys, date FROM T2
WHERE (date >= '2012-06-26'AND date < '2012-06-30') AND (Avail > '0')
GROUP BY RoomType HAVING COUNT( * ) = 4
UNION
SELECT COUNT(*) as total, toys, date FROM T3
WHERE (date >= '2012-06-26'AND date < '2012-06-30') AND (Avail > '0')
GROUP BY RoomType HAVING COUNT( * ) = 4