I want to subtract 2 periods of dates to compare data of current year vs previous year and my query is not working.
If I remove a period it works, but not sure how to pull 2 periods.
Thanks
SELECT year(date) as year, WEEK(ADDDATE(date, 5-DAYOFWEEK(date)), 3) AS 'Week', DATE,
FROM TABLE
WHERE year(date) in (2015, 2016)
AND (DATE BETWEEN "2016-10-23" AND "2016-11-12")
AND (DATE BETWEEN "2015-11-06" AND "2015-11-12")
ORDER BY DATE, year(date)
Please try this one.
SELECT year(date) as year, WEEK(ADDDATE(date, 5-DAYOFWEEK(date)), 3) AS 'Week', DATE,
FROM TABLE
WHERE
((DATE BETWEEN "2016-10-23" AND "2016-11-12")) OR
((DATE BETWEEN "2015-11-06" AND "2015-11-12"))
ORDER BY DATE, year(date)
I think you want or:
SELECT year(date) as year, WEEK(ADDDATE(date, 5-DAYOFWEEK(date)), 3) AS Week, DATE
FROM TABLE
WHERE DATE BETWEEN '2016-10-23' AND '2016-11-12' OR
DATE BETWEEN '2015-11-06' AND '2015-11-12';
Notes:
You don't need the date() comparison in the where clause.
You should use single quotes for date and string constants.
You should not use single quotes for column aliases.
SELECT year(DATE) AS year
,WEEK(ADDDATE(DATE, 5 - DAYOFWEEK(DATE)), 3) AS 'Week'
,DATE
FROM TABLE
WHERE (
(
DATE BETWEEN '2016-10-23'
AND '2016-11-12'
)
OR (
DATE BETWEEN '2015-11-06'
AND '2015-11-12'
)
)
Related
I can't figure out a way to COUNT al transactions by month each YEAR on a single query. I figured out a way but for one month at a time, there's a lot of data (time consuming)
this is the table
i want to have something like this
thank you
here is how you can do it , using extract to extract month /year from your date column and group by it:
select
extract(year_month from Date) year_month
, count(*) total_sales
from your table
group by extract(year_month from Date)
order by extract(year_month from Date)
I expect this query to give me the avg value from daily active users up to date and grouped by month (from Oct to December). But the result is 164K aprox when it should be 128K. Why avg is not working? Avg should be SUM of values / number of current month days up to today.
SELECT sq.month_year AS 'month_year', AVG(number)
FROM
(
SELECT CONCAT(MONTHNAME(date), "-", YEAR(DATE)) AS 'month_year', count(distinct id_user) AS number
FROM table1
WHERE date between '2020-10-01' and '2020-12-31 23:59:59'
GROUP BY EXTRACT(year_month FROM date)
) sq
GROUP BY 1
Ok guys thanks for your help. The problem was that on the subquery I was pulling the info by month and not by day. So I should pull the info by day there and group by month in the outer query. This finally worked:
SELECT sq.day_month, AVG(number)
FROM (SELECT date(date) AS day_month,
count(distinct id_user) AS number
FROM table_1
WHERE date >= '2020-10-01' AND
date < '2021-01-01'
GROUP BY 1
) sq
GROUP BY EXTRACT(year_month FROM day_month)
Do not use single quotes for column aliases!
SELECT sq.month_year, AVG(number)
FROM (SELECT CONCAT(MONTHNAME(date), '-', YEAR(DATE)) AS month_year,
count(distinct id_user) AS number
FROM table1
WHERE date >= '2020-10-01' AND
date < '2021-01-01'
GROUP BY month_year
) sq
GROUP BY 1;
Note the fixes to the query:
The GROUP BY uses the same columns as the SELECT. Your query should return an error (although it works in older versions of MySQL).
The date comparisons have been simplified.
No single quotes on column aliases.
Note that the outer query is not needed. I assume it is there just to illustrate the issue you are having.
My I'm having problems with a query.
Here is what I have
SELECT COUNT(id) as Counted
FROM referral_code_logs
GROUP BY DATE_FORMAT(time_stamp, '%c/%e/%y');
I am trying to see the amount of referrals each day has. The part I'm having problems with is the date_format.
Table layout
Here are how the time_stamps are entered in the Table: (month/day/year)
month is 1-12 day is 0-31 year is YY
Time_stamps
You just need to add in the date to the select part:
SELECT DATE_FORMAT(STR_TO_DATE(time_stamp, '%c/%e/%y'), '%c/%e/%y') as DT
,COUNT(id) as Counted
FROM referral_code_logs
GROUP BY DATE_FORMAT(time_stamp, '%c/%e/%y');
This is my SQL statement which pulls back all the fills I've had in a certain time frame. Is there a way to get the list to come up and also pull the SUM of all of them?
SELECT customerName, date, gallons
FROM addFill
WHERE date >= CONVERT(datetime, '3-3-2015' )
ORDER BY CONVERT(DATE, date) ASC
First, I would write your query as:
SELECT customerName, date, gallons
FROM addFill
WHERE date >= '2015-03-03'
ORDER BY date ASC;
I see no value in ordering by the date and not the date/time component. Also, you might as well just use a recognizable date format for the comparison.
If you want the sum as well, then that is tricky. One method uses rollup:
SELECT customerName, date, SUM(gallons) as gallons
FROM addFill
WHERE date >= '2015-03-03'
GROUP BY customerName, date with rollup
sums of fills per customer since '3-3-2015'
SELECT customerName, date, sum(gallons)
FROM addFill
WHERE date >= CONVERT(datetime, '3-3-2015' )
GROUP BY customerName
sum of fills for all customers since '3-3-2015'
SELECT sum(gallons)
FROM addFill
WHERE date >= CONVERT(datetime, '3-3-2015' )
For example I have a table with fields:
id date
1 2001-01-01
2 2001-01-05
.................
N 2011-12-31
How get i get all months last days from this table?
example:
if i have dates like 2001-05-31 and 2001-06-01
i need only 2001-05-31 not both
You can do SELECT LAST_DAY for example the below returns Oct. 31st. 2010
SELECT LAST_DAY('2010-10-10');
select max(date_format(date, '%d')) as last_day_of_the_month
from table
group by date_format(date, '%Y%m')
maybe this would work better?
select DISTINCT(LAST_DAY(date)) from table GROUP BY date_format(date, '%Y%m')
SELECT id, date
FROM `table`
WHERE DATE( date ) = LAST_DAY( date )
The DATE function over field is for filter the date without time, use only if you have a datetime column.
The query get all rows with date = last day of month.
select subdate(adddate(subdate(`date`, day(`date`) - 1), interval 1 month), 1)
Note: This is the "hard way". See #harper89's answer - it's better :)
I found a solution. but this query is very slow on large tables. so I am still looking for a better solution
select DISTINCT(LAST_DAY(date)) from table;
select max(date)
from table
group by year(date), month(date)