Get results from two time intervals - mysql

There is a query which brings back sales data for the last 7 days.
How to get the sales of the last 30 days as well (to see the sales for the last 7 days AND the last 30 days in the results)?
SELECT
a.row_id,
MAX(ad.new_value) - MIN(ad.new_value) AS sales7days
FROM
_audit a
LEFT JOIN _audit_data ad
ON a.audit_id = ad.audit_id
WHERE ad.col = 'sales'
AND a.triggered_datetime > NOW() - INTERVAL 7 DAY
GROUP BY a.row_id
ORDER BY sales7days DESC;

Perhaps with a CASE expression:
SELECT a.row_id
, MAX(case when a.triggered_datetime > NOW() - INTERVAL 7 DAY
then ad.new_value else NULL end)
- MIN(case when a.triggered_datetime > NOW() - INTERVAL 7 DAY
then ad.new_value else NULL end) AS sales7days
, MAX(case when a.triggered_datetime > NOW() - INTERVAL 30 DAY
then ad.new_value else NULL end)
- MIN(case when a.triggered_datetime > NOW() - INTERVAL 30 DAY
then ad.new_value else NULL end) AS sales30days
FROM _audit a, _audit_data ad
WHERE a.audit_id = ad.audit_id AND ad.col = 'sales'
GROUP BY a.row_id;

