I would like to group a series of counts but by week. I know that using the week() function returns a number for the week, but i'd like something like this:
Week count(*)
Jan 1 - Jan 7 30
Jan 8 - 14 50
and so on...
Is there a way to do this? Thank you very much.
select
concat(cast(DATE_ADD(yourdate, INTERVAL(1-DAYOFWEEK(yourdate)) DAY) as char), ' - ',
cast(DATE_ADD(yourdate, INTERVAL(7-DAYOFWEEK(yourdate)) DAY) as char))
as period, count(*)
from tablename
group by week(yourdate)
Related
I'm trying to calculate the duration in days for different years where:
startdate enddate duration (days)
2016-09-20 2018-09-20 730
Where i want the following outcome in one row as duration per year,
with the year as column name and the days as result:
2016 = 103 days - 2017 = 365 days - 2018 = 263 days
I couldn't find a specific solution except using DATEDIFF and entering the enddate of each year but this doesn't calculate the remaining days over next years. Anyone who can help me out finding a solution with MySQL?
Assuming (hoping) that you have a numbers table you can the following calculation:
SELECT startdate,
enddate,
number AS year_num,
DATEDIFF(
LEAST(DATE(CONCAT(number, '-12-31')), enddate),
GREATEST(DATE(CONCAT(number, '-01-01')), startdate)
) + 1 AS num_days
FROM t
INNER JOIN (
SELECT 2010 AS number UNION ALL
SELECT 2011 UNION ALL
SELECT 2012 UNION ALL
SELECT 2013 UNION ALL
SELECT 2014 UNION ALL
SELECT 2015 UNION ALL
SELECT 2016 UNION ALL
SELECT 2017 UNION ALL
SELECT 2018 UNION ALL
SELECT 2019
) AS numbers ON number BETWEEN YEAR(startdate) AND YEAR(enddate)
I am having problem with dealing with data in MySQL. How can I select all data from the pasty seven available data?
I tried to run
SELECT * FROM database where ship_day
BETWEEN DATE_SUB(NOW(), INTERVAL 7 DAY) AND NOW();
but I doesn't work for my purpose.
Let's say we are on January, 9th and my MAX(ship_day) is January, 7th and I want to pull data from the past 7 available data so from January 1st to 7th.
I tried to run
SELECT * FROM database where ship_day
BETWEEN DATE_SUB(MAX(ship_day), INTERVAL 7 DAY) AND MAX(ship_day);
but I got an error.
I know that I can increase INTERVAL 7 DAY to INTERVAL 9 DAY but I want this process to be automatic. How can I solve?
Well, here is one method:
SELECT d.*
FROM database d
WHERE d.ship_day >= COALESCE( (SELECT DISTINCT d2.ship_day
FROM database d2
ORDER BY d2.ship_day DESC
LIMIT 1 OFFSET 6
), d.ship_day
);
Note: If you have only one row per ship_day, then just use limit:
SELECT d.*
FROM database d
ORDER BY d.ship_day DESC
LIMIT 7
Let's say we are on January, 9th and my MAX(ship_day) is January, 7th
and I want to pull data from the past 7 available data so from January
1st to 7th.
I assume you are looking for the query below but hard to say for sure without example data and expected results.
SELECT
*
FROM
t
WHERE
ship_day >= (
SELECT
MAX(ship_day) - INTERVAL 7 DAY
FROM
t
)
Or you can also write this as
SELECT
*
FROM
t
WHERE
ship_day >= (
SELECT
MAX(ship_day)
FROM
t
) - INTERVAL 7 DAY
You can get the last date for which you have data with:
select MAX(ship_day) FROM database
so use it like this:
SELECT * FROM database
where ship_day >= DATE_SUB((select MAX(ship_day) FROM database), INTERVAL 7 DAY)
I want the count of records in every month and total count of records from start upto that month.
For ex.,
I have a table that looks like this:
#id,created#
1,'2016-01-01'
2,'2011-02-02'
3,'2011-02-09'
4,'2011-02-05'
5,'2011-03-07'
6,'2011-03-08'
How do I select and group these so the output is:
#Month, new, total#
Jan 2016, 1, 1
Feb 2016, 3, 4
Mar 2016, 2, 6
Thanks very much.
Here you go:
SELECT DATE_FORMAT(`created`,'%M %Y') AS month, COUNT(*) AS count,
(SELECT count(*) FROM test WHERE MONTH(created) <= MONTH(t.created)) AS total
FROM test t
GROUP BY MONTH(created);
Here's the SQL Fiddle.
Using single table read:
SELECT
CONCAT(LEFT(MONTHNAME(dt), 3), ' ', YEAR(dt)) month,
new,
#total:=#total + new total
FROM
(SELECT
created - INTERVAL DAY(created) - 1 DAY dt, COUNT(*) new
FROM
t
GROUP BY created - INTERVAL DAY(created) - 1 DAY
ORDER BY dt) t
CROSS JOIN
(SELECT #total:=0) t2
Demo
I'm trying to get all posts from the 12 last month, group by month. I have a quite correct query:
SELECT MONTH(time) as mois, YEAR(time) as annee, count(*) as nbre
FROM touist_stories
WHERE time >= DATE_SUB(now() + INTERVAL 1 MONTH, INTERVAL 2 YEAR)
group by MONTH(time)
order by YEAR(time) DESC, MONTH(time) DESC
But one month is always missing : november 2012
I tryied to add
+ INTERVAL 1 MONTH
to now() but it still missing... How can I get the 12 last month and not the 11 ones please?
Thanks
To get one year ago, here's a technique I've used in the past. Using #mysql variables, create a date based on the first day of a given month/year (via now()), then subtract 12 months. This example will get from Oct 1, 2012 to current -- which will include current Oct 2013. To exclude that, just add to where clause where I re-added 1 year so it goes from Oct 1, 2012 at 12:00:00 am to LESS THEN Oct 1, 2013 12:00:00.
SELECT
MONTH(time) as mois,
YEAR(time) as annee,
count(*) as nbre
FROM
touist_stories,
( select #lastYear := date_add( DATE_FORMAT(NOW(),
'%Y-%m-01'), interval -11 month) ) sqlvar
WHERE
time >= #lastYear
group by
MONTH(time)
order by
YEAR(time) DESC,
MONTH(time) DESC
Revised to make it go 11 months back (to November per example), and include UP TO AND INCLUDING all Current October activity.
For realy want on year data use 11 MONTH not 12
SELECT time
FROM touist_stories
WHERE time
BETWEEN
date_sub(Now(), INTERVAL 11 MONTH)
AND
Now();
Here's the table structure and some sample data:
pID.....month.....year
27 .....3 .....2008
27 .....12 .....2012
31 .....6 .....2008
99 .....1 .....2006
42 .....1 .....2009
pID is the practiceID and month and year represent the date period they've entered data for. I need to grab the number of practices that have entered data for the first time in Oct 2012, Nov 2012, Dec 2012 and so on.
I tried the following query for Oct 2012:
SELECT *
FROM
IPIPKDIS
where
practiceID NOT IN (
SELECT practiceID
from
IPIPKDIS
where
year < 2012 and month < 10
)
and year = 2012
and month = 10
and measureCatRecID = 2
ORDER BY year, month;
but it's grabbing months and year less than 10/2012.
If I run the queries isolated (not as subquery) they both work fine.
Any ideas?
This summary query will yield the first (smallest) date in the table for each value of practiceID.
SELECT practiceID,
MIN(STR_TO_DATE( CONCAT(year, ' ', month), '%Y %m')) first_date
FROM IPIPKDIS
GROUP BY practiceID
If you want to retrieve then the whole row for the first reported month, you'd do a nested query like this:
SELECT *
FROM IPIPKDIS I
JOIN (
SELECT practiceID,
MIN(STR_TO_DATE( CONCAT(year, ' ', month), '%Y %m')) first_date
FROM IPIPKDIS
GROUP BY practiceID
) first ON ( first.practiceID = I.practiceID
AND STR_TO_DATE( CONCAT(I.year, ' ', I.month), '%Y %m') = first.first_date)
The trick to the second query is to use the JOIN to extract just the first-month rows from your table. We use date arithmetic to do the date comparisons.