Select from two tables with group by date - mysql

I have two tables:
Table t1:
id | date_click
1 | 2016-02-31 17:17:23
2 | 2016-03-31 12:11:21
3 | 2016-03-31 13:13:23
So from this table I want to get count field Id for each day.
For this I use next query:
SELECT date_format(date_click, '%Y-%m-%d') as date_click_event
, COUNT(id) as count_click
FROM t1
GROUP
BY date_click_event
ORDER
BY date_click_event DESC;
It's work good.
So next table is t2.
id | count | date_sent
1 | 33 | 2016-02-31 11:12:23
2 | 22 | 2016-03-31 14:11:22
3 | 11 | 2016-03-31 13:12:13
To select data by date from this table I use next query:
SELECT date_format(date_sent, '%Y-%m-%d') as date_sent_push
, SUM(count) as count_sent
FROM t2
GROUP
BY date_sent_push
ORDER
BY date_sent_push DESC
LIMIT 100;
It's also work good. So my purpose is merge these two queries into one SELECT that next I can write in php one tables with count of Id by date from table t1 and with count of count field from table t2 by date.
When I try next query:
SELECT date_format(t2.date_sent, '%Y-%m-%d') AS date_sent_push
, SUM(t2.count) as count_sent
, COUNT(t1.id) as count_click
FROM t2
, t1
WHERE date_format(t2.date_sent, '%Y-%m-%d') = date_format(t1.date_click, '%Y-%m-%d')
GROUP
BY date_sent_push
ORDER
BY date_sent_push
DESC
LIMIT 100;
It's not work. What I do wrong?

First you should UNION these results and then group by days and select aggregate fields. Also you can JOIN these queries but it can be a problem if some days miss in one of two tables:
SELECT date_sent_push,
MAX(count_click) as count_click,
MAX(count_sent) as count_sent
FROM
(SELECT date_format(date_click, '%Y-%m-%d') as date_sent_push
, COUNT(id) as count_click
, NULL as count_sent
FROM t1
GROUP BY date_sent_push
UNION ALL
SELECT date_format(date_sent, '%Y-%m-%d') as date_sent_push
, NULL as count_click
, SUM(count) as count_sent
FROM t2
GROUP
BY date_sent_push
) as t3
GROUP BY date_sent_push
SQL fiddle demo

use this code
(SELECT date_format(date_sent, '%Y-%m-%d') as date_sent_push, SUM(count) as count_sent FROM t2 GROUP BY date_sent_push ORDER BY date_sent_push DESC LIMIT 100)
UNION
(SELECT date_format(t2.date_sent, '%Y-%m-%d') AS date_sent_push, SUM(t2.count) as count_sent, COUNT(t1.id) as count_click FROM t2, t1 WHERE date_format(t2.date_sent, '%Y-%m-%d')=date_format(t1.date_click, '%Y-%m-%d') GROUP BY date_sent_push ORDER BY date_sent_push DESC LIMIT 100)

Related

MySQL - Count and GroupBy

I have this structure:
id| date_1 | date_2
---------------------
01|2017-01-01|2017-02-22
02|2017-01-02|2017-03-25
03|2017-02-10|2017-03-20
04|2017-03-11|2017-04-10
05|2017-03-15|2017-05-01
06|2017-03-20|2017-05-20
I would need this kind of result:
Month |Count(date_1)|Count(date_2)
---------------------------------
2017-01| 2 | 0
2017-02| 1 | 1
2017-03| 3 | 2
2017-04| 0 | 1
2017-05| 0 | 2
Now, I use this query (it works with only one date):
SELECT CONCAT(YEAR(date_1), '-', DATE_FORMAT(date_1,'%m')) AS month,
COUNT(*) AS items
FROM table
GROUP BY YEAR(date_1), MONTH(date_1)
ORDER BY date_1 DESC
You could union all the date values and then group and count them:
SELECT DATE_FORMAT(d, '%y-%m'), COUNT(*)
FROM (SELECT date_1 AS d FROM mytable
UNION ALL
SELECT date_2 FROM mytable) t
GROUP BY DATE_FORMAT(d, '%y-%m')
ORDER BY d DESC
To get the count of date_1 and date_2 in two different fields, with sub query:
SELECT DATE_FORMAT(temp1.d, '%y-%m'), COALESCE(d1count,0) AS date_1_count, COALESCE(d2count,0)AS date_2_count
FROM (
select date_1 as d from dates group by date_1
union all
select date_2 as d from dates group by date_2
) as temp1
LEFT JOIN (
select date_1, count(*) as d1count
from dates
group by DATE_FORMAT(date_1, '%y-%m')) as temp2
on DATE_FORMAT(temp2.date_1, '%y-%m') = DATE_FORMAT(temp1.d, '%y-%m')
LEFT JOIN (
select date_2, count(*) as d2count
from dates
group by DATE_FORMAT(date_2, '%y-%m')) as temp3
on DATE_FORMAT(temp3.date_2, '%y-%m') = DATE_FORMAT(temp1.d, '%y-%m')
GROUP BY DATE_FORMAT(temp1.d, '%y-%m')
Consider using subqueries behind SELECT
SELECT distinct DATE_FORMAT(t.d, '%y-%m'),
(
SELECT count(*)
FROM your_table as dd
where DATE_FORMAT(dd.date_1, '%y-%m') = DATE_FORMAT(t.d, '%y-%m')
) as count_date_1,
(
SELECT count(*)
FROM your_table as dd
WHERE DATE_FORMAT(dd.date_2, '%y-%m') = DATE_FORMAT(t.d, '%y-%m')
) as count_date_2
FROM
(
SELECT date_1 AS d FROM your_table
UNION ALL
SELECT date_2 as d FROM your_table
) as t
dbfiddle demo