SELECT
d7.row_id,
d7.salesdays, d30.salesdays
FROM
(
Select a.row_id, MAX(ad.new_value) - MIN(ad.new_value) AS salesdays
From _audit a
LEFT JOIN _audit_data ad ON a.audit_id = ad.audit_id
WHERE ad.col = 'sales' AND a.triggered_datetime > NOW() - INTERVAL 7 DAY
GROUP BY a.row_id
) d7,
(
Select a.row_id, MAX(ad.new_value) - MIN(ad.new_value) AS salesdays
From _audit a
LEFT JOIN _audit_data ad ON a.audit_id = ad.audit_id
WHERE ad.col = 'sales' AND a.triggered_datetime > NOW() - INTERVAL 30 DAY
GROUP BY a.row_id
) d30
where d7.row_id = d30.row_id
ORDER BY sales7days DESC;
assume you want the same row id for both - and either value to show, you may or may not want to make it inner or outer joined and/or COALESCE the value fields (don't know enough about the data).

Related

QOQ growth Mysql

i have query
select a.`2021`,
b.`2022`,
a.product,
concat(ceil((b.`2022`-a.`2021`)/ a.`2021` * 100), '%') as growth
from ( SELECT SUM(total) as `2021`,
product,
sum
FROM table
WHERE YEAR(month) = 2021
AND case when day(CURRENT_DATE()) > 10
then QUARTER(month) = QUARTER(CURRENT_DATE() - INTERVAL 1 MONTH)
else QUARTER(month) = QUARTER(CURRENT_DATE() - INTERVAL 3 MONTH)
end
GROUP BY Product ,
YEAR(month) )a
JOIN ( SELECT SUM(total) as `2022`,
Product
FROM table
WHERE YEAR(month) = 2022
AND case when day(CURRENT_DATE()) > 10
then QUARTER(month) = QUARTER(CURRENT_DATE() - INTERVAL 1 MONTH)
else QUARTER(month) = QUARTER(CURRENT_DATE() - INTERVAL 3 MONTH)
end
GROUP BY Product ,
YEAR(month) ) b on a.Product=b.Product;
if the current date is not the end of the quarter then the data that appears is the data in the previous quarter period

How sum values in days intervals MySQL 5.7?

I have a server with MySQL 5.7.
I have two tables. First one t contains creating dates for each id. Second table t0 contains profit records day by day for each id.
I want to get columns with sums of profit for first and second 30 days for each id as well as for the first day.
SELECT t.created_at,
t.id,
sum(t1.profit) profit_1_week,
sum(t2.profit) profit_2_week,
sum(t3.profit) profit_1_day
FROM t
LEFT JOIN t0 t1 ON t.id = t.id
AND t1.inport_date BETWEEN t.created_at AND DATE_ADD(t.created_at, INTERVAL 30 DAY)
LEFT JOIN t0 t2 ON t.id = t.id
AND t2.inport_date BETWEEN DATE_ADD(t.created_at, INTERVAL 30 DAY) AND DATE_ADD(t.created_at, INTERVAL 60 DAY)
LEFT JOIN t0 t3 ON t.id = t.id
AND t3.inport_date BETWEEN t.created_at AND DATE_ADD(t.created_at, INTERVAL 1 DAY)
GROUP BY t.created_at,
t.id
ORDER BY t.created_at
This code runs but sums are wrong because sum of the first day much more then monthly. Where I'm wrong and how to fix it?
Your problem statement is not that clear, but based on your attempt, I suspect that you can do conditional aggregation:
select t.created_at, t.id,
sum(case when t0.inport_date >= t.created_at and t0.inport_date < t.created_at + interval 30 day then t0.profit else 0 end) profit_1,
sum(case when t0.inport_date >= t.created_at + interval 30 day and t0.inport_date < t.created_at + interval 60 day then t0.profit else 0 end) profit_2,
sum(case when t0.inport_date >= t.created_at and t0.inport_date < t.created_at + interval 1 day then t0.profit else 0 end) profit_3
from t
left join t0 on t0.id = t.id
group by t.created_at, t.id
order by t.created_at
The logic is to join just once, and then to use case expressions within the sum()s to choose which values should be taken into account.
I changed the date filtering logic to use half-open intervals rather than between, because it seems more relevant to me. You can adapt that as you wish according to your actual use case.

Rails and Kaminari pagination with certain priority per page

I have complex query that I wrote partly as MySQL database view and partly as ActiveRecord logic in Rails. Each record has it's own priority from 0-4 where 4 is top priority.
I'm using Kaminari for pagination and I'm wondering if there's a way to show per page sets of records with some extra rules:
Show all #4 priority rows on first page
Take per_page number and show priority 3 with this formula: 0.3*per_page
Then do the same with priority 2
Then if all 3 steps didn't produced 100% of per_page show the rest with priority 0 and 1
How could I achieve result by using Rails. Or is it better to implement it directly in SQL?
Here is sample of my db view:
select *
from (
select
s.id as source_id,
'Spree::Store' as source_type,
(case when (s.created_at >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY AND s.created_at < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY)
then
'new'
else
'old'
end) as sub_type,
1 as priority,
s.created_at as created_at,
s.updated_at as updated_at,
null as owner_id
from spree_stores as s
where s.image_id is not NULL and s.is_hidden = false
union
select
e.id as source_id,
'Event' as source_type,
(case
when (e.status = 1 and e.is_featured is false)
then
'live'
when (e.is_featured = true)
then
'featured'
else
case when (e.created_at >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY AND e.created_at < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY)
then
'new'
else
'old'
end
end) as sub_type,
(case
when (e.status = 1 or e.is_featured is true)
then
3
else
1
end) as priority,
e.created_at as created_at,
e.updated_at as updated_at,
null as owner_id
from events as e
where e.status >= 1 and e.expires_at >= curdate()
union
select
o.id as source_id,
'Spree::Order' as source_type,
(case when (o.created_at >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY AND o.created_at < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY)
then
'new'
else
'old'
end) as sub_type,
1 as priority,
o.created_at as created_at,
o.updated_at as updated_at,
o.user_id as owner_id
from spree_orders as o
where o.user_id is not NULL and o.share is true and o.state = 'complete' and o.completed_at is not NULL
union
select
p.id as source_id,
'Spree::Product' as source_type,
(case when (p.created_at >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY AND p.created_at < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY)
then
'new'
else
'old'
end) as sub_type,
1 as priority,
p.created_at as created_at,
p.updated_at as updated_at,
null as owner_id
from spree_products as p
join spree_variants as sv on (sv.product_id = p.id and sv.is_master = true)
join spree_assets as sa on (sa.viewable_id = sv.id and sa.viewable_type = 'Spree::Variant')
where p.deleted_at is NULL
group by p.id
) a
order by priority desc, created_at desc;
This is the result I'm getting (only few lines not all 200 results):
This sounds like more complex logic than Kaminari is built for and probably worth doing it yourself. Kaminari is certainly convenient for knocking out a quick pagination UI, but it really doesn't add a huge amount of value compared to rolling your own solution. You might be able to hack it to fit your needs, but that's probably more headache than just doing it yourself.
I'm also a little skeptical the complex algorithm you're wanting is really going to benefit users. Only you know that for sure, but you might want to consider a simple "score" or "rank" column and then just use Kaminari with a query sorted by score desc.

run multiple mysql queries as 1 query

im trying to take 3 sql queries and insert them into 1 table without getting the null value's and using a group by number as to not get duplicate numbers in the same column.
I have the issue where running query 1 leaves me with a bunch of null data values
and running query 2 doesnt group the numbers resulting in thousands of rows numbers only go up to 100
QUERY 1
insert into table ( number)
select number as 1day from table where date = CURDATE() - interval 1day group by number
insert into table ( number)
select number as 2day from table where date = CURDATE() - interval 1day group by number
insert into table ( number)
select number as 7day from table where date = CURDATE() - interval 1day group by number
so i try to run
QUERY 2
insert into table (number,number,number)
select
*
from
(select number as 1day from test.test where date = curdate() - interval 1 day group by
number) as 1day,
(select number as 2day from test.test where date > curdate() - interval 2 day group by
number) as 2day,
(select number as 7day from test.test where date > curdate() - interval 7 day group
by number) as 7day;
try the below:
insert into table (number,number,number)
select
table.1day,table.2day,table.7day
from
((select number from test.test where date = curdate() - interval 1 day group by
number) as 1day,
(select numberfrom test.test where date > curdate() - interval 2 day group by
number) as 2day,
(select number from test.test where date > curdate() - interval 7 day group
by number) as 7day) as table
select (case one.number when two.number then null else one.number end) as '1day',(case two.number <= third.number when true then (case one.number = two.number when true then null else two.number end) else (case one.number = two.number when false then null else two.number end) end) as '2day',(case (third.number < one.number and third.number = two.number) when true then null else third.number end) as '7day'
from (
(select x.number
from (
(select number,'1day' as 'type' from testtable where date = curdate() - interval 1 day group by number)
union all
(select number,'2day' as 'type' from testtable where date > curdate() - interval 2 day group by number)
union all
(select number,'7day' as 'type' from testtable where date > curdate() - interval 7 day group by number)) as x
where x.type='2day' order by x.number) as two,
(select x.number
from (
(select number,'1day' as 'type' from testtable where date = curdate() - interval 1 day group by number)
union all
(select number,'2day' as 'type' from testtable where date > curdate() - interval 2 day group by number)
union all
(select number,'7day' as 'type' from testtable where date > curdate() - interval 7 day group by number)) as x
where x.type='1day' order by x.number) as one,
(select x.number
from (
(select number,'1day' as 'type' from testtable where date = curdate() - interval 1 day group by number)
union all
(select number,'2day' as 'type' from testtable where date > curdate() - interval 2 day group by number)
union all
(select number,'7day' as 'type' from testtable where date > curdate() - interval 7 day group by number)) as x
where x.type='7day' order by x.number) as third
)
where ((one.number = two.number) or (one.number is null or two.number is null)) or
((third.number = two.number) or (two.number is null or third.number is null))

Is this possible with mysql?

First of all: sorry for the title, but maybe I will find a better one later.
I asked this some minutes ago, but since I was not able to describe what I want I try it again :)
Here is my table structure:
http://sqlfiddle.com/#!2/b25f9/37
The table is used to store user sessions.
Out of this I would like to generate a stacked bar chart that should show how many active users I have. My idea was that I group the users based on their online-times of the last days like this
Lets say its friday:
Group B: Users that were online thursday (and today)
Group C: Users that were not online thursday but wednesday (and today)
Group D: Users that were not online thursday or wednesday but tuesday (and today)
Group E: Users that were not online thursday, wednesday or tuesday but last monday, sunday or saturday (and today)
Group A: Users that do not match the other groups (but were only today)
I only want to know the number of users in those groups (for a specific day)
a user can only be in ONE of these groups (for the same day)
Another Update: Accidently (by copy&paste) had starttime = ... or starttime = ... but it should be starttime = ... or endtime = ...
UPDATE:
To explain my query in more detail (in the final query there are even more comments):
First we simply got
SELECT
...
FROM gc_sessions s
WHERE DATE(starttime) = CURDATE() OR DATE(endtime) = CURDATE()
That's nothing more like saying "give me all users whose session started today or ended today". Having to consider those two times again and again makes the query a bit clumsy, but actually it's not that complicated.
So, usually we would use the COUNT() function to count something, obviously, but since we want "conditional counting", we simply use the SUM() function and tell it when to add 1 and when not.
SUM (CASE WHEN ... THEN 1 ELSE 0 END) AS a_column_name
The SUM() function examines now each row in the result set of sessions from today. So for each user in this result set we look if this user was online the date we specify. It doesn't matter how many times he/she was online, so for performance reasons we use EXISTS. With EXISTS you can specify a subquery which stops as soon as something is found, so it doesn't matter what it returns when something is found, as long as it's not NULL. So don't get confused why I selected 1. In the subquery we have to connect the user which is currently examined from the outer query with the user from the inner query (subquery) and specify the time window. If all criterias meet count 1 else 0 like explained before.
SUM(CASE WHEN
EXISTS (SELECT 1 FROM gc_sessions sub_s WHERE s.user = sub_s.user
AND ((date(starttime) = CURDATE() - INTERVAL 1 DAY)
OR (date(endtime) = CURDATE() - INTERVAL 1 DAY)))
THEN 1 ELSE 0 END) AS todayAndYesterday,
Then we make a column for each condition and voila, you have all you need in one query. So with your updated question your criteria has changed, we just have to add more rules:
SELECT
/*this is like before*/
SUM(CASE WHEN
EXISTS (SELECT 1 FROM gc_sessions sub_s WHERE s.user = sub_s.user
AND ((date(starttime) = CURDATE() - INTERVAL 1 DAY)
OR (date(endtime) = CURDATE() - INTERVAL 1 DAY)))
THEN 1 ELSE 0 END) AS FridayAndThursday,
SUM(CASE WHEN
EXISTS (SELECT 1 FROM gc_sessions sub_s WHERE s.user = sub_s.user
AND ((date(starttime) = CURDATE() - INTERVAL 2 DAY)
OR (date(endtime) = CURDATE() - INTERVAL 2 DAY)))
/*this one here is a new addition, since you don't want to count the users that were online yesterday*/
AND NOT EXISTS (SELECT 1 FROM gc_sessions sub_s WHERE s.user = sub_s.user
AND ((date(starttime) = CURDATE() - INTERVAL 1 DAY)
OR (date(endtime) = CURDATE() - INTERVAL 1 DAY)))
THEN 1 ELSE 0 END) AS FridayAndWednesdayButNotThursday,
SUM(CASE WHEN
EXISTS (SELECT 1 FROM gc_sessions sub_s WHERE s.user = sub_s.user
AND ((date(starttime) = CURDATE() - INTERVAL 3 DAY) /* minus 3 days to get tuesday*/
OR (date(endtime) = CURDATE() - INTERVAL 3 DAY)))
/*this is the same as before, we check again that the user was not online between today and tuesday, but this time we really use BETWEEN for convenience*/
AND NOT EXISTS (SELECT 1 FROM gc_sessions sub_s WHERE s.user = sub_s.user
AND ((date(starttime) BETWEEN CURDATE() - INTERVAL 2 DAY AND CURDATE() - INTERVAL 1 DAY)
OR (date(endtime) BETWEEN CURDATE() - INTERVAL 2 DAY AND CURDATE() - INTERVAL 1 DAY)))
THEN 1 ELSE 0 END) AS FridayAndTuesdayButNotThursdayAndNotWednesday,
.../*and so on*/
FROM gc_sessions s
WHERE DATE(starttime) = CURDATE() OR DATE(endtime) = CURDATE()
So, I hope you get the idea now. Any more questions? Feel free to ask.
end of update
Answer to previous version of question:
select
SUM(CASE WHEN EXISTS (SELECT 1 FROM gc_sessions sub_s WHERE s.user = sub_s.user
AND ((date(starttime) = CURDATE() - INTERVAL 1 DAY)
OR (date(starttime) = CURDATE() - INTERVAL 1 DAY)))
THEN 1 ELSE 0 END) AS todayAndYesterday,
SUM(CASE WHEN EXISTS (SELECT 1 FROM gc_sessions sub_s WHERE s.user = sub_s.user
AND ((date(starttime) BETWEEN CURDATE() - INTERVAL 2 DAY AND CURDATE() - INTERVAL 1 DAY)
OR (date(starttime) BETWEEN CURDATE() - INTERVAL 2 DAY AND CURDATE() - INTERVAL 1 DAY)))
THEN 1 ELSE 0 END) AS todayAndYesterdayOrTheDayBeforeYesterday,
SUM(CASE WHEN EXISTS (SELECT 1 FROM gc_sessions sub_s WHERE s.user = sub_s.user
AND ((date(starttime) BETWEEN CURDATE() - INTERVAL 7 DAY AND CURDATE() - INTERVAL 1 DAY)
OR (date(starttime) BETWEEN CURDATE() - INTERVAL 7 DAY AND CURDATE() - INTERVAL 1 DAY)))
THEN 1 ELSE 0 END) AS todayAndWithinTheLastWeek
from gc_sessions s
where date(starttime) = CURDATE()
or date(endtime) = CURDATE()
Instead of relying on session table, I suggest you to create separate table, which stores 2 fields, date and user_id.
Every time user logs-in you need to insert new entry into this table.
This way you will be able to retrieve all the 3 requirement of yours.
Example table:
CREATE TABLE `test`.`user_login_history` (
`id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
`userid` INTEGER UNSIGNED NOT NULL,
`date` DATETIME NOT NULL,
PRIMARY KEY (`id`)
)
ENGINE = InnoDB;
Once a user login, check whether he/she has login today or not:
select count(*) from user_login_history where
userid = 1 and `date` = '2013-01-28 00:00:00';
If the returned value is 1, means he/she has login today. no changes needed.
but, if the returned value is 0, means he/she has not login today. So record it down.
insert into user_login_history(userid,`date`)values(1,'2013-01-28 00:00:00');
Q1. How many users were online TODAY that were also online YESTERDAY?
select count(*) from user_login_history u where
u.`date` = '2013-01-28 00:00:00' and
(
select count(*) from user_login_history v where
v.`date` = '2013-01-27 00:00:00' and
v.userid = u.userid
) = 1;
Q2. How many users were online TODAY that were also online within in the last TWO DAYS
select count(*) from user_login_history u where
u.`date` = '2013-01-28 00:00:00' and
(
select count(*) from user_login_history v where
v.`date` >= '2013-01-26 00:00:00' and
v.`date` <= '2013-01-27 00:00:00' and
v.userid = u.userid
) > 0;
Q3. How many users were online TODAY that were also online within the last 7 DAYS
select count(*) from user_login_history u where
u.`date` = '2013-01-28 00:00:00' and
(
select count(*) from user_login_history v where
v.`date` >= '2013-01-21 00:00:00' and
v.`date` <= '2013-01-27 00:00:00' and
v.userid = u.userid
) > 0;
For yesterday
select id from gc_sessions where id in
(
select id
from gc_sessions
where starttime > subdate(current_date, 2)
and endtime < subdate(current_date, 1)
)
and starttime > subdate(current_date, 1);
For 2 Days
select id from gc_sessions where id in
(
select id
from gc_sessions
where starttime > subdate(current_date, 3)
and endtime < subdate(current_date, 1)
)
and starttime > subdate(current_date, 1);
For 7 Days
select id from gc_sessions where id in
(
select id
from gc_sessions
where starttime > subdate(current_date, 8)
and endtime < subdate(current_date, 1)
)
and starttime > subdate(current_date, 1);
You need to add a subquery that loads the data from the specified range (eg, 1day/2day/7days) and compares it with the data for the current day.
set #range = 7;
select * from gc_sessions
WHERE user in (SELECT user from gc_sessions
where starttime between subdate(current_date, #range) AND subdate(current_date, 1))
AND starttime > subdate(current_date, 0)
Where #range holds information about the number of days. See your expanded sql fiddle at - http://sqlfiddle.com/#!2/9584b/24
SELECT today.user
, GROUP_CONCAT(DISTINCT today.ip) ip
FROM gc_sessions today
JOIN gc_sessions yesterday
ON DATE(yesterday.starttime) = DATE(today.starttime) - INTERVAL 1 DAY
AND today.user = yesterday.user
WHERE DATE(today.starttime) = '2013-01-10'
GROUP
BY today.user;