how can I count last 7 days data from SQL and group them by day/date (excluding today).
I should be able to use the result as $resultday1, $resultday2, $resultday3 etc.
If there was 10 total SQL entries in day 1 (yesterday) $resultday1 should show "10".
and the days should be last 7 only, and today/current day should not consider.
The following PHP SQL script shows the total count only
SELECT COUNT(1) FROM orders WHERE username='jondoe'
database is a list of referrals made by a registered user in previous days.
a single table contains all user's referral details, table name "orders" as per above example.
This is the exact query as you want
SELECT
COUNT(*), DATE(order_date) order_date
FROM
orders
WHERE
order_date < CURDATE()
AND order_date > DATE_SUB(NOW(), INTERVAL 7 DAY)
GROUP BY order_date
ORDER BY order_date DESC;
Related
I have a table in which there are 5 columns,
id (auto incrementing number), titleId, version, created_at
A combination of titleId and version is always unique. I want to find out for each day for the past 1 month, how many unique titleIds were present along with the count of how many entries per day. This is because on a given day there might be multiple versions of the same titleId.
select count(*) from titles where created_at >= '2019-08-12 00:00:00' and created_at <= '2019-08-13 00:00:00' will give me total number of titles which came on 12th August
and
select count(distinct titleId) from titles where created_at >= '2019-08-12 00:00:00' and created_at <= '2019-08-13 00:00:00'
will give me the count of unique titles on the 12th August. Is there a way for me to generate the data for the past 30/60 days?
I know I can run this command manually 30 times by changing the date to get the numbers, but was wondering if there is a better way to do this in mysql
As long as there is an entry every day, this query should give you the data for each day for the last 30:
select date(created_at) as cdate, count(distinct titleId) as cnt
from titles
where created_at >= cur_date() - interval 30 day
group by cdate
Input: I have a table that has a list of accounts and what they spent each day. For simplicity, we will say that the table has the following fields: account, account_id, date, sales.
Output: The end goal is to get a report of the Top 25 accounts and list their sales for the prior 8 weeks. The report will have the account, account_id, and then 8 additional columns for each of the prior 8 weeks with the sales summed in each column.
Problem 1: I'm having trouble grabbing the Top 25 accounts. I wrote a subquery to sum the prior month's sales for each account and grab the Top 25 by using the where clause with an in operator, but subqueries don't support in operators.
Problem 2: I am unsure how to pivot the 8 prior weeks into columns.
select
yearweek(date,1) as year_week
,date_add(makedate(year(date),1), interval week(date,1) week) as first_day_of_week
,account_id
,account
,sum(sales) as total_sales
from daily_sales
where date >= date_sub(curdate(), interval 8 week)
and account_id in ( select
account_id
from daily_sales
where year(date)=2018 and month(date)=11
group by 1
limit 25)
group by 1,2,3,4;
I have a table that I can pull up that shows daily if a customer made a purchase or not and the values are 0 or 1 for the day with the list of customers.
Table = Customers_daily,
column = made_purchase = 1 or 0
Select date_utc, count(distinct customer_id), made_purchase
from customers_daily
If i wanted to analyze the customers who made a purchase only in their 6th month since reg_date where should i specify that?
this is the output im looking for
Im just trying to make sure on this date_utc we are pulling the customers who are in their 6th month since registration date
http://sqlfiddle.com/#!9/b65b3fd/1
SELECT date_utc,
count(distinct customer_id),
MAX((date_utc >= reg_date + INTERVAL 5 MONTH) AND (date_utc <= reg_date + INTERVAL 6 MONTH))
FROM customers_daily
GROUP BY customer_id
The only thing I can't get is count(distinct customer_id) - it will always return 1, since I GROUP BY customer_id?
But if you want to GROUP BY date_utc - than what made_purchase meaning over many different customers?
Please provide sample of raw data and expected result.
"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/