I need to sort a table by date (descending), but all columns in the table are varchar, so I need to manipulate the data on the fly for sorting it correctly.
date sales
10/09/2014 100
13/09/2014 250
30/08/2014 200
Is that possible without altering the table? So the result will be like below, newest dates first?
date sales
13/09/2014 250
10/09/2014 100
30/08/2014 200
Like pseudocode
SELECT * FROM table ORDER BY (CONCAT(REGEXP(date, '[0-9]{4}'),
REGEXP(date, '/[0-9]{2}/'), REGEXP(date, '^[0-9]{4}/')) DESC
I think I need to use substring_index somehow, because regexp just returns 1 or 0, not the actual value found.
You need to convert your varchar-stored date objects into DATE objects, then use them to order.
This you can do on the fly like so
ORDER BY STR_TO_DATE(date,'%d/%m/%Y') DESC
But performance is going to be horrible. For best results store your dates in a DATE column in your table.
you can use STR_TO_DATE
SELECT *
FROM Table1
ORDER BY STR_TO_DATE(date, '%d/%m/%Y') desc,
sales desc
Related
I am storing several dates in a MySQL database.
1992-01-03
1990-02-30
1990-01-28
1990-01-13
1990-01-01
(Note: The order of the dates is not the same as the order in my database)
If I referenced the date 1990-01-29 for any arbitrary reason, and I needed to get the first date that was smaller than 1990-01-29, how could I create a query that would do that for me?
Search for all dates that are less than the one you want, sort the result and just grab one row. Something like:
SELECT theDate
FROM yourTable
WHERE theDate < '1990-01-29'
ORDER BY theDate DESC
LIMIT 1
Is there a way to use limit offset and get the most recent (MAX) date from that group
My table: column_id, column_data, column_date
I've tried
SELECT max(column_date) FROM table_name limit 2000 offset 22000
I'm trying to get the most recent date in the 2000 rows returned using the offset. In other words, I'm looking for the last date modified in each group of 2000.
The table structure above has 100,000 rows. each query gets 2000 rows and I would like to retrieve the most recent date from the 2000 rows (using offset).
You must extract the whole group then find MAX() over it:
SELECT MAX(date_column)
FROM ( SELECT date_column
FROM source_table
ORDER BY some_expression /* compulsory! must provide rows uniqueness! */
LIMIT #rows_in_group OFFSET #group_offset ) AS subquery
I have a MySQL DB where one column is the DATE and the other column is the SIGNAL. Now I would like to calculate the SUM over Signal for 4 days each.
f.e.
SUM(signal over DATE1,DATE2,DATE3,DATE4)
SUM(signal over DATE5,DATE6,DATE7,DATE8)
...
whereas Date_N = successor of DATE_N-1 but need not to be the day before
Moreless the algo should be variable in the days group. 4 ist just an example.
Can anyone here give me an advice how to perform this in MySQL?
I have found this here group by with count, maybe this could be helpful for my issue?
Thanks
Edit: One important note: My date ranges have gaps in it. you see this in the picture below, in the column count(DISTINCT(TradeDate)). It should be always 4 when I have no gaps. But I DO have gaps. But when I sort the date descending, I would like to group the dates together always 4 days, f.e. Group1: 2017-08-22 + 2017-08-21 + 2017-08-20 + 2017-08-19, Group2: 2017-08-18 + 2017-08-17+2017-08-15+2017-08-14, ...
maybe I could map the decending dateranges into a decending integer autoincrement number, then I would have a number without gaps. number1="2017-08-17" number2="2017-08-15" and so on ..
Edit2:
As I see the result from my table with this Query: I might I have double entries for one and the same date. How Can I distinct this date-doubles into only one reprensentative?
SELECT SUM(CondN1),count(id),count(DISTINCT(TradeDate)),min(TradeDate),max(TradeDate) ,min(TO_DAYS(DATE(TradeDate))),id FROM marketstat where Stockplace like '%' GROUP BY TO_DAYS(DATE(TradeDate)) DIV 4 order by TO_DAYS(DATE(TradeDate))
SUM() is a grouping function, so you need to GROUP BY something. That something should change only every four days. Let's start by grouping by one day:
SELECT SUM(signal)
FROM tableName
GROUP BY date
date should really be of type DATE, like you mentioned, not DATETIME or anything else. You could use DATE(date) to convert other date types to dates. Now we need to group by four dates:
SELECT SUM(signal)
FROM tableName
GROUP BY TO_DAYS(date) DIV 4
Note that this will create an arbitary group of four days, if you want control over that you can add a term like this:
SELECT SUM(signal)
FROM tableName
GROUP BY (TO_DAYS(date)+2) DIV 4
In the meantime and with help of KIKO I have found the solution:
I make a temp table with
CREATE TEMPORARY TABLE if not EXISTS tradedatemaptmp (id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY) SELECT Tradedate AS Tradedate, CondN1, CondN2 FROM marketstat WHERE marketstat.Stockplace like 'US' GROUP BY TradeDate ORDER BY TradeDate asc;
and use instead the originate tradedate the now created id in the temp table. So I could manage that - even when I have gaps in the tradedate range, the id in the tmp table has no gaps. And with this I can DIV 4 and get the always the corresponding 4 dates together.
I have a table
id |value |date
-------------------
1 |2.8 |28-3-14
2 |2.9 |28-7-14
3 |3.9 |20-1-14
in this table i need to get the value of 21-3-14.
but if value or object is not present for that then query get output of 20-1-14 directly without one by one search object by minus date by 1 day.
if any one know about this please give me suggestion.
You just need to sort by date
SELECT value FROM table WHERE date<='21-3-14' ORDER BY date DESC LIMIT 1;
Based on your table it should print:
2.8
Assuming the date 21-3-14 wasn't there, it should print:
3.9
try this,
SELECT
*
FROM
<tablename>
WHERE
STR_TO_DATE(`date`,'%d-%m-%y') <= STR_TO_DATE('YOUR_DATE','%d-%m-%y')
ORDER BY
`date` DESC < LIMIT 1 >
It is recommended to store date in date format i.e. < yyyy-mm-dd >
you may refer,
PHP mysql insert date format
I have a table with inconsistent date formats. For example:
ID date name
1 01.02.2015 exampleA
2 12.13.2015 exampleB
3 1.11.2015 exampleC
Sometimes the date format is d.m.Y and sometimes the format is m.d.Y. I have created this SQL statement, which transforms the date column of the result in an uniform format:
SELECT IFNULL(DATE_FORMAT(STR_TO_DATE(date, '%d.%m.%Y'), '%d.%m.%Y'),
DATE_FORMAT(STR_TO_DATE(date, '%m.%e.%Y'), '%e.%m.%Y')) AS date,
Name,
ID
FROM `exampleTable`
ORDER BY `date` DESC
But the result is not really ordered by date, because this column is treated like a number or something else, so that the order makes no sense.
What should i do to get the required result?
Is there a way to create a new column and write the date in an
uniform format in my table, so that i can delete the other date
column?
It seems impossible to distinguish programmaticaly the format for the date like 02.03.2015 it can be both %d.%m.%Y and %m.%e.%Y it will give results in both cases though the results will be different. For alike varcahar inputs the information has been lost