I want select SQL query in where condition between month and year. in my database have field name months and years for store produce input of month and year. now I want search produce input in December,2015 to august 2016
This is my sql
SELECT * FROM tbl_produce p WHERE p.status =0 AND ((p.months >='12'
AND p.years>='2015') AND (p.months <='8' AND p.years<='2016)) ;
but result return NULL, I known my query not correct but how to correct it ?
Why not design your date column as date ?
If you want to keep your current design, try this:
SELECT *
FROM tbl_produce
WHERE status = 0
AND concat(years, if(months > 9, months, concat('0', months)))
BETWEEN '201512' AND '201608'
OR
SELECT *
FROM tbl_produce
WHERE status = 0
AND (
(years = 2015 AND months = 12)
OR
(years = 2016 AND months < 9)
)
Your SQL query is looking for result where p.months is equal to both 12 and 8, and where p.years is equal to both 2015 and 2016.
To achieve the result you are looking for, replace the AND between the two sets of parenthesis with OR, to look like so:
SELECT * FROM tbl_produce p WHERE p.status =0 AND ((p.months >='12' AND p.years = '2015') OR (p.months <='8' AND p.years = '2016));
If I had access to your table structure I would run the test myself to confirm, but I do believe this to be the correct syntax for the result you are looking for.
Related
Re-do Times 2 for clarity
For simplicity and clarity, the data I am aggregating is in a database I will refer to as "BaseA".
Normally, when comparing Month over month data, I can use the following query:
select date_trunc('month',hour) as date,
sum(a) as total_a,
sum(b) as available_b,
sum(c) as c,
sum(d) as net_d
from BaseA where id=12345 and hour >='2022-01-01'
group by date order by date desc
Instead of looking back and collecting ALL months from 2021-2022 for the duration I wish to view, I want to collect ONLY two months of data, those being the following:
October 2021
vs.
April 2022
I'd like the data to be visualized in the Month over month format, like so:
example
However, I would like to:
Select all BaseA columns (aka, select *)
Only include Two rows: April 2022 & October 2021
So, should come out like so:
example 2
This query is what Im trying to do (in word form since I can't write it)
Select *
from BaseA
where date
is in
April 2022
&
October 2022
The result of the above should result in 2 rows of data (one for each month referenced)
Is there a place in the below query where DISTINCT would make that actualized?
select * from BaseA
where id=12345 and
(
(month(month) = '04' and year(month) = '2022')
OR
(month(month) = '10' and year(month) = '2021')
)
--DISTINCT go where?
Appreciate the help!
To get data for the two months try something like this:
select * from BaseA
where _id=12345 and
(
(month(month) = '04' and year(month) = '2022')
OR
(month(month) = '10' and year(month) = '2021')
)
I have no idea what you mean by "return data consolidated (grouped) by month (so multiple lines occur per month)". Can you provide dummy data that illustrates the data that you have and the result that you want to achieve?
I am currently working on a MySQL db with MySQL Workbench.
My objective is to retrieve the signups from a database to establish my company's KPIs on an Excel spreadsheet.
I wrote some sql queries that worked but I want to set up a very complete one in order to avoid using xxx different queries.
To get the signups for each month (based on 'created_at'), this makes the job:
SELECT year(u.created_at) year, monthname(u.created_at) month, COUNT(DISTINCT u.id) as 'New shoppers signups'
FROM users u
GROUP BY year, month
ORDER BY u.created_at
But I also wanted to have the total of previous signups for each month
Jan : 12
Feb : 14 (12 + 2 new signups)
March : 22 (14 + 8 new signups)
...
Where I get the sum of all the previous signups
I was thinking about something like:
DECLARE #month = '2012-01-01' //startdate
WHILE #month < curdate()
BEGIN
SELECT count(distinct u.id)
WHERE u.created_at < #month
dateadd(month, 1, #month) // incrementing to next month
END
But neither the while loop, the declare, set, or date function do work on MySQL Workbench.
I heard I have to declare procedures but I didn't have any more success...
I know I could use excel to get the result, but I want to improve my use of SQL and make this a very clear work.
You are actually close to the answer. Take your results and make that an inner query. Then that is basis of an outer query using MySQL variables to accumulate for each row.
select
pq.yearAdded,
pq.monthAdded,
pq.NewShoppers as 'New shoppers signups',
#runBal := #runBal + pq.NewShoppers as TotalNewShoppers
from
( SELECT
year(u.created_at) yearAdded,
monthname(u.created_at) monthAdded,
COUNT(DISTINCT u.id) as NewShoppers
from
users u
GROUP BY
year(u.created_at),
monthname(u.created_at)
ORDER BY
year(u.created_at),
monthname(u.created_at) ) pq,
( select #runBal := 0 ) sqlvars
I would just suggest having column names stay away from possible reserved words, such as Year, Month and other standard SQL commands and function names... otherwise you typically need to add tick-marks around the column names
I want to retrieve the records of employees who were joined in first quarter or in the first month. I have tried this but am not getting the right answer...
SELECT * FROM table
WHERE DOJ(date_created) = DOJ(CURRENT_DATE - INTERVAL 1 MONTH)
Please help me with this!
Answering the question as clarified in a comment...
SELECT * FROM table
WHERE YEAR(table.doj) = 2015 AND QUARTER(table.doj) = 1
If instead you want "first quarter of prior year"...
SELECT * FROM table
WHERE YEAR(table.doj) = YEAR(CURRENT_DATE) - 1 AND QUARTER(table.doj) = 1
In either case, note that there's no code to include the first month, because that's part of the first quarter. However, if you wanted to make that explicit (at a slight performance hit), you could code it as follows...
SELECT * FROM table
WHERE YEAR(table.doj) = 2015 AND (QUARTER(table.doj) = 1
OR MONTH(table.doj) = 1)
If you run into performance problems because you have a lot of records but only an index on table.doj, you could also write the query over an explicit date range...
SELECT * FROM table
WHERE table.doj >= '2015-01-01' AND table.doj <= '2015-03-31'
I want to take all the records from a specific year.
My table is race_details and the year is a part of the date in race_date (in unix timestamp)
SELECT *
FROM race_details AS r
WHERE r.race_date=(SELECT MAX(YEAR(FROM_UNIXTIME(race_date)))
FROM race_details)
Unless there's more to the problem, I think you're overcomplicating your query:
SELECT *
FROM race_details AS r
WHERE YEAR(race_date) = 2014;
Substitute 2014 for the year to look up, and if it's a variable, remember to parameterize!
You need to compare the year of the race_date, not the whole race_date:
WHERE YEAR(race_date) = (SELECT ...)
I mentioned database table structure below,
name, expired_date, address, payment_date
----------------------
name1, 2013/06/02, address1,2013/07/23
name2, 2013/06/02, address2,2013/07/23
name3, 2013/04/02, address3,2013/07/23
name4, 2013/05/02, address4,2013/07/23
name5, 2013/06/02, address5,2013/07/24
...
name6, 2013/06/02, address6,.....
In this table I update date in yyyy/mm/dd format . current month is April but I need after two records only.
For example I need 06 month records only.
I need SQL query for this.
Assuming you want to query by expired_date:
Following query gets records after two months from current date:
SELECT * FROM tableName where expired_date>=DATE_ADD(CURDATE(), INTERVAL 1 MONTH)
If you specifically want records for June:
SELECT * FROM tableName where expired_date>='2013-06-01' AND expired_date<'2013-07-01'
Try it.
Select [fields] from [yourtable] where Month([yourDateField]) = [Value, ex: for June : 6]
Implement
I am assuming you want to check expired_date
Select * from Table1 where Month(expired_date) = 6
if you want to check year also then, assuming year is 2013.
Select * from Table1 where Month(expired_date) = 6 and Year(expired_date) = 2013