Score Table
user_idx (int)
date (datetime)
score (int)
I need to find out how much total score has increased over a week from today's date. I know that I need two of the same user tables grouped by user_idx that one contains total scores from the past to today and the other contains total scores from the past to a date of a week ago.
After that, by substracting one from the other will give me the answer... but I'm struggling to write effective sql query that does it.
I've tried
SELECT BLAH BLAH
FROM (SELECT user_idx, COUNT(*) as last_week_study_amount
FROM user_table
WHERE date <= date_sub(now(),INTERVAL 1 WEEK)
GROUP BY user_idx)
AS a WHERE .....
Could you help me :( ?
Let me clear you want to get total count in last week.
Try below query
SELECT *
FROM (SELECT user_idx, COUNT(*) as last_week_study_amount
FROM user_table
WHERE date <= date_sub(now(),INTERVAL 1 WEEK)
GROUP BY user_idx)
AS a WHERE .....
SELECT (SUM(score) - last_week_score) AS increased_score,
FROM user a
JOIN (SELECT b.user_idx, COUNT(*) as last_week_score
FROM userb
WHERE date<= date_sub(now(),INTERVAL 1 WEEK)
GROUP BY b.user_idx) As c ON a.user_idx = c.user_idx
WHERE DATE(date) <= DATE(NOW())
GROUP BY a.user_idx
I ended up writing this code and I think this one is working okay... not sure if it's the best or has a critical error. I will update it if it turns out to be a bad one...
Related
I simplified the query and the table, but to give you some context: I have an "orders" table that contains all the items ordered by customers.
I want to check which items have been ordered at least once in history, but then, for whatever reason, nobody ordered them anymore (in this case, 180 days but it's just an example).
SELECT DISTINCT (idItem)
FROM orders
WHERE myDate < DATE_SUB(NOW(),INTERVAL 180 DAY)
AND
(idItem NOT IN
(SELECT
DISTINCT(idItem)
FROM orders
WHERE myDate > DATE_SUB(NOW(),INTERVAL 180 DAY)
)
)
ORDER BY myDate
This was my reasoning: I pick all the items ordered MORE THAN 6 months ago, and from the result I keep only the ones that DO NOT APPEAR in the previous 6 months.
I have 2 problems: one, it gives me back basically all of them, and second, regardless of "distinct", I still get plenty of duplicates :(
I do not usually deal with SQL, I know enough for simple queries but this one is probably as complex as I can make them :)
SELECT idItem
FROM orders
GROUP BY idItem
HAVING 0 = SUM(myDate > (CURRENT_DATE - INTERVAL 180 DAY))
?
With NOT EXISTS:
SELECT DISTINCT o.idItem
FROM orders o
WHERE NOT EXISTS (
SELECT 1 FROM orders
WHERE idItem = o.idItem AND myDate > DATE_SUB(NOW(), INTERVAL 180 DAY)
)
"SELECT count(id) AS total FROM participant where dateofbooking='$datepick'";
I am using this code. But its showing only one date(selected date from php) count. but I want to select single date and it should show me upto 5 days daily wise count booking.
output should be like this:-
2018-05-20------>48
2018-05-21------>58
2018-05-22------>67
2018-05-23------>78
2018-05-24------>43
You can use DATE_ADD() :
SELECT dateofbooking, count(id) AS total
FROM participant
WHERE dateofbooking >= $datepick AND
dateofbooking <= DATE_ADD($datepick, INTERVAL 5 DAY)
GROUP BY dateofbooking;
You can group it by the date column you are using, and if you want multiple days you can add dateofbooking >= some_start_date and dateofbooking <= some_end_date
"SELECT count(id) AS total FROM participant where dateofbooking='$datepick' group by dateofbooking";
the multiple may look something like
"SELECT count(id) AS total FROM participant where dateofbooking>='$datepickstart' AND dateofbooking<='$datepickend' group by dateofbooking";
I have table user with column login_time.
I want to select all the users that have logged in more than 10 times in a month.
I tried something like this:
SELECT login_time, count(id) as loginCount FROM user
WHERE login_time between DATE_SUB(login_time INTERVAL 1 month) AND login_time
GROUP BY id, MONTH(login_time) HAVING loginCount > 10;
Im not sure about my selection between dates.
How can I select with a month intervals avoiding double records.
For example if I have this values for login_time:
1. '2015-02-01 14:05:19'
2. '2015-01-21 14:05:19'
3. '2015-01-11 14:05:19'
Both 3 and 2 are within month range of 1.
So will I get double records for that values?
To find the users who have logged in more than ten times in the month ending right now, do this.
SELECT COUNT(*) times_logged_in,
userid
FROM user
WHERE login_time >= NOW() - INTERVAL 1 MONTH
GROUP BY user_id
HAVING COUNT(*)> 10
To find the users who have logged in more than ten times in any calendar month in your table, do this.
SELECT COUNT(*) times_logged_in,
DATE(DATE_FORMAT(login_time, '%Y-%m-01')) month_beginning,
userid
FROM user
GROUP BY user_id, DATE(DATE_FORMAT(login_time, '%Y-%m-01'))
HAVING COUNT(*)> 10
The trick here is the expression DATE(DATE_FORMAT(login_time, '%Y-%m-01')), which converts any timestamp to the first day of the month in which it occurs.
Your question mentioned this WHERE condition:
WHERE login_time between DATE_SUB(login_time INTERVAL 1 month) AND login_time
This doesn't do anything interesting because it always comes back true. Each given login_time always falls in the interval you specified.
Edit: You can GROUP BY MONTH(dt) if you want. But the way I have shown it automatically accounts for years as well as months, so in my opinion it's much better for accurate reporting.
Another edit: This formula yields the preceding Sunday for any given date or timestamp item.
FROM_DAYS(TO_DAYS(login_time) -MOD(TO_DAYS(login_time) -1, 7))
If Monday is the first day of the week in your jurisdiction, change the -1 to -2. Grouping by this function is superior to doing GROUP BY WEEK(login_time) because WEEK() does odd things at the beginnings and ends of calendar years.
This is all written up in more detail here: http://www.plumislandmedia.net/mysql/sql-reporting-time-intervals/
I apologize if this has been asked before.. I'm very new to developing and although I've tried searching a lot, I'm not really sure what to look for.
Anyway so I have a table which counts records being entered per day. It looks something like this (each record is represented by a letter) (assume today's date is 27/01/2013):
RECORD | COUNT | DATE
------A-----|-----4-----|27/01/2013
------B-----|-----7-----|27/01/2013
------B-----|-----3-----|24/01/2013
------C-----|-----8-----|22/01/2013
------A-----|-----2-----|19/01/2013
Each new post is checked in the table and it updates the count if the record already exists on the current day, otherwise a new record is created.
For the page which prints the records which have been added 'TODAY', I have the MySQL query
SELECT * FROM `table` ORDER BY `date` DESC, `count` DESC LIMIT 1000
and use a php 'if' statement to only print the records where the date('Y-m-d') = date in the table. So only the records and the corresponding count which has been entered that day are printed.
- the table above would produce the result:
1. B 7
2. A 4
What I would like is a page which prints the records which have been entered in the last week. I know I can use DATE_SUB(now(),INTERVAL 1 WEEK) AND NOW(), to print the records from last week but I need to duplicate records to be combined and the counts added together.. so the result for this table would look like this:
1. B 10
2. C 8
3. A 4
How would I go about combining those duplicate records and have a list of records ordered by count? Is this the best method to get a 'last week' record count, or is there another table structure which would be better?
Again I'm sorry if this a silly question or if my explanation was long-winded, but just some simple pointers will be really appreciated.
Try this
SELECT `record`, SUM(`count`) AS `count`
FROM `table`
WHERE `date` > DATE_SUB(CURDATE(),INTERVAL 1 WEEK)
GROUP BY `record`
ORDER BY `count` DESC
And you can LIMIT 1000 grouped resultset if you need to
Using GROUP BY will allow you group related records together
SELECT `record`
, SUM(`count`) AS `count`
FROM `table`
WHERE `date` > `date` - INTERVAL 1 WEEK
GROUP BY `record`
ORDER BY `count` DESC
LIMIT 1000
I have a query which returns total number of users registered on a particular days in 7 days interval time. I also want to get the zero count data so i can plot it on my graph. How to fetch zero count values?
The query :
select date(update_timestamp) as date, count(*) users
from registered
where date(update_timestamp) between date_sub(sysdate(), interval 7 day) and sysdate()
group by date(update_timestamp)
I had few problems with my query earlier which was solved on this post http://bit.ly/12irdyf .The problem is solved, however i need modification in my query, now i need to show null values as well.
Thanks
best way to keep a calender_table which has got entries for each date for the year.
select date(update_timestamp) as date, count(*) users
from calender_table c
left join registered r
on date(update_timestamp)=c.date
where c.date between date_sub(sysdate(), interval 7 day) and sysdate()
group by c.date
What do you mean by zero count values? Maybe you need:
select date(update_timestamp) as date, count(*) users, '0' AS zero
Maybe you wanted to fetch what date it was when you had no users?
Simply locate the date before the first user registered and assign a zero to it like above.