How to get the dates available in a mysql table - mysql

I am not very good with this, so I hope somebody can help me. I have a table filled with measurements. These measurements are taken every 30 seconds over the last 3 years, during most of the days. I would like to have a list with all the dates available in the table. So something like:
31//1/2010
1/2/2010
2/2/2010
4/2/2010
It does not matter how many measurements were taken on a certain day, as long as there is one then it should show the date. In the above example it shows that there are no measurements taken on 3/2/2010.
Is there a simple mysql select statement that can do this?
my table looks like this:
table1:
int id (auto, prim)
datetime measurementTime
double value1
double value2

SELECT DISTINCT DATE(`column_with_date_and_time`) AS `date`
FROM `table` ORDER BY `date`

Try this:
SELECT DISTINCT date_col FROM your_table
if your table stores date and time separately.
On the contrary, use this:
SELECT DISTINCT DATE(date_time_col) FROM your_table

Related

MySQL query to find rows with the same values in two columns, but different values in a third

I have a table with the following columns: ord_date, invoice, part, manufacturer, part_type
I want to be able to query the table to get a list of invoice numbers that have the same part, but different manufacturer for a particular ord_date and part_type.
I've tried something like:
select ord_date, invoice, part, manufacturer, part_type from my_table m
where exists (
select 1 from my_table m2
where ord_date like '2021-05-24%'
and part_type='car'
and m.invoice=m2.invoice
and m.part=m2.part
and m.manufacturer<>m2.manufacturer)
order by invoice;
This works, but takes a long time. Is there a way to optimize this?
where ord_date like '2021-05-24'
and part_type='car'
and m.invoice=m2.invoice
and m.part=m2.part
and m.manufacturer<>m2.manufacturer)
If ord_date is DATETIME, you are probably not getting what you want.
Perhaps you did not want and m.invoice=m2.invoice?
Add an index that contains the columns are tested by =.

How to select Point In Time record

I have a MariaDB 10.2.21.
My table contains records that include a 'ChangeDate' field like this: '2019-09-18 10:57:26'.
I want to do a SELECT based on a timestamp, and get the nearest previous record to return.
This allows me to do a 'point-in-time' selection providing me with field values as they were at that moment.
I seeked StackOverflow but do no recognize a proper solution.
Any hints? Thanks!
Try following Query
SELECT *
FROM my_table
WHERE ChangeDate < '2019-09-18 10:57:26'
ORDER
BY ChangeDate DESC
LIMIT 1

How to count the same date values per day

