I have data like this:
select * from date_table;
startdate
2018-08-22
2018-08-24
2018-08-27
2018-08-29
2018-08-31
2018-09-05
2018-09-07
2018-09-10
I have written this query, which only gives days difference
CREATE temporary TABLE if not exists results AS (
select t.startdate, datediff(
(select min(t1.startdate)
from
date_table t1 where
t1.startdate>t.startdate),
t.startdate ) days_diff
from
date_table t) ;
select * from results:
Above query gives result as:
startdate days_diff
2018-08-22 2
2018-08-24 3
2018-08-27 2
2018-08-29 2
2018-08-31 5
2018-09-05 2
2018-09-07 3
2018-09-10
But I want result like:
startdate enddate days_diff
2018-08-22 2018-08-24 2
2018-08-27 2018-08-29 2
2018-08-31 2018-09-05 5
2018-09-07 2018-09-10 3
I'm using MySQL(version 5.6). Kindly let me know if there is any solution for this problem.
Thanks in advance.
You appear to want to enumerate the rows and then aggregate. In pre-8.0 versions, you can use variables. The rest is then aggregation:
select min(start_date) as start_date, max(start_date) as end_date
from (select (#rn := #rn + 1) as seqnum, t.*
from (select t.* from date_table t order by start_date) t cross join
(select #rn := 0) params
) t
group by floor((seqnum - 1) / 2);
Related
I have table orders:
id
login_name
success
order_date
1
login1
0
2021-01-05
2
login2
0
2021-01-06
3
login3
0
2021-01-08
4
login1
1
2021-01-04
5
login2
0
2021-01-01
I need to select id, login_name with success=0 for which exist another order with order_date older or younger than 60 days.
The result should be:
1 - login1, 2 - login2, 5 - login2
I have this, but I think that is not a right way:
SELECT id, login_name, COUNT(*)
FROM orders
WHERE success=0
GROUP BY login_name
HAVING COUNT(*) > 1
You can use the EXISTS as follows:
SELECT id, login_name, COUNT(*)
FROM orders r
WHERE success=0
and exists
(select 1 from orders rr
where rr.login = r.login
and abs(datediff(rr.order_date, r.order_date)) <= 60
and rr.id <> r.id
)
If you want orders that appear within 60 days of each other, you can use lag() and lead():
select o.*
from (select o.*,
lag(order_date) over (partition by login_name order by order_date) as prev_order_date,
lead(order_date) over (partition by login_name order by order_date) as lead_order_date
from orders o
) o
where prev_order_date > dateadd(day, -60, order_date) or
next_order_date < dateadd(day, 60, order_date);
I have this query where I calculated cumulative sum. Now, I need to calculate reverse cumulative sum for the same variable
SELECT t1.date, t1.ant, t1.hab,
(#csum:= #csum + t1.hab) as cumulative_hab
from(
SELECT date,
ant,
sum(num_habit) as hab
from xxxxxxxxxx
WHERE date BETWEEN CURDATE() - INTERVAL 5 DAY AND CURDATE()
group by ant) AS t1
,(select #csum := 0) vars
order by t1.ant
My table look like this
date ant hab cumulative_hab
24-05-2020 0 382,000 382,000
24-05-2020 1 28,000 410,000
24-05-2020 2 26,000 436,000
24-05-2020 3 11,000 447,000
24-05-2020 4 29,000 476,000
24-05-2020 6 6,000 482,000
24-05-2020 7 12,000 494,000
28-05-2020 8 50,000 544,000
24-05-2020 12 5,000 549,000
24-05-2020 13 6,000 555,000
I would like another column with reverse running sum (reverse cumulative sum), the first value is calculated 555 - 382
date ant hab cumulative_hab reverse_cum_hab
24-05-2020 0 382,000 382,000 555,000
24-05-2020 1 28,000 410,000 173,000,
24-05-2020 2 26,000 436,000 145,000
24-05-2020 3 11,000 447,000 119,000
24-05-2020 4 29,000 476,000 108,000
24-05-2020 6 6,000 482,000 79,000
24-05-2020 7 12,000 494,000 73,000
28-05-2020 8 50,000 544,000 61,000
24-05-2020 12 5,000 549,000 11,000
24-05-2020 13 6,000 555,000 6,000
As a starter: if you are running MySQL 8.0, you can do this easily with window functions:
select
date,
ant,
sum(num_habit) as hab,
sum(sum(num_habit)) over(order by date) cumulative_hab,
sum(sum(num_habit)) over(order by date desc) reverse_cumulative_hab
from mytable
where date between current_date - interval 5 day and current_date
group by date, ant
order by date
In earlier versions, it is more complicated. I would suggest joining two queries:
select t.*, r.reverse_cumulative_hab
from (
select t.*, #csum := #csum + hab cumulative_hab
from (
select date, ant, sum(num_habit) as hab
from mytable
where date between current_date - interval 5 day and current_date
group by date, ant
order by date
) t
cross join (select #csum := 0) x
) t
inner join (
select t.*, #rcsum := #rcsum + hab reverse_cumulative_hab
from (
select date, ant, sum(num_habit) as hab
from mytable
where date between current_date - interval 5 day and current_date
group by date, ant
order by date desc
) t
cross join (select #rcsum := 0) x
) r on r.date = t.date
order by t.date
This assumes no duplicate ant per date.
It might also be possible to simplify the logic and compute the reverse sum by taking the difference between the cumulative sum and the overall sum:
select t.*, z.total_hab - t.cumulative_hab reverse_cumulative_hab
from (
select t.*, #csum := #csum + hab cumulative_hab
from (
select date, ant, sum(num_habit) as hab
from mytable
where date between current_date - interval 5 day and current_date
group by date, ant
order by date
) t
cross join (select #csum := 0) x
) t
cross join (
select sum(num_habit) as total_hab
from mytable
where date between current_date - interval 5 day and current_date
) z
order by date
Note that these queries are safer than your original code in regard of ordering of the rows: records are ordered in a subquery before the cumulative sum is computed.
This question already has answers here:
generate days from date range
(30 answers)
Closed 3 years ago.
I have a following table with columns:
id | number | created_at
1 | A11 | 2020-01-01 06:08:19
2 | A21 | 2020-01-04 06:08:19
How do I query all the data in a date range from specific date and count all data per day?
I tried something like that :
SELECT DATE_FORMAT(created_at, '%Y-%m-%d') AS the_date , COUNT(*) AS count
FROM `transactions`
WHERE created_at BETWEEN DATE_FORMAT('2020-01-01', '%Y-%m-%d') AND DATE_FORMAT('2020-01-04', '%Y-%m-%d')
GROUP BY the_date
Then i got data like that :
the_date | count
2020-01-01 | 1
2020-01-04 | 1
I want to achieve
the_date | count
2020-01-01 | 1
2020-01-02 | 0
2020-01-03 | 0
2020-01-04 | 1
if your version is below mysql 8.0 then you can use this script :
step1 : create a sequence N rows table :
create table sequence(id int);
create procedure insert_data_proc(in v_i int)
begin
declare i int default 0;
while i < v_i
do
insert into sequence values (i);
set i = i + 1;
end while;
end;
call insert_data_proc(1000);
drop procedure insert_data_proc;
step2 : query the table and left join your table's by mindate,maxdate,datediff
select
t1.created_at the_date
,case when count is null then 0 else count end as count
from (
select date_add(t2.mincreated_at , interval id day) created_at
from sequence t1
left join (
select datediff(max(created_at),min(created_at)) diff
,date(min(created_at) ) mincreated_at
,date(max(created_at) ) maxcreated_at
from transactions
) t2 on 1=1
where t1.id < t2.diff+1
) t1
left join (
select date(created_at) created_at,count(1) count
from transactions
group by date(created_at)
) t2 on t1.created_at = t2.created_at
order by the_date
note : if your data's days over 1000 day then you only need to increase the SP value.
[Online Demo Link MySQL 5.7 | db<>fiddle](https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=57d3e60bb2b918e8b6d2d8f3d5e63a6c
)
you can use like this :
SET #date_min = '2019-01-01';
SET #date_max = '2019-01-04';
SELECT
date_generator.date as the_date,
IFNULL(COUNT(transactions.id), 0) as count
from (
select DATE_ADD(#date_min, INTERVAL (#i:=#i+1)-1 DAY) as `date`
from information_schema.columns,(SELECT #i:=0) gen_sub
where DATE_ADD(#date_min,INTERVAL #i DAY) BETWEEN #date_min AND #date_max
) date_generator
left join transactions on DATE(created_at) = date_generator.date
GROUP BY date;
so here I am creating a temporary table date_generator will dates in between of given date range and join to with your main table (transactions).
output as expected:
the_date | count
2020-01-01 | 1
2020-01-02 | 0
2020-01-03 | 0
2020-01-04 | 1
I will give a suggestion for you to do this,
1 Solution
Create temporary table and add the dates and then join with the transactions table
create temporary table tempcalander
as
select * from
(select adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) dates from
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
where dates between '2020-01-01' and '2020-01-04';
SELECT DATE_FORMAT(dates, '%Y-%m-%d') AS the_date , COUNT(created_at) AS count
FROM transactions t right join tempcalander c on t.created_at = c.dates
WHERE dates BETWEEN DATE_FORMAT('2020-01-01', '%Y-%m-%d') AND DATE_FORMAT('2020-01-04', '%Y-%m-%d')
GROUP BY the_date
2 Solution
you can create a separate table to add your dates.
CREATE TABLE calendar
(
dates date PRIMARY KEY
) ;
Then add you dates to this table,
INSERT INTO
calendar (dates)
VALUES
('2020-01-01'),
('2020-01-02'),
('2020-01-03'),
('2020-01-04'),
('2020-01-05'),
('2020-01-06') ;
after you can join the the transactions table with the calendar table and get the output
SELECT DATE_FORMAT(dates, '%Y-%m-%d') AS the_date , COUNT(created_at) AS count
FROM transactions t right join calendar c on t.created_at = c.dates
WHERE dates BETWEEN DATE_FORMAT('2020-01-01', '%Y-%m-%d') AND DATE_FORMAT('2020-01-04', '%Y-%m-%d')
GROUP BY the_date
I have table as below
name price date
soap 10 2013-09-18
soap 10 2013-09-18
pens 8 2013-09-18
deos 7 2013-09-18
book 13 2013-09-17
book 13 2013-09-17
soap 10 2013-09-17
pens 8 2013-09-17
Based on the above data , i would like to calculate the rank of total selling per item ( based on price as below for today and previous day.
name totalselling date todayrank previousdayrank
soap 20 2013-09-18 1 2
pens 8 2013-09-18 2 2
deos 7 2013-09-18 3 -
if the todays item doesnt exit in yesterday ,then its ranking in previous day should be null.
You will have to use subselects, date intervals, some variables, a case and some brainwork.
Mix all this together and you will get something like this:
SELECT today.*,
CASE
WHEN yesterday.yesterdayrank IS NULL THEN '-'
ELSE yesterday.yesterdayrank
END
FROM (SELECT #i:=#i +1 AS todayrank,
name,
SUM(price) AS price
FROM test,
(SELECT #i:= 0) AS foo
WHERE createdate = DATE (NOW()) - INTERVAL 2 DAY
GROUP BY name
ORDER BY todayrank) today
LEFT JOIN (SELECT #j:= #j +1 AS yesterdayrank,
name,
SUM(price) AS price
FROM test,
(SELECT #j:= 0) AS bar
WHERE createdate = DATE (NOW()) - INTERVAL 3 DAY
GROUP BY name
ORDER BY yesterdayrank) yesterday ON today.name = yesterday.name
I hope this helps.
SELECT a.name,
a.todaysales,
a.date,
a.rank AS ranktoday,
b.rank AS rankyesterday
FROM
(SELECT name, sum(price) AS todaysales, date, #n := #n + 1 AS rank
FROM TABLE , (SELECT #n := 0) alias
WHERE date=curdate() group by 1 order by count(*))a
INNER JOIN
(SELECT name, sum(price) AS todaysales, date, #n := #n + 1 AS rank
FROM TABLE , (SELECT #n := 0) alias
WHERE date=curdate() - interval 1 DAY group by 1 order by count(*))b ON a.name=b.name
I have the following data of a particular user -
Table temp -
time_stamp
2015-07-19 10:52:00
2015-07-18 10:49:00
2015-07-12 10:43:00
2015-06-08 12:32:00
2015-06-07 11:33:00
2015-06-06 10:05:00
2015-06-05 04:17:00
2015-04-14 04:11:00
2014-04-02 23:19:00
So the output for the query should be -
Maximum streak = 4, Current streak = 2
Max streak = 4 because of these -
2015-06-08 12:32:00
2015-06-07 11:33:00
2015-06-06 10:05:00
2015-06-05 04:17:00
And current streak is 2 because of these (Assuming today's date is 2015-07-19)-
2015-07-19 10:52:00
2015-07-18 10:49:00
EDIT: I want a simple SQL query for MYSQL
For MAX streak(streak) you can use this, I have use the same query to calculate max streak. This may help you
SELECT *
FROM (
SELECT t.*, IF(#prev + INTERVAL 1 DAY = t.d, #c := #c + 1, #c := 1) AS streak, #prev := t.d
FROM (
SELECT date AS d, COUNT(*) AS n
FROM table_name
group by date
) AS t
INNER JOIN (SELECT #prev := NULL, #c := 1) AS vars
) AS t
ORDER BY streak DESC LIMIT 1;
A general approach with the gaps and islands queries is to tag each row with its rank in the data and with its rank in the full list of dates. The clusters will all have the same difference.
Caveats: I don't know if this query will be efficient. I don't remember if MySQL allows for scalar subqueries. I didn't look up the way to calculate a day interval in MySQL.
select user_id, max(time_stamp), count(*)
from (
select
t.user_id, t.time_stamp,
(
select count(*)
from T as t2
where t2.user_id = t.user_id and t2.time_stamp <= t.time_stamp
) as rnk,
number of days from t.time_stamp to current_date as days
from T as t
) as data
group by usr_id, days - rnk