Please help me modify the following query to extract only records made in the past 2 months.
SELECT COUNT(*)
FROM course_participants
WHERE course_id = courses.id
WHERE YEAR(created) = 2019)
Now it gets results from current year, I need to get the results from the past 2 months.
Thanks!
You can try below -
SELECT COUNT(*) FROM course_participants
WHERE created>=now()-interval 2 month
You can use the Month-function:
SELECT
COUNT(*)
FROM
course_participants
WHERE
course_id = courses.id and
YEAR(created) = 2019 and
MONTH(created) in (4,5)
If the date should not be fix, but depend on the current date. you can check out the Dateadd-Function.
If you want your query to return records from the past two months dynamically, then you can add a DATE_ADD expression in your WHERE clause:
SELECT COUNT(*) FROM course_participants WHERE course_id = courses.id and
created > DATE_ADD(CURDATE(), INTERVAL -2 MONTH)
This takes the current date and goes back 2 months. You can see some documentation on the function here.
You can substract 2 months from the current date.
SELECT COUNT(*) FROM course_participants WHERE course_id = courses.id AND created >= DATE_SUB(CURRENT_DATE, INTERVAL 2 MONTH)
Related
My table is like this:
root_tstamp
userId
2022-01-26T00:13:24.725+00:00
d2212
2022-01-26T00:13:24.669+00:00
ad323
2022-01-26T00:13:24.629+00:00
adfae
2022-01-26T00:13:24.573+00:00
adfa3
2022-01-26T00:13:24.552+00:00
adfef
...
...
2021-01-26T00:12:24.725+00:00
d2212
2021-01-26T00:15:24.669+00:00
daddfe
2021-01-26T00:14:24.629+00:00
adfda
2021-01-26T00:12:24.573+00:00
466eff
2021-01-26T00:12:24.552+00:00
adfafe
I want to get the number of users in the current year and in previous year like below using SQL.
Date Users previous_year
2022-01-01 10 5
2022-01-02 20 15
The code is written as follows.
select CAST(root_tstamp as DATE) as Date,
count(DISTINCT userid) as users,
count(Distinct case when CAST(root_tstamp as DATE) = dateadd(MONTH,-12,CAST(root_tstamp as DATE)) then userid end) as previous_year
FROM table1
But it returns 0 for previous_year values.
How can I fix that?
Possible solution for SQL Server:
WITH cte AS ( SELECT 2022 [year]
UNION ALL
SELECT 2021 )
SELECT cte.[year],
COUNT(DISTINCT test.userId) current_users_amount,
COUNT(DISTINCT CASE WHEN YEAR(test.root_tstamp) < cte.[year]
THEN test.userId
END) previous_users_amount
FROM test
JOIN cte ON YEAR(test.root_tstamp) <= cte.[year]
GROUP BY cte.[year]
https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=88b78aad9acd965bdbac4c85a0b81927
This query (for MySql) returns unique number of userids where the root_timestamp is in the current year, by day, and the number of unique userids for the same day last year. If there is no record for a day in the current year nothing will be displayed for that day. If there are rows for the current year, but no rows for the same day last year, then NULL will be shown for that lastyear column.
SELECT cast(ty.root_tstamp as date) as Dte,
COUNT(DISTINCT ty.userId) as users_this_day,
count(distinct lysd.userid) as users_sameday_lastyear
FROM test ty
left join
test lysd
on cast(lysd.root_tstamp as date)=date_add(cast(ty.root_tstamp as date), interval -1 year)
WHERE YEAR(ty.root_tstamp) = year(current_date())
GROUP BY Dte
If you wish to show output rows for calendar days even if there are no rows in current year and/or last year, then you also need a calendar table to be introduced (let's hope that it is not what you need)
I want to select created_by="James" (count) when day is Monday and month is January.
Hot to achieve it?
Could you please help me showing some queries examples?
Thanks,
Nicola
Have a look at the reference manual regarding date and time functions:
https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html
I think your query should look similar to this:
SELECT count(*)
FROM your_table
WHERE created_by = "James"
AND MONTH(your_date_column) = 1
And DAYNAME(your_date_column) = "Monday"
to get the counts for the different days of the week you need to group by dayname:
SELECT count(*) AS count,
DAYNAME(your_date_column) AS dayname
FROM your_table
WHERE created_by = "James"
AND MONTH(your_date_column) = 1
GROUP BY DAYNAME(your_date_column)
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...
I already count inscriptions, this week.
But i need to count inscription per week last 12 weeks, and send it in an array.
My actual request is :
SELECT COUNT(*) FROM PERSON
YEAR(date_inscription) = YEAR(NOW())
AND WEEK(date_inscription) = WEEK(NOW())
SELECT YEARWEEK(date_inscription) as yweek,
COUNT(*) as cnt
FROM PERSON
where YEARWEEK(date_inscription) >= YEARWEEK(curdate()) - 12
group by yweek
I've got the following query:
$data = mysql_fetch_array(mysql_query("SELECT
c.`id` AS cid,
p.`id` AS pid,p.`email`
FROM `coupons` AS c
LEFT JOIN `coupons_partners` AS cp ON cp.`cid` = c.`id`
LEFT JOIN `partners` AS p ON p.`id` = cp.`pid`
LEFT JOIN `bills` AS b ON b.`pid` = p.`id`
WHERE
(
CURRENT_DATE() BETWEEN c.`expires` AND ADDDATE(c.`expires`, INTERVAL 1 MONTH)
)
OR
(
CURRENT_DATE() NOT BETWEEN c.`expires` AND ADDDATE(c.`expires`, INTERVAL 1 MONTH)
AND
CURRENT_DATE() BETWEEN b.`date` AND ADDDATE(b.`date`, INTERVAL 1 MONTH)
)
ORDER BY b.`id` DESC"));
It's kinda messy. I want to do a cronjob and create bills automatically. They should only be created after 1 month a coupon expires (c.expires is DATETIME) or if the LAST bill (b.date) was created 1 month ago.
The thing also is that I don't want to create any bill if the coupon didn't started yet. And this is possible, because I create coupons that'll start in maybe 3 months. So I guess the "Between-Solution" doesn't fit here?
I'm trying to figure it out to do it properly, but now I would appreciate any help.
I would be happy and thankful, if someone could help me out.
Best Regards,
Alex
Use DATEDIFF
SELECT DATEDIFF(month, '2000-02-10','2000-01-10');
yields 1 since 02-10 is one month after 01-10
month can be changed to day to get the number of days instead.
Reference: http://www.1keydata.com/sql/sql-datediff.html
UPDATE:
In MSQL you could use DATEDIFF because it can be passed month as an argument, the MySQL version only returns days. In MySQL you could instead use PERIOD_DIFF. Although it doesn't know about the exact number of days in each month.
PERIOD_DIFF(P1,P2)
"Returns the number of months between periods P1 and P2. P1 and P2 should be in the format YYMM or YYYYMM. Note that the period arguments P1 and P2 are not date values."
mysql> SELECT PERIOD_DIFF(200802,200703);
-> 11
If you need to know if exactly how many months differ you need to make a multistep calculation. To find out the number of days in a month you could use the LAST_DAY function and from the results extract the day part and use as the basis for further calculations.
LAST_DAY('2003-02-05');
-> '2003-02-28'