This question already has answers here:
How to get first day of every corresponding month in mysql?
(15 answers)
Closed 5 years ago.
Let's say that I have an input date of "2017-01-31" or "2017-02-28". I want to take this input date and make SQL change it to "2017-01-01" or "2017-02-01".
Is there a way to do this via MySQL functions in a query?
Several ways to do that. My preference is to use DATE_FORMAT to replace the day portion with constant 01.
SELECT DATE_FORMAT('2017-01-31','%Y-%m-01') + INTERVAL 0 DAY AS dt
There are lots of ways to skin that cat.
For example, we could subtract the integer number of days minus 1 ...
SELECT '2017-01-31' + INTERVAL 1-DAY('2017-01-31') DAY
(With the second form, the date value needs to be supplied twice. With the first, we only need to supply the value one time. I think the first form is easier for a future reader to understand... pretty clear what the author is intending.)
Use your date instead of my example:
SELECT CONCAT_WS('-',YEAR('2017-01-28'),MONTH('2017-01-28'),'01')
Related
This question already has answers here:
Interleave rows of MySQL table
(2 answers)
Closed 5 years ago.
I have a time stamped data for specific dates in my mysql database. Whenever I retrieve the data in different ways, this is what I usually get:
I'm trying to get this query to look like this
What I want is to return different/distinct rows for date1 while keeping time ordered. If you look at the desired result, you will see that date1 starts with Sep then Oct then goes back to Sep, Oct etc. I hope this makes it clear.
Note: This is just an example, the real data have four different dates in date1 column so I'm expecting every four rows to have a different entry of date1
If I understand your question correctly, you just need to specify an order by clause.
select
...
from
...
where
...
order by
date2
, time
This question already has answers here:
ROW_NUMBER() in MySQL
(26 answers)
Closed 8 years ago.
I have a table that tracks the activity in several websites. Each row is of the following form: (Date, Hour, Website, Hits)
The Hour field is a number between 0 and 23 and represents an entire hour (for example, 22 is for any hits between 22:00 and 22:59).
I want to find the overall slowest hour for each website, meaning the input should be something like (Website, Hour).
In order to do that, I was thinking I should have a nested query to find the minimum hits for each website on each day, and then count the values of Hour (again, for each website on each day), and see which value is the maximal.
I'm still new to SQL so I'm having difficulties using the min() function properly, to find the minimal value only for a specific date and website. Then I have the same problem with using count() for a specific website.
I'm also curious if I can get not just the most common slowest hour, but maybe the 3 slowest, but at least to me it seems like it's really complicating the problem.
For the first nested query, I considered something like this:
SELECT DISTINCT Date Date_t, Website Website_t, Hour,
(SELECT min(Hits) from HITS_TABLE WHERE Date=Date_t and Website=Website_t) as MinHits
FROM HITS_TABLE
But not only it takes an abnormally long time to calculate, it also gives me multiple entries of (Date_t, Website_t, Hour, min(Hits)) for each value of Hour, so I take it that I'm not doing it in the smartest, nor the most efficient way.
Thanks in advance for any help!
You can get the minimum hour using a trick in MySQL:
select website, substring_index(group_concat(hour order by hits), ',', 1) as minhour
from table t
group by website;
For each website, this constructs a comma-delimited list of hours, ordered by the number of hits. The function substring_index() returns the first row.
This is something of a hack. In most other databases, you would use window/analytic functions, but these are not available in MySQL.
EDIT:
You can do this in standard SQL as well:
select t.*
from table t
where not exists (select 1
from table t2
where t2.hour = t.hour and
t2.hits < t.hits
);
This is interpreted as: "Get me all rows from the table where there are no other rows with the same hour and a lower number of hits." This is a round-about way of saying: "Get me the hour with the minimum value." Note that this will return multiple rows when there are ties.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
how do I get month from date in mysql
I want to get month using date example 2011-04-02 so I want month april. How to get this in MySQL?
SELECT MONTHNAME(date) AS monthName for January, February...
SELECT MONTH(date) AS monthName for 1, 2...
SELECT MONTHNAME(`date`) AS month_name FROM table_name;
You can use MONTHNAME() to get the month name. If you want month number, consider to use MONTH()
You can have a much more elegant solution to this if you use a second table as a date dimension table, and join your date field to it, in order to extract more useful information. This table can contain dates, month names, financial quarters, years, days of week, weekends, etc.
It is a really tiny table, only 365(ish) rows per year of data you have... And you can easily write some code to populate this table with as much data as you require. I did mine in Excel, exported as a CSV file and then imported the data into a blank table.
It also gives lots of benefits, for example, imagine a monthly data table with the following fields (and any others you can think of!) fully populated for all the months in a given range;
Date (E.g. 2009-04-01)
Day (E.g. 1)
Day of Week (E.g. Wednesday)
Month (E.g. 4)
Year (E.g. 2009)
Financial Year (E.g. 2009/10)
Financial Quarter (E.g. 2009Q1)
Calendar Quarter (E.g. 2009Q2)
Then combining this with your own table as follows;
SELECT `DT`.`monthName`
FROM `your_table`
INNER JOIN `dateTable` as DT
ON `your_table`.`your_date_field` = `dateTable`.`theDate`
There are many other nice outputs that you can get from this data.
Hope that helps!
This question already has an answer here:
MySQL - Thousands separator
(1 answer)
Closed 9 months ago.
I have a query:
select sum(invoiceamount) as invoice
from fact_salescount
where year in ({YEAR})
and month >= ({FROMMONTH})
and month <= ({TOMONTH})
This query can return a value from 100.00 to 15034115.93. It will return ONE value.
I would like to add, for each 000, like this: 15,034,115.93
I've seen a lot of similar questions, but none match mine. I hope someone can help me out.
I am using Pentaho and MySQL, and creating these queries within the Design Studio.
SELECT FORMAT(sum(invoiceamount),2)
FROM fact_salescount
WHERE year IN ({YEAR})
AND month >= ({FROMMONTH})
AND month <= ({TOMONTH})
This should do what you want, but I still don't like formatting number in the backend.
I have several rows in a table, each containing a start date and an end date. The user has a checkbox for each month of the year. I need to determine which rows contain a date range that includes any of the user's chosen months.
It's easy to check the start & end months by, for example, MONTH(start_date) IN ($month_list), but this approach won't match any months between the two dates.
So I suppose what I'm asking is: is there a way of obtaining the inclusive months from a date range purely in SQL?
I assume you would want to include data rows where the date range spans or intersects with the selected periods - in which case, I'd shove the user selected periods into a table and do a fuzzy join, something like.....
SELECT DISTINCT at.*
FROM a_table at, user_periods up
WHERE at.start_date<=up.end_date
AND at.end_date>=up.start_date
AND up.trans_id=$SOME_VAR
(the trans_id just allows the table to be used for multiple operations)
To minimise the effort here, the user_periods table should have an index on start_date and end_date, and similar for a_table.
Can something like this help?
WHERE
MONTH(start_date) < MONTH_YOU_ARE_CHECKING and
MONTH() > MONTH_YOU_ARE_CHECKING
If you need to check all at once you can do a list of all the months and after delete from the list the month that the user choose, and after compare against the list. It will be better with a pseudocode example :)
MONTHS = 1,2,3,4,5,6,7,8,9,10,11,12
USER_SELECTED_MONTHS= 1,6,8,9,12
LIST_TO CHECK = 2,3,4,5,7,10,11
so, now you can do:
MONTH(start_date) NOT IN (2,3,4,5,7,10,11)
What do you think, could it help you?
regards