There is an existing Dates dimension table which I need to query.
The structure looks like this:
ID | Year | Month | Day
How can I query for a range of dates?
Example: I want to get the dates from 03/02/2018 to 03/02/2020
I've tried the following:
SELECT *
from Dates dateDim
where (dateDim.Year >= 2018 and dateDim.Month >= 2 and dateDim.Day >= 3)
and (dateDim.Year <= 2020 and dateDim.Month <= 2 and dateDim.Day <= 3);
With standard SQL you can compare tuples (=multiple columns) with a single expression:
select *
from dates
where (year, month, day) >= (2018,2,3) and (year, month, day) <= (2020, 1, 3);
Not all DBMS products support that however, but you didn't mention a specific DBMS product and the tag sql refers to "standard SQL".
Online example: https://rextester.com/NTZH63192
But you should really consider change that to a single column of type DATE.
SELECT *
from Dates dateDim
where dateDim.Year * 10000 + dateDim.Month * 100 + dateDim.Day
between 20180203 and 20200203
As has been commented, change your Dates table so it actually holds DATE types. You can easily retrieve the year/month/day from a date. Most typical operations that involve your date table will involve conversion to a date, then some operation (like adding days/months etc) then converting back. If you store these thigns as dates you save that first step. Over the life of the DB storing it properly will save you more than whatever spurious reason they're stored like this is intended to help with
You could also do this mathematically (or stringily):
SELECT * FROM dates WHERE year * 10000 + month * 100 + day BETWEEN 20180203 and 20200103
But it's a bit of a lame hack compared to storing the data properly in the first place
If you are using MS-SQL, I would recommend, if Year, Month and Day are INT's, converting these to a DateTime and you have much more control, something like this should suffice:
SELECT
*
FROM
Dates.dateDim AS foo
WHERE
CAST(CAST(foo.Year AS varchar) + '-' + CAST(foo.Month AS varchar) + '-' + CAST(foo.Day AS varchar) AS DATETIME) BETWEEN '2018/02/03' AND '2020/02/03'
Related
I am working with MySQL.
I have some queries that begin like this:
WITH dates (start_date, end_date) AS (
SELECT '2020-11-01', '2020-11-02'
UNION ALL SELECT '2020-11-02', '2020-11-03',
UNION ALL SELECT '2020-11-03', '2020-11-04')
SELECT dates.start_date, dates.end_date, id, COUNT(*)
FROM dates
INNER JOIN ...
I also sometimes need to run the same query, but with each date range being a week (Monday to Sunday), or a calendar month. Moreover, sometimes there are 100 or more of these, which is quite prone to typos in addition to occupying a lot of lines and taking a long time to write.
Is there more elegant and flexible way to achieve this ? Ideally I would like to be able to just specify a overall start date, an overall end date and a period (daily, weekly, monthly, yearly etc)
You can use a recursive CTE:
with recursive dates as (
select date('2020-11-01') as start_date, date('2020-11-02') as end_date
union all
select start_date + interval 1 day, end_date + interval 1 day
from dates
where start_date < '2020-12-01'
)
select *
from dates;
Here is a db<>fiddle. Of course, the logic would be a little different for months.
Create some date_ranges table which stores: series identifier, range number, range boundaries.
Insert needed series into it each time when you need a series which is not present in this table yet.
Use this table as source table in your query (maybe with additional condition which narrows the range if needed).
Remove series which will be used never in future.
I have table that contains first three columns day, month, year. I have kept it separated instead of making single column date, because this table contains information about subscription analysis. So by keeping separated I can analyze data on daywise. Now I have situation where I want to reset few data. Example like I want to reset data which day, month, year comes between date 2008-01-01 and 2010-10-01. So how can I make where condition in mysql query for separated day, month and year columns?
Please try the following...
WHERE STR_TO_DATE( CONCAT( yearValue,
'-',
monthValue,
'-',
dayValue ) ) BETWEEN '2008-01-01' AND
'2010-10-01'
This clause uses the CONCAT() function to form a string representation of the date being examined, then the STR_TO_DATE() function to convert that string to a DATE value which is then compared to the range specified.
If you have any questions or comments, then please feel free to post a Comment accordingly.
Further Reading
https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#operator_between (on MySQL's BETWEEN operator)
https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_concat (on MySQL's CONCAT() function)
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date (on MySQL's STR_TO_DATE() function)
You should really store the value as a date. But you can still do what you want. Here is one way:
where year * 10000 + month * 100 + day between 20080101 and 20101001
Note that values are all integers. A date would be better because the query could take advantage of an index.
You may concatenate your three INT columns with the TO_CHAR function. The transform that into a date using TO_DATE, and then compare.
WHERE TO_DATE(TO_CHAR(year || month || day), 'yyyymmdd') BETWEEN '20080101' AND '20100101';
I have attendance data for employees stored in the table attendance with the following column names:
emp_id (employee ID)
date
type (leave, absent, etc.)
(there are others but I'm omitting them for the sake of simplicity)
My objective is to retrieve all dates of the given month on which the employee was on leave (type = 'Leave') and the last leave taken in the last month, if any.
It's easy to do it using two queries (I'm using PHP to get process the data), but is there any way this can be done in a single query?
I'm answering my own question so as to close it. As #bpgergo pointed out in the comments, UNION will do the trick here.
SELECT * FROM table_name
WHERE type="Leave" AND
date <= (CURRENT_DATE() - 30)
Select the fields, etc you want then se a combined where clause using mysql's CURRENT_DATE() function. I subtracted 30 for 30 days in a month.
If date is a date column, this will return everyone who left 1 month or longer ago.
Edit:
If you want a specific date, change the 2nd month like this:
date <= (date_number - 30)
I'm curious what the right way is to construct a query where the rows are pulled based on a timestamp that represents a specific month. Given that different months have different numbers of days, is there a way to generate a query that always gives you the rows where the timestamp contains the current month so that the results would only include the current month?
Do you mean something like this
SELECT * FROM tbl WHERE
MONTH(timesp) = MONTH(NOW()) AND
YEAR(timesp) = YEAR(NOW());
You can use the FROM_UNIXTIME() function:
SELECT *
FROM tableName
WHERE MONTH(FROM_UNIXTIME(timestampField))==6
Just use MONTH:
select *
from foo
where month_column = MONTH(getdate())
and year_column = YEAR(getdate())
Try this sql.
select *
from yourtable
where yourdatefield>=DATE_SUB(CURDATE(),INTERVAL 1 MONTH);
You're looking for something like this:
SELECT * FROM table where MONTH(date_row) = $month;
If you have an index on your date field, then this is efficient (T-SQL syntax, the idea applieas to any RDBMS though)
SELECT
*
FROM
tableName
WHERE
dateTimeField
BETWEEN
-- build the string 'YYYY-MM-01', cast back as a datetime
CAST(
CAST(YEAR(GETDATE()) AS varchar) + '-' + CAST(MONTH(GETDATE()) AS varchar) + '-01'
AS datetime
)
AND
-- add one month, subtract one day
DATEADD(mm, 1,
-- build the string 'YYYY-MM-01', cast back as a datetime
CAST(
CAST(YEAR(GETDATE()) AS varchar) + '-' + CAST(MONTH(GETDATE()) AS varchar) + '-01'
AS datetime
)
) - 1
Of course any other method to get two datetime values in the right range would work.
SQL Server has LEFT(CONVERT(varchar, GETDATE(), 120), 8) + '01' to convert a datetime to string, other Db servers have their own functions to do the same. Maybe you can calculate the two values in the calling application more easily - how you get them, is not the point.
The point is that BETWEEN can use an index, whereas the other solutions that work with WHERE MONTH(dateTimeField) = 6 will trigger a table scan, which is about the slowest operation you can do on a table.
my dates in my table are strings in the format:
"10/12/2009"
Now how would one get all the records from a month, lets say June (number "6" being provided)?
Check the MySQL function STR_TO_DATE.
You should not store dates as string, however. Use the type DATE.
The short answer to your question is that you can use the STR_TO_DATE and MONTH functions to 1) convert the string representation into a DATE, and 2) extract the month component from the date:
SELECT t.*
FROM mytable t
WHERE MONTH(STR_TO_DATE(t.dateasstringcol,'%M/%d/%Y')) = 6
(This is assuming here that by '10/12/2009', you are specifying Oct 12, and not Dec 10. You'd need to adjust the format string if that's not the case.)
Alternatively, if month is indeed the leading part of the date, you could do a simple string comparison, if the month is the leading component:
SELECT t.*
FROM mytable t
WHERE t.dateasstringcol LIKE '6/%'
OR t.dateasstringcol LIKE '06/%'
(You could eliminate one of those predicates, if you have an exact format specified for the striing value representing the date: either if month is always stored as two digits -OR- the month is never stored with a leading zero.)
If you are passing in an argument for the month, e.g. '6', then you could construct your statement something like this:
WHERE t.dateasstringcol LIKE '6' + '/%'
If month is the second component of the date, then you could use:
SELECT t.*
FROM mytable t
WHERE t.dateasstringcol LIKE '%/' + '6' + '/%'
OR t.dateasstringcol LIKE '%/' + '06' + /%'
NOTE:
All of the previous example queries will return rows for June of any year (2009, 2010, 2011)
You can extend those examples, and do something similar with the year, using the YEAR function in place of the MONTH function, or for string comparison
AND t.dateasstringcol LIKE '%/%/2011'
Normally, we'd extract rows for a particular month for a particular year, using a date range, for example:
SELECT t.*
FROM mytable t
WHERE MONTH(STR_TO_DATE(t.dateasstring,'%M/%d/%Y')) >= '2011-06-01'
AND MONTH(STR_TO_DATE(t.dateasstring,'%M/%d/%Y')) < '2011-07-01'
Of course, when the date value is stored as a DATE datatype rather than as a VARCHAR, this means we don't need the STR_TO_DATE and MONTH functions, we can specify a comparison to the native DATE column. This approach allows us to make use of an index on the date column, which can improve query performance on large tables.
SELECT t.*
FROM mytable t
WHERE t.realdatecol >= '2011-06-01'
AND t.realdatecol < '2011-07-01'
The STR_TO_DATE function is your friend here:
SELECT * FROM my_table
WHERE STR_TO_DATE('10/12/2009','%M/%d/%Y') >= '2012-06-01';
MONTH should help here if we want current month or particular month data. e.g:
$month = date('m'); OR particular month.
SELECT * FROM users WHERE MONTH(str_to_date("10/12/2009",'%e/%m/%Y')) = $month;