My table has this field:
`processeddate` int(14) DEFAULT NULL,
a datetime converted by strtotime .
The content is like 1401847266
How do I select distinct this field which is an integer not a datetime?
I want to select per day.
Like the sum of member field ,registered, on 20140603 .
On database:
Member | Processed_Date
A | 1401847266 //real date 20140604
B | 1401846898 //real date 20140604
C | 1401844093 //real date 20140604
D | 1401788219 //real date 20140603
E | 1401788219 //real date 20140603
RESULT that I want to be displayed :
Date | Member
20140603 | 2
20140604 | 3
PS.
The processeddate is in datetime format converted into time integer, not date format.
You can use FROM_UNIXTIME() to convert the integer value to a datetime value and then use functions like YEAR() and MONTH() and DAY() on it. However when using a function on a column MySQL can't use an index anymore. Best is, to convert the other value like this:
...
WHERE your_column BETWEEN UNIX_TIMESTAMP('2014-06-04 00:00:00') AND UNIX_TIMESTAMP('2014-06-04 23:59:59');
EDIT:
What you want to do can be achieved with this query:
select
date(from_unixtime(Processed_Date)) as date, count(*) as member
from your_table
group by date
see it working live in an sqlfiddle
Related
If I consider the following:
Customer | Date
100 | 09-06-2021
100 | 17-03-2020
I am trying to find out the most recent date from the above, by using: select MAX(Date) from table, and it is returning 17-03-2020, which is wrong.
How do I get the most recent date as 09-06-2021?
What is wrong is that you are storing the date as a string, not a date.
You can fix your immediate problem by doing:
select max(str_to_date(date, '%d-%m-%Y'))
from t;
But you should really fix the data. Start by putting the date in a canonical format:
update t
set date = str_to_date(date, '%d-%m-%Y');
Then modify the column to the correct data type:
alter table t modify column date date;
I've a column sample_date in form of string as 200912301111230000000000 (UTC Time).How can I convert it from string to datetime in form of yyyymmdd using SQL select statement?
As you only want yyyymmdd, which is the first 8 chacters of your string, it is enough to simply use LEFT
I added the ST_TO_DATE so that you can see hw a conversionto a Date column could work
SELECT LEFT('200912301111230000000000',8),STR_TO_DATE(LEFT('200912301111230000000000',8),'%Y%m%d')
LEFT('200912301111230000000000',8) | STR_TO_DATE(LEFT('200912301111230000000000',8),'%Y%m%d')
:--------------------------------- | :-------------------------------------------------------
20091230 | 2009-12-30
db<>fiddle here
So it would loke like this
SELECT LEFT(Your_Column,8) FROM Your_Table
I'm trying to write a SQL command that would recreate report data from a certain date. The table has three columns (location (varchar), value (int), datetime (datetime)). Over time, rows are added to the table to provide updated values for each location.
What SQL should I be using to:
For each location, return only the row with the most recent datetime before a given datetime.
Example:
Location Value Datetime
A 5 2011-01-01
B 6 2011-01-01
A 7 2012-01-01
A 8 2012-06-01
If I'm curious about 2012-05-01, I would like to get back rows 2 and 3.
What I'm thinking is to GROUP BY location, and specify to keep the largest datetime that is less than the given datetime. I'm assuming there is some built in functionality with datetime objects in SQL that I should be using.
The straight-forward way: Get the maximum date per location, then select the complete record:
select *
from mytable
where (location, datetime) in
(
select location, max(datetime)
from mytable
where datetime < date '2012-05-01'
group by location
);
In MySQL 8.x you can use the ROW_NUMBER() window function. For example:
with x as (
select
location, value, datetime,
row_number() over (partition by location order by datetime desc) as rn
from my_table
where datetime < '2012-05-01' -- cutoff date
)
select * from x where rn = 1
I don't know what is wrong in my MYSQL query:
SELECT * FROM package_customers pc
left join installations ins on ins.package_customer_id = pc.id
WHERE pc.status = 'scheduled'
AND CAST(ins.schedule_date as DATE) >='10-27-2017'
The fields are:
status data type enum
schedule_date data type varchar
In the column schedule_date, the data is like this: 10-27-2017 12AM 12PM
I am trying to find date-wise data.
cast function can work if the source data is in acceptable format.
There are some conditions to validate date and time formats.
Your schedule_date column value does not match them. And hence, cast failed.
Please read documentation on Date and Time Types.
You should think of redesigning the table to include schedule_start and schedule_end columns with datetime data type. MySQL has various date and time functions to work with such data fields.
For time being, your varchar date data can be handled in following way.
mysql> SELECT #dt_string:='10-27-2017 12AM 12PM' AS dt_string
-> , #dtime:=STR_TO_DATE( #dt, '%m-%d-%Y %h%p %h%p' ) AS date_time
-> , DATE( #dtime ) AS my_date;
+-----------------------+---------------------+------------+
| dt_string | date_time | my_date |
+-----------------------+---------------------+------------+
| 10-27-2017 12AM 12PM | 2017-10-27 12:00:00 | 2017-10-27 |
+-----------------------+---------------------+------------+
1 row in set (0.00 sec)
There's a date (its a varchar(30)) in a format, like this
d.m.y
, where
day = a day without leading zeros
m = a month with leading zeros
y = last two numbers of a year
And a table, that looks like this
id | date | price
1 | 7.04.14 | 10
2 | 8.04.14 | 20
3 | 9.04.14 | 30
And when a query is executed,
SELECT `price` FROM `table` WHERE `date` BETWEEN '7.04.14' AND '9.04.14';
it returns nothing
The thing: I cannot change a date format, and I have to get prices between two dates. Is there an easy way of doing this?
Just parse the dates.
SELECT price
FROM `table`
WHERE STR_TO_DATE(`date`, '%d.%m.%y')
BETWEEN STR_TO_DATE(...) AND STR_TO_DATE(...)
Also, consider taking a look at the manual page for STR_TO_DATE.
But as #juergen d writes, it is far better to use date types.