MYSQL query for data between two dates,table does not have date column

DevG

I have a table with four columns

 DAY |MONTH | YEAR | SUM_FILE_SIZE
----------------------------------------

How do i select sum_file_size from this table for a date range between 'day 1 month 7 year 2013' to 'day 17 month 9 year 2014'?

I have tried this query and it is giving incorrect data.

select day, month, year, sum_file_size
from bermuda_stat.fileandcite_daily
where day between 1 and 17 and month between 7 and 9 and year between 2013 and 2014
group by day, month, year
order by year, month, day.

How do i get all the sum_file_size between this two date range?

Please note that it is excluding all days other than whatever dates falls between 1 and 17 ,similarly it is excluding all months other than whatever months falls between 7 and 9?

Thanks.

AdamMc331

Please see this list of helpful date and time functions. The first one you should notice(for this problem) is the MAKEDATE() function. If you give this a constant 1, it can be used to get the first day of a given year. So, for you example this would be:

SELECT MAKEDATE(year, 1) AS firstDayOfYear
FROM myTable

Now, you can use the ADDDATE() function to add the rest of your information. You can add the day like this:

SELECT ADDDATE(MAKEDATE(year, 1), INTERVAL (day - 1) DAY) AS dayAndYear
FROM myTable;

The reason I used (day-1) is because we already have a day of 1 from makedate. So 01-01 + 3 gives you 01-04 which is incorrect.

The month part works the same way, building on the last date.

SELECT ADDDATE(ADDDATE(MAKEDATE(year, 1), INTERVAL (day - 1) DAY), INTERVAL (month - 1) MONTH) AS fullDate
FROM myTable

Now, you can implement that into your where statement. Since you know the dates you want to restrict, you can treat those like dates. The final query looks like this:

SELECT SUM(filesize)
FROM myTable
WHERE (ADDDATE(ADDDATE(MAKEDATE(year, 1), INTERVAL (day - 1) DAY), INTERVAL (month - 1) MONTH))
  BETWEEN '2013-07-01' AND '2014-09-17';

Here is an SQL Fiddle example. I left each part in there, so you can see how the breakdown works step by step.

This question might also be helpful.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Mysql Query data between date on joined table

From Dev

MYSQL: Query data between two dates using date_format doesn't work

From Dev

Select Data frame between two dates of a date column

From Dev

mySQL query between two dates and two times

From Dev

MySQL query table to get data between start and end date

From Dev

MySQL query to search in between two time range between two dates using timestamp data

From Dev

Mysql between query for two date is not working properly

From Dev

how to fetch data between two dates(From Date, To Date) tables in MYSQL?

From Dev

MySQL query to find if a value of one column in one table is between two values in two columns on another table

From Dev

MySQL: Select all dates between date range and get table data matching dates

From Dev

How to MySQL query between two dates when the result holds only the start date from which the attribute is valid?

From Dev

Query to select data between two dates

From Dev

data between two dates in column for each day

From Dev

SQL query to find all Data by date range which falls between two dates

From Dev

MySQL: Select value where date between two dates. If date/value does not exist display date and value 0

From Dev

Spotfire - Getting data from one table that falls between two dates in another table and adding to a calculated column

From Dev

How to group date between two past date in one column MYSQL

From Dev

mysql random update date column between two date with condition

From Dev

check given date exists between two date column in mysql

From Dev

php mysql If I do not have the same data between two tables, can I print the other table data?

From Dev

Laravel - query data from mysql between dates

From Dev

Mysql : select from five different table where matching only one column based on a date or between dates

From Dev

Retrieve data between two dates in mysql

From Dev

Need to set difference of two dates fields in other column inside same table using single MySQL query

From Dev

SQL query to get a list of date ranges by month between two dates

From Dev

How to get results from the query if the selected date is between two dates?

From Dev

To Check whether a date is between two dates in column in MongoDB using JAVA

From Dev

Finding in between which two dates in a column the target date is

From Dev

To Check whether a date is between two dates in column in MongoDB using JAVA

Related Related

  1. 1

    Mysql Query data between date on joined table

  2. 2

    MYSQL: Query data between two dates using date_format doesn't work

  3. 3

    Select Data frame between two dates of a date column

  4. 4

    mySQL query between two dates and two times

  5. 5

    MySQL query table to get data between start and end date

  6. 6

    MySQL query to search in between two time range between two dates using timestamp data

  7. 7

    Mysql between query for two date is not working properly

  8. 8

    how to fetch data between two dates(From Date, To Date) tables in MYSQL?

  9. 9

    MySQL query to find if a value of one column in one table is between two values in two columns on another table

  10. 10

    MySQL: Select all dates between date range and get table data matching dates

  11. 11

    How to MySQL query between two dates when the result holds only the start date from which the attribute is valid?

  12. 12

    Query to select data between two dates

  13. 13

    data between two dates in column for each day

  14. 14

    SQL query to find all Data by date range which falls between two dates

  15. 15

    MySQL: Select value where date between two dates. If date/value does not exist display date and value 0

  16. 16

    Spotfire - Getting data from one table that falls between two dates in another table and adding to a calculated column

  17. 17

    How to group date between two past date in one column MYSQL

  18. 18

    mysql random update date column between two date with condition

  19. 19

    check given date exists between two date column in mysql

  20. 20

    php mysql If I do not have the same data between two tables, can I print the other table data?

  21. 21

    Laravel - query data from mysql between dates

  22. 22

    Mysql : select from five different table where matching only one column based on a date or between dates

  23. 23

    Retrieve data between two dates in mysql

  24. 24

    Need to set difference of two dates fields in other column inside same table using single MySQL query

  25. 25

    SQL query to get a list of date ranges by month between two dates

  26. 26

    How to get results from the query if the selected date is between two dates?

  27. 27

    To Check whether a date is between two dates in column in MongoDB using JAVA

  28. 28

    Finding in between which two dates in a column the target date is

  29. 29

    To Check whether a date is between two dates in column in MongoDB using JAVA

HotTag

Archive