Return SQL query in specific chunks? [duplicate] - mysql

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

Related

Go Back To Beginning Of Month On Input Date MySQL [duplicate]

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')

SQL query to find the most common index of a minimal field [duplicate]

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.

MySQL - is it possible to display empty rows? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Displaying zero valued months with SQL
For a certain attribute, is it possible to display its rows even if its empty?
Say I have attribute date_of_car_sale. Also, let's say I want to do a monthly report.
From January to December of 2009, there's two months where the company has been away on holiday, so there's no date_of_car_sale for June and August.
Is it possible to make a report that displays every month of 2009, even if June and August has no data value?
I can make every month show and by grouping them to show individual months only. But can't seem to get June and August to display because they're empty.
PS: This only requires one column from the table Company.
In cases like this, I usually make a table with all values I want displayed and do a left join to my data set. So in your case, I would make a table called "months" with a single date column that stores the first day of the month or maybe two columns with the first and last day and do join like this:
SELECT m.month_first, COUNT(x.date_of_car_sale) as sales
FROM months m
LEFT JOIN (
SELECT date_of_car_sale
FROM sales
) x ON m.month_first <= x.date_of_car_sale
AND m.month_last >= x.date_of_car_sale
GROUP BY m.month_first
ORDER BY m.month_first

mysql getting all possible months from datetime field

so i have a table with hundreds records. And a have a filed name "created" type with a datetime format. Now I want to make and archive with the months. For example January, February.... etc. I need to create query to find all possible months. For example if my records start from 2011/05/01 to now I will need to fetch the months that means months 5,6,7,8,9,10,11,12.
Is there a way to that ???
If you are looking at the list of all Months present in the created field (as I understand your query) then do this:
SELECT DISTINCT(MONTH(created)) FROM posts;
The resulting set would be the list of unique months in the field. If this will complain then try:
SELECT DISTINCT(MONTH(DATE(created))) FROM posts;
You can then substitute MONTH for MONTHNAME and get names instead. I did not add the WHERE clause to these queries but you can limit the dataset you are looking at as you see fit.
For more information take a look at http://dev.mysql.com/doc/refman/5.1/en/functions.html this has a list of quite a few functions that MySQL natively provides.
Yes, use the DATE_FORMAT function and other date and time functions.
More details here
For example, if you want all your records for December 2011:
SELECT * FROM posts WHERE YEAR(created) = 2011 AND MONTH(created) = 12

How to get month using date in MySQL [duplicate]

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!