Mysql query fetching record between two months - mysql

I am looking for query which fetch record between two months.
It looks simple but I am looking for a query which fetch record for two different months.
ie.
January and April
query should show result from the month of January and April but it should not show result for February and march.

Try
SELECT * from table_name where month(date_field) = 1 or month(date_field) = 4
this will show all results for january and april, but for all years. to filter year, add another check in query
SELECT * from table_name where (month(date_field) = 1 or month(date_field) = 4) and year(date_field) = 2016

SELECT * FROM data where (data.createdAt BETWEEN "2016-01-01" AND "2016-01-31") OR (data.createdAt BETWEEN "2016-04-01" AND "2016-04-31")

Related

Retrieve ALL row data from TWO different months in SAME table?

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?

Search data on basis of month and date Mysql

select * from table where 1 and DATE_FORMAT(dateColumn,"%d-%m") BETWEEN "01-09" and "30-09"
This query is returning all the data. I just want the data between these dates, without taking into account the year.
In the table dateColumn is yyyy-mm-dd.
You need to do the comparison as MM-DD, not DD-MM:
select *
from table
where DATE_FORMAT(dateColumn, '%m-%d') BETWEEN '09-01' and '09-30'
Or, for this case, you could just do:
where month(dateColumn) = 9
Try this:
SELECT *
FROM table
WHERE MONTH(dateColumn) = 9
You can check if the month is September
select * from table where DATE_FORMAT(dateColumn,"%m")
= '09
If you want rows from your table where the month of your date is 9 and the day of month is between 1 and 30, you can actually do this quite easy and more explicit:
SELECT
*
FROM
table t
WHERE
MONTH(t.date) = 9 and
DAY(t.date) BETWEEN 1 and 30
It's also easier to understand.

SQL statement, get query RSQLite

I am new to SQL and having trouble with RSQLite.
Here is an example of my table:
counts Month
0 June
4 June
2 March
5 July
3 July
I would like to create a search query using dbGetQuery that will count up the number of counts for each month from my totals table. Im looking for output that looks like this:
counts Month
4 June
2 March
8 July
So far I have this but is incorrect. dbGetQuery(conn=db, "SELECT Count(counts) FROM totals group by [Month]")
From your desired output seems that you don't want to count the counts but to sum them. As stated by #AlexK, you also need Month in your select to have it displayed along with the sum result
dbGetQuery(conn=db, "SELECT [Month], sum(counts) as counts FROM totals group by [Month]")

How can I get the month that is not yet updated in SQL by inserting another row on every update?

I have a table that contains records of different transaction that is needed to be updated monthly. Once the record for a specific month has been successfully updated, it will insert a new record to that table to indicate that it is already updated. Let's take this example.
**date_of_transaction** **type**
2015-04-21 1 //A deposit record
2015-04-24 2 //A withdrawal record
2015-04-29 1
2015-04-30 2
2015-04-30 3 //3, means an update record
2015-05-14 1
2015-05-22 1
2015-05-27 2
2015-05-30 2
2015-06-09 1
2015-06-12 2
2015-06-17 2
2015-06-19 2
Let's suppose that the day today is July 23, 2015. I can only get the data one month lower than the current month, so only the data that I can get are june and downwards records.
As you can see, there is an update performed in the month of April because of the '3' in the type attribute, but in the month of May and June, there are no updates occurred, how can I get the month that is not yet updated?
This will return you months, which has no type=3 rows
SELECT MONTH([trans-date]) FROM [table] GROUP BY MONTH([trans-date]) HAVING MAX([trans-type])<3
Note: this will not work if 3 is not max value in the column
My approach would be to find all the months first, then find the months whose records were updated. Then select only those months from all months whose records werent updated (A set minus operation).
Mysql query would be something like this
select extract(MONTH,data_of_transaction) from your_table_name where month not in (select extract(MONTH,data_of_transaction) from table where type=3);
You can try this;
select *
from tbl
where date_of_transaction < 'July 23, 2015'
and
date_format(date_of_transaction, '%M-%Y') in (
select
date_format(date_of_transaction, '%M-%Y')
from tbl
group by date_format(date_of_transaction, '%M-%Y')
having max(type) != 3
)
date_format(date_of_transaction, '%M-%Y') will take month-year in consideration and filter the data having type = 3.

In SQL date function for before 2 months

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