In the sales table I have a field called end_date (date type),
I want to display the result if the end_date is current month.
also If the end_date is future date then dispay the result if end_date is 1 month before current month.
How to get that to work in SQL query?
For example like that:
select *
from sales
where DATE_FORMAT(date_end, '%Y%m') = DATE_FORMAT(now() - interval 1 month, '%Y%m')
Returns all rows where end_date is current month or any day earlier (after your comment: "Also if end_date like 4 months ago, it should display as well." – user791022):
select * from sales where end_date < adddate(last_day(now()), 1)
ps. I don't have a MySQL right now so I can't test it.
Related
From this database, I want to select those data which payment_date is current month but the service_date is any month except this month.
Database sample
One way to check if 2 dates are in the same month is with the use of the function last_day(), because if for 2 dates the last day of their month is the same then they are in the same month:
select *
from tablename
where last_day(payment_date) = last_day(curdate())
and last_day(service_date) <> last_day(curdate())
If you want the results for any month and not for the current month only:
select *
from tablename
where last_day(payment_date) <> last_day(service_date)
How to get values from year to date ?I need to display 2 values from ytd current year and previous year without changing query in future. As i undertand, ytd starts from first day of January. For example; from '2018-01-01' until current month of the year, and another query will be for ytd previous year.
Use:
SELECT * FROM TABLE WHERE DATE > DATE_FORMAT(NOW(), '%Y/1/1') AND DATE < NOW();
For this Y2D and:
SELECT * FROM TABLE WHERE DATE > (SELECT DATE_FORMAT((SELECT date_sub(DATE_FORMAT(NOW(), '%Y/1/1'), interval 1 year) AND DATE < (SELECT DATE_FORMAT((SELECT date_sub(NOW(), interval 1 year);
For last years Y2d
I want to display last 12 months sales in a chart. SQL table has year and month field and not a combined date field.
Im not able to give the interval condition of 12months on Year field.
SELECT s_month,s_year,SUM(s_amount) FROM table
WHERE s_month >= Date_add(now(),interval - 12 month)
AND s_year >= Date_add(now(),interval - 12 month)
GROUP BY s_year,s_month
One method is:
select s_year, s_Month, sum(s_amount)
from t
where date(concat_ws('-', s_year, s.month, 1)) >= curdate() - interval 12 month
group by s_year, s_month;
You may want to adjust the date arithmetic, depending on whether you want the date from 12 months ago.
If you want the last 12 months in the data, you can do:
select s_year, s_month, sum(amount)
from t
group by s_year, s_month
order by s_year desc, s_month desc
limit 12;
This is a strong argument against storing date parts (month, year) in separate columns.
The WHERE clause you have does not do what you expect!
It is virtually always better to have a DATE column (or TIMESTAMP or DATETIME) and use date functions as needed to split it apart.
SELECT MONTH(dat), YEAR(dat), SUM(amount)
FROM table
WHERE dat >= CURDATE() - INTERVAL 12 MONTH
GROUP BY LEFT(dat, 7) -- eg, "2017-12"
There is another problem with your query. SUM(amount) will have a partial month at either end. I can't solve that for you without better understanding where the data comes from and when. If it is already a single reading stored on the first of the month, then no problem. If it is daily or hourly amounts, then my point stands.
Suppose I have a table with 3 columns: EMPLOYEE_ID, NUM_SALES, DATE. Simply this is the table of Employees indicating daily sales. For each row in the table, I try to compute this; average number of sales of that EMPLOYEE_ID in the last K days excluding this day.
How can I query this in MySQL? I try to group by with EMPLOYEE_ID and DATE but I cannot figure out how to find last K sales for each row.
To select an interval of days, you can use MySQL's DATE_SUB() function:
WHERE `date` >= DATE_SUB(NOW(), INTERVAL 3 DAY)
This will select all records that are from the past 3 days. However, to exclude "today" from that:
WHERE `date` BETWEEN
DATE_SUB(NOW(), INTERVAL 3 DAY)
AND DATE_SUB(NOW(), INTERVAL 1 DAY)
After that you should be able to GROUP BY the employee_id to get what you're after:
SELECT
employee_id, avg(num_sales) AS avg_num_sales
FROM
employee_table
WHERE `date` BETWEEN
DATE_SUB(NOW(), INTERVAL 3 DAY)
AND DATE_SUB(NOW(), INTERVAL 1 DAY)
GROUP BY
employee_id
You need to be able to select items from your table, let's call it dailysale, by date.
Here's what you do.
SELECT employee_id, AVG(num_sales) AS avg_sales
FROM dailysale
WHERE date >= CURDATE() - INTERVAL 3 DAY
AND date < CURDATE()
GROUP BY employee_id
This uses two WHERE clauses to winnow down the date range you're using. date >= CURDATE() - INTERVAL 3 DAY excludes all records before midnight three days ago, and date < CURDATE() excludes all records on or after midnight today.
You need to use CURDATE() rather than NOW() because, well, NOW() includes the date and the present time of day. date < NOW() will include today's sales, because your date column only records dates and not times.
If you want to list the employees in order of sales, you could add
ORDER BY AVG(num_sales) DESC, employee_id
to the query.
I have the following columns in my table Log:
year, month, day, info
I need a query that selects the rows in a range of date determined by the user. The user will select the initial day, month and year and also the final day, month and year. At the moment, my query is:
SELECT
CONCAT(LPAD(`day`,2, 0),'/',LPAD(`month`,2, 0),'/',`year`) AS data,
info
FROM
Log
WHERE
(year > :initial_year OR (year = :initial_year AND month >= :initial_moth AND day >= :initial_day))
AND (year < :final_year OR (year = :final_year AND month <= :final_month AND day <= :final_day))
GROUP BY
year, month, day
ORDER BY
year DESC, month DESC, day DESC
But this query doesn't display any results, even that they are in the database! What is wrong and how can I fix it?
Your logic is wrong:
WHERE (
year > :initial_year OR (
year = :initial_year AND month >= :initial_moth AND day >= :initial_day
)
)
Will exclude any dates in your initial year where the day portion is greater than the initial day portion. e.g. yyyy-01-31 as the initial day will exclude all results for yyyy where the day portion is not 31.
Similar problems exist with the final date.
As suggested in the comments, use one DATE field in your database and do the other fiddling in your application code; it will save a lot of drama.
If you can't change the database, find and berate the person who designed it until they change it. If you can't do that then:
WHERE (year>:initial_year OR (year=:initial_year AND (month>:initial_month OR (month=:initial_month AND day>=:initial_day))))
and similar for the final date
What about:
select
concat(year,month,day) as thedate, info
from
log
where
thedate >= :startdate and thedate <= :enddate
order by
thedate desc;