I'm completly begginer but and don't even know what type this query is.
BTW. I want to get count Date(MYdatetime) type values per day by join the same table.
Is there any question comparing this query?
I have query like that:
select
date_format(
adddate('2011-1-1', #num:=#num+1),
'%Y-%m-%d'
) date
from
any_table,
(select #num:=-1) num
limit
365
In MySQL, I would simply do:
select date(t.datecol), count(*)
from any_table t
group by date(t.datecol);
If you want data for a particular time span, use a where clause:
select date(t.datecol), count(*)
from any_table t
where t.datecol >= '2011-01-01' and t.datecol < '2012-01-01'
group by date(t.datecol);
Use GROUP BY:
SELECT
year(theDate),
month(theDate),
day(theDate),
count(*)
FROM
test_table
GROUP BY
year(theDate),
month(theDate),
day(theDate)
You are wanting a count of all dates in a given time frame, including those dates that are not in the table (and thus would have a count of 0). This is a bit of a headache but not unprecedented. You basically need to create a utility table that is just every date you could possibly want and then do an outer join against that table. So create a simple table like:
CREATE TABLE `all_dates` (
`the_date` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
And populate it with all dates you think you could potentially need. I think a 10 year radius from the current year would be good. So fill it with every date between 2008-01-01 and 2028-12-31.
INSERT INTO all_dates
(the_date) VALUES
('2008-01-01'),
('2008-01-02'),
('2008-01-03')
There is probably a clever way to generate all rows in one query using a procedure or somesuch, but I don't know what it would be, so I would personally just create a simple script (like in PHP) to generate those rows.
Once the all_dates table has all the date value rows for your needs, you can then do a query to get the count of each date (including missing dates) like:
SELECT DATE(all_dates.the_date), COUNT(any_table.datecol)
FROM any_table
RIGHT JOIN all_dates ON DATE(any_table.datecol) = all_dates.the_date
GROUP BY all_dates.the_date
HAVING all_dates.the_date BETWEEN '2011-01-01' AND '2012-01-01'

Order By Date Difference Between Two Date Columns

I have a table that has two datetime columns (one for start time and one for end time).
I need to be able to select all entries and order them based on the time difference between these two columns (the period between the start and end time columns)
Try this::
select * from table order by TIMEDIFF(to, from)
SELECT ...
FROM ...
ORDER BY DATEDIFF(endDate, starDate);
[edit]
The above query works with dates. As pointed out by Sashi Kant, it would ignore the time part of your DATETIME
On the other hand, TIMEDIFF fails if the difference is outside of the TIME range (-838:59:59 to 838:59:59).
A complete solution could be:
SELECT ...
FROM ...
ORDER BY
DATEDIFF(endDate, starDate),
TIMEDIFF(TIME(endDate), TIME(starDate));
Probably performs terribly, though... If such precision is required, and if the difference between the two dates may be outside of the TIME range, then splitting each of your DATETIME columns into two DATE and TIME columns would certainly perform better, assuming you would apply one index on each of the four resulting columns.
SELECT something FROM table ORDER BY TIMEDIFF(end_date, start_date);
There are mysql defined functions to compute the difference between two timestamps, like TIME_TO_SEC and TIMEDIFF.
SELECT TIME_TO_SEC(TIMEDIFF(from_time, to_time)) diff from your_table order by diff;
This is correct: select * from table order by TIMEDIFF(to, from)
But if you are not selecting timediff in select statement you might have to add this line of code:
SET sql_mode = '';
select * from table order by TIMEDIFF(to, from)
to disable ONLY_FULL_GROUP_BY mode
For anyone who just needs the time distance between two timestamps, irrespective or without having to know which date is earlier and which date is later, you could use the following:
SELECT t.*,
TIMEDIFF( t.date_column, t.other_date_column ) AS distance
FROM table t
ORDER BY IF(distance<0, 0-distance, distance)
Or if you have a fixed/static target timestamp:
SELECT t.*,
TIMEDIFF( t.date_column, '2021-04-20' ) AS distance
FROM table t
ORDER BY IF(distance<0, 0-distance, distance)
The IF() function always returns a positive "distance" measurement of time between the two timestamps. This allowed me to find the nearest available record whether it was earlier or later than the target timestamp.

Get the closest value from mysql table

This might be a simple one, but since I don't have much knowledge about MySQL I don't know how to do this, This is what I basically want,
I have a query like this
//time format "yyyy-MM-dd"
SELECT ID
FROM `id_table`
WHERE time > "2012-01-05 " AND time < "2012-01-10";
But in the id_table I have data only up to 2012-01-04 then it starts again from "2012-01-20", so above query would return null. Is there a any way where I can retrieve the last data record from the table, as for this example can I get the ID of 2012-01-04 date from the table when I query like this
SELECT ID
FROM `id_table`
WHERE time > "2012-01-05"
Are you looking for the one (i assume max ID) ID of the row with the nearest time to 2010-01-05?
SELECT MAX(ID) as LastId FROM id_table
WHERE time = (SELECT MAX(time)
FROM id_table WHERE time < '2012-01-05')
Try this:
SELECT ID FROM id_table
WHERE time between
least((select max(time) from id_table), "2012-01-05") AND "2012-01-10";
Note that between will get data from "2012-01-10" and ("2012-01-05" OR "2012-01-05)
To get the record closest to the given time ( you could easily tweak this if you care about dealing with duplicates, if that doesn't matter then this alone should suffice)
SELECT
ID
FROM
id_table
ORDER BY ABS(DATEDIFF(time, '2012-01-05'))
LIMIT 1
NOTE: If the time field is a time value yyyy-mm-dd hh:mm:ss then you can use TIMEDIFF for a more accurate comparison.
OP If you will explain more on how you would like to handle different cases I can tweak this example to suit them. Cheers.