To find the maximum number of order count that occur in any 1 hour of the day from the database?

I have a food selling website in which there is order table which record the order of every user.It column for user id ,user name,orderid ,timestamp of order.I want to know the maximum number of order that has been made in any one hour span through out the day.Give me any formula for this,or any algorithm or any sql queries for these.
SQL server:
with CTE as
(
select cast(t1.timestamp as date) as o_date, datepart(hh, t1.timestamp) as o_hour, count(*) as orders
from MyTable t1
group by cast(t1.timestamp as date), datepart(hh, t1.timestamp)
)
select o_date, o_hour, orders
from CTE
where orders = (select max(orders) from CTE)
Oracle
with CTE as
(
select to_char(t1.timestamp, 'YYYYMMDD') as o_date, to_char(t1.timestamp, 'HH24') as o_hour, count(*)
from MyTable t1
group by to_char(t1.timestamp, 'YYYYMMDD'), to_char(t1.timestamp, 'HH24')
)
select o_date, o_hour, orders
from CTE
where orders = (select max(orders) from CTE)
You can get count by day and hour like this
For SQL
SELECT TOP 1
COUNT(*)
FROM myTable
GROUP BY DATEPART(day, [column_date]), DATEPART(hour, [column_date])
ORDER BY COUNT(*) DESC;
For MySQL
SELECT
COUNT(*)
FROM myTable
GROUP BY HOUR(column_date), DAY(column_date)
ORDER BY COUNT(*) DESC
LIMIT 1;

Aggregate data by last n days for each row in MySQL

I have data in below format -
Date Amount
1/1/2000 1
2/1/2000 1
3/1/2000 1
4/1/2000 2
5/1/2000 1
6/1/2000 1
7/1/2000 1
Here, each row represents Amount collected on that day, I want to prepare a table where each row represents amount collected on last 3 days. Thus for 3/1/2000 - it will show amount =3 ( Amount 1 on 1/1 , 1 on 2/1 and 1 on 3/1 , so 1+1+1 = 3
So, from above data, table I want is -
Date Amount
1/1/2000 1 //1
2/1/2000 2 //1+1
3/1/2000 3 //1+1+1
4/1/2000 4 //1+1+2
5/1/2000 4 //1+2+1
6/1/2000 4 //2+1+1
7/1/2000 3 //1+1+1
How to write a SQL query for this?
I tried this -
select date, sum(amount) as amount_sum
from SQLTABLE
where DATEDIFF(date,date)<=2
group by date
You can use correlated subqueries in order to get the Amount values of the two previous records:
SELECT `Date`,
Amount +
COALESCE((SELECT Amount
FROM mytable AS t2
WHERE t2.`Date` < t1.`Date`
ORDER BY `Date` DESC LIMIT 1), 0) +
COALESCE((SELECT Amount
FROM mytable AS t2
WHERE t2.`Date` < t1.`Date`
ORDER BY `Date` DESC LIMIT 1, 1), 0) AS Amount
FROM mytable AS t1
The above query works even if there are gaps between consecutive records.
Edit:
If there are no gaps between consecutive records, then you can use the following query:
SELECT `Date`,
COALESCE((SELECT SUM(Amount)
FROM mytable AS t2
WHERE t2.date <= t1.date AND DATEDIFF(t1.date,t2.date) <= 2
ORDER BY `Date` DESC LIMIT 1), 0) AS Amount
FROM mytable AS t1
This can be done by using a sub-select.
SELECT date,
(SELECT sum(amount)
from SQLTABLE t2
WHERE DATEDIFF(t1.date,t2.date) IN (0,1,2)) amount_sum
from SQLTABLE t1

Mysql complex query, group by with limit

I've the following table
store_visits: (store_id, city_id, date, visits, ...)
I want to select the maximum 5 stores ordered by visits.
SELECT X.*
FROM (
SELECT
store_id, SUM(visits) as sum_visits FROM store_visits
WHERE
(date <= '2014-06-28' AND date >= '2014-06-27')
AND
store_visits.city_id = 2
GROUP BY
store_id
ORDER BY
sum_visits desc
) X
LIMIT 5
I was wondering if there's a way to enhance the query to eleminate the temporary table and filesort.
Try this:
SELECT store_id, SUM(visits) AS sum_visits
FROM store_visits sv
WHERE sv.date <= '2014-06-28' AND sv.date >= '2014-06-27' AND sv.city_id = 2
GROUP BY store_id
ORDER BY sum_visits DESC LIMIT 5

Mysql self join with multiple order by

assume we have table
id, title, date
I need to build 1 query to:
select date = TODAY, order by id
select data < TODAY, order by date desc,
select data > TODAY, order by date asc,
I think you need to use UNION and sub queries:
SELECT * FROM (
SELECT *
FROM YourTable
WHERE Date(dateField) = Date(Now())
ORDER BY ID
) t1
UNION
SELECT * FROM (
SELECT *
FROM YourTable
WHERE dateField < Now()
ORDER BY dateField DESC
) t2
UNION
SELECT * FROM (
SELECT *
FROM YourTable
WHERE Date(dateField) > Now()
ORDER BY dateField
) t3
Here is a simplified SQL Fiddle example.
Good luck.