How can I find last month records? - mysql

how can I found last month of records while from current month.
ex.
12-11-2015
12-12-2015
12-1-2016
12-2-2016
I wrote my query like this:
select * from tbl where MONTH(date) < MONTH(CURDATE());
It shows only one record 12-1-2016. But I want all record except last record 12-2-2016.
I think problem with different years.

Try this:
SELECT *
FROM tbl
WHERE (MONTH(date) < MONTH(CURDATE()) AND YEAR(date) = YEAR(CURDATE()))
OR
YEAR(date) < YEAR(CURDATE())
The predicates used in the above query essentially say: if both dates are on the same year, then you can compare month, otherwise you have to only compare the year value.

select * from tbl where MONTH(date) < MONTH(CURDATE()) OR YEAR(date) < YEAR(CURDATE());

Related

Find number of rows for each hour where datetime columns match certain criteria

RDBMS: MySQL
The time column(s) datatype is of datetime
For every hour of the 24 hour day I need to retrieve the number of rows in which their start_time matches the hour OR the end_time is great than or equal to the hour.
Below is the current query I have which returns the data I need but only based off of one hour. I can loop through and do 24 separate queries for each hour of the day but I would love to have this in one query.
SELECT COUNT(*) as total_online
FROM broadcasts
WHERE DATE(start_time) = '2018-01-01' AND (HOUR(start_time) = '0' OR
HOUR(end_time) >= '0')
Is there a better way of querying the data I need? Perhaps by using group by somehow? Thank you.
Not exactly sure if i am following, but try something like this:
select datepart(hh, getdate()) , count(*)
from broadcasts
where datepart(hh, starttime) <=datepart(hh, endtime)
and cast(starttime as date)=cast(getdate() as date) and cast(endtime as date)=cast(getdate() as date)
group by datepart(hh, getdate())
Join with a subquery that returns all the hour numbers:
SELECT h.hour_num, COUNT(*) AS total_online
FROM (SELECT 0 AS hour_num UNION SELECT 1 UNION SELECT 2 ... UNION SELECT 23) AS h
JOIN broadcasts AS b ON HOUR(b.start_time) = h.hour_num OR HOUR(b.end_time) >= h.hour_num
WHERE DATE(b.start_time) = '2018-01-01'
GROUP BY h.hour_num

Count number of months till last month using sql query

I want to count number of months till last month where registeration is done.i wrote the query as
SELECT count(MONTH(`regdate`) < MONTH(CURDATE()) AND YEAR(`regdate`) <= YEAR(CURDATE())) FROM `client_details` WHERE hospitalid='apollo123'
Maybe you want this:
select month(regdate), count(*)
from client_details
where regdate < curdate()
and hospitalid = 'apollo123'
group by month(regdate);
This will, for each month prior to curdate() give you the count of registrations that happened in that month.
SELECT count (*)from (
select month(regdate)
from client_details
where regdate < curdate()
and hospitalid = 'apollo123'
group by month(regdate));
This will give you number of months till last month when registrations were done.
One solution is
select count(distinct(cast(MONTH(regdate) as nvarchar(10)) + cast(year(regdate) as nvarchar(10)))) from client_details where hospitalid = 'apollp123' and regdate < DATEADD(month, DATEDIFF(month, 0, GETDATE())-1, 0)
I tested this one on Sql Server, you should adjust the code for mySql .

How to count records as "0" that have value "0" when using WHERE x = 1

I have a table structure that looks like this:
I have a perfectly working query that counts how many records there have been per day the last 30 days. It looks likes this:
SELECT DATE(timestamp) AS date, COUNT(id) AS emails FROM 'emails WHERE timestamp >= now() - interval 1 month GROUP BY DATE(timestamp)
This outputs the following which is perfectly fine:
However, the next thing seems too difficult for me to imagine. Now I want to count how many records there have been per day the last 30 days BUT only where newsletter = 1.
I've tried to put a WHERE statement looking like this:
SELECT DATE(timestamp) AS date, COUNT(*) AS emails, nyhedsbrev FROM emails WHERE timestamp >= now() - interval 1 month AND nyhedsbrev = 1 GROUP BY DATE(timestamp)
... And that outputs the following:
The problem is, that its omitting the records with newsletter = 0 and there by I cant compare my first query against the new one, as the dates doesnt match. I know that is because I use WHERE newsletter = 1.
In stead of omitting the record I want a query that just puts a "0" from that date. How can I do this? The final query should be outputting this:
You should be able to simply use SUM() and IF() to get the desired output:
SELECT
DATE(timestamp) AS date,
COUNT(*) AS emails,
SUM(IF(nyhedsbrev > 0, 1, 0)) as nyhedsbrev_count
FROM
emails
WHERE
timestamp >= now() - interval 1 month
GROUP BY
DATE(timestamp)
SQLFiddle DEMO
Edit: You might even be able to simplify it, since it's a boolean, and simply use SUM(nyhedsbrev), but this REQUIRES that nyhedsbrev is only 0 or 1:
SELECT
DATE(timestamp) AS date,
COUNT(*) AS emails,
SUM(nyhedsbrev) as nyhedsbrev_count
FROM
emails
WHERE
timestamp >= now() - interval 1 month
GROUP BY
DATE(timestamp)
Possibly best to get a list of the dates and then left join that against sub queries to get the counts you require.
Something like this
SELECT Sub1.date, Sub2.emails, IFNULL(Sub3.emails, 0)
FROM (SELECT DISTINCT DATE(timestamp) AS date
FROM emails
WHERE timestamp >= now() - interval 1 month) Sub1
LEFT OUTER JOIN (SELECT DATE(timestamp) AS date, COUNT(id) AS emails
FROM emails WHERE timestamp >= now() - interval 1 month
GROUP BY DATE(timestamp)) Sub2
ON Sub2.date = Sub3.date
LEFT OUTER JOIN (SELECT DATE(timestamp) AS date, COUNT(*) AS emails
FROM emails
WHERE timestamp >= now() - interval 1 month AND nyhedsbrev = 1
GROUP BY DATE(timestamp)) Sub3
ON Sub1.date = Sub3.date
(you can probably optimise one subselect of this away, but I have done it in full to make it obvious how it is working)
Assuming newsletter is boolean 1/0 values then this might give you the table that you want:
SELECT DATE(timestamp) AS date, COUNT(*) AS emails, nyhedsbrev
FROM emails WHERE timestamp >= now() - interval 1 month GROUP BY DATE(timestamp),nyhedsbrev ;
Just adding another GROUP BY parameter.

get the count value for this week

i have to use below query for get the count value on today.
select *
from orders
where status='P'
AND DATE_FORMAT(FROM_UNIXTIME(date),'%Y-%m-%d')=CURDATE();
it is successfully displayed count value.but get the count value for this month means have to use this below query.
select * from xcart_orders where status='C' AND DATE_FORMAT(FROM_UNIXTIME(date),'%m')=DATE_FORMAT(CURDATE(),'%m');
it is successfully displayed correct count value.
How is get the count value for this week..please help me.how is write the query for this week
You need to remove:
DATE_FORMAT(FROM_UNIXTIME(date),'%Y-%m-%d')= (MONTH(date)
your query should be:
select *
from orders
where status='P' AND
(MONTH(FROM_UNIXTIME(date)) = MONTH(CURDATE()) AND
(YEAR(date) = YEAR(CURDATE());
Just modifying your query a little, this should work:
select * from orders
where status='P'
AND MONTH(date) = MONTH(CURDATE())
AND YEAR(date) = YEAR(CURDATE());
You could rewrite this as:
select * from orders
where status='P'
AND EXTRACT(YEAR_MONTH FROM date) = EXTRACT(YEAR_MONTH FROM NOW())
The following works for me, try it and let me know
SELECT * FROM TABLE_NAME WHERE date_format(FROM_UNIXTIME(date_field), '%u')=date_format(now(), '%u')

Mysql - Subquery with date and like

I have a table with a column date (type mysql date : yyyy-mm-dd)
I have done this request:
Select Month(MAX(date)) AS last_month, YEAR(MAX(date)) AS last_year FROM mytable
to get the last year and last month.
I want for this last year and last month select all data in one request.
How can I use something like:
Select * from mytable where date like 'last_year-last_month-%'
this in sub-query ??
Is the table being update continuously?
If so, then you can go without the subqueries since they will slow the query down by quite a lot.
This selects everything from the current month.
SELECT * FROM mytable
WHERE MONTH(date) = MONTH(NOW())
AND YEAR(date) = YEAR(NOW())
ORDER BY date ASC
And this one selects everything from the current and last year.
SELECT * FROM mytable
WHERE YEAR(date) > YEAR(NOW())-1
ORDER BY date ASC
This should do the trick (maybe not the best performing solution though):
SELECT * FROM mytable
WHERE MONTH(date) = (SELECT MONTH(MAX(date) FROM mytable))
AND YEAR(date) = (SELECT YEAR(MAX(date) FROM mytable));
EDIT: Corrected brackets.
Thanks for alls,
here is the good request :
SELECT * FROM mytable
WHERE MONTH(date) = (select Month(MAX(date)) FROM mytable)
AND YEAR(date) = (select YEAR(MAX(date)) FROM mytable)
ORDER BY date ASC