I have a table "T" that contains Date as one of the column.
In the table, there are multiple rows associated with single date entry.
I want an query that will give me all the rows associated with the latest date available in the table.
select * from table_name where date = (select max(date) from table_name);
If the column type is datetime then use this query
SELECT * FROM T
WHERE CAST(<DateColumn> AS DATE) = (SELECT MAX(cast(<DateColumn> AS DATE)) FROM T)
and if column type is just date then use this query
SELECT * FROM T
WHERE <DateColumn> = (SELECT MAX(<DateColumn>) FROM T)
Group function Max when used on date it results in latest date.
SELECT <<Columns from table>> T where date = (select max(date) from T);
Related
I have been attempting to update a column based on a group by a select from one table into another table. The below subquery on the set statement works but only for one date because if I use a date range I get an error of "subquery returns more than 1 row".
I instead want to run that on a date range fetching the group by for each day (from "Monthly" table) inserting each matching row by day into "Dayfile" table. The dayfile table has a row for each date with the "LogDate column" as date and the monthly table is a log file of minute-by minute values where the "LogDateTime" data type is datetime.
UPDATE
Dayfile
SET
MaxFeelsLike =
(SELECT MAX(FeelsLike)
FROM Monthly, Dayfile
WHERE DATE(LogDateTime) = "2018-10-04"
AND DATE(LogDateTime) = DATE(LogDate)
GROUP BY DATE(LogDateTime)
);
You should use a JOIN rather using the subquery as a value.
UPDATE Dayfile AS d
JOIN (
SELECT DATE(LogDateTime) AS date, MAX(FeelsLike) AS feels
FROM Monthly
GROUP BY date
) AS m ON DATE(d.LogDate) = m.date
SET d.MaxFeelsLike = m.feels
Include LIMIT 1 at the end of your subquery
In my query below I got the correct return on MAX(Date) but It has incorrect return on BidModifier column. Is there a line that I need to add so that I can get the correct data corresponds to my MAX(Date)?
Here is my query:
SELECT AdGRoupId, Criteria, MAX(DATE) LatestDate , CpcBid, CpcBidSource,
BidModifier
FROM aw_placementbid
WHERE DATE
IN (
SELECT DATE
FROM aw_placementbid
GROUP BY AdGroupId, Criteria
)
GROUP BY AdGroupId, Criteria
ORDER BY BidModifier DESC
Try this query:
SELECT AdGRoupId, Criteria, DATE, CpcBid, CpcBidSource, BidModifier
FROM
(
SELECT AdGRoupId, Criteria, DATE, CpcBid, CpcBidSource, BidModifier
FROM aw_placementbid
ORDER BY DATE DESC
) AS t
GROUP BY AdGroupId, Criteria
ORDER BY BidModifier DESC
Firstly, you select all the data you need and order by DATE, thus records with a big DATE will be ranked in the top.
Then, you divide the temporary table above into several groups(via GROUP BY AdGroupId, Criteria).
At last, you just fetch the first row of each group(it must be the row with a MAX(DATE) as you have sorted all records before).
I want to pull specific rows from a table where the date matches a certain date. First I'm converting the date string to date format, here's the query:
SELECT id, str_to_date(candidate.AddDate,"%d/%m/%Y") n FROM candidate WHERE n='2016-01-01';
But I get the error "Unknown column 'n' in WHERE clause"
How do I make the query use the result of str_to_date in the where clause?
You cant use the alias on the same level, because isnt created at that time
SELECT id,
Str_to_date(candidate.adddate, "%d/%m/%y") n
FROM candidate
WHERE Str_to_date(candidate.adddate, "%d/%m/%y") = '2016-01-01';
Or create a subquery
SELECT *
FROM (
SELECT id,
Str_to_date(candidate.adddate, "%d/%m/%y") n
FROM candidate
) T
WHERE n = '2016-01-01';
I dont know if this is what you are trying to achieve.
SELECT id, adddate from candidate C where C.adddate = "2016-01-01"
Why cant you pull all the table rows where the given date is 2016-01-01. Is this what you want? Or something else. If you have stored the date as date field you dont really need to do str_to_time.
If it is stored as string then
SELECT * FROM ( SELECT id, DATE_FORMAT(STR_TO_DATE(candidate.adddate, '%d/%m/%Y') x FROM candidate
) C WHERE x = '2016-01-01';
I want to select data from a table, group by this data with the date value maximum.
In my table i have 4 columns - id, message_id, client_id and date. column id is unique and auto incremented, while message_id and client_id have duplicate values. date is almost unique.
I want to select all records, group by message_id and client_id, that has date maximum.
my query is -
SELECT *,MAX(`date`) AS `maxdate` FROM `table_name` group by `message_id`,`client_id` order by `date` desc
but this does not give the date with maximum value.
I am getting the grouped record with first date.
Please help, and tell me the correct query, i am quite new to mysql.
Try this query out - this is how I would do it in Oracle:
select n1.id, n1.message_id, n1.client_id, n1.date
from shailjas_note n1
where n1.date = (select max(n2.date)
from shailjas_note n2
where n1.message_id = n2.message_id
and n1.client_id = n2.client_id)
I have an table called TableA which has a column called date_entered which is of datetime type. I need to count the number of TableA rows that have been added on a given date.
To accomplish this I must CAST the datetime to a date and COUNT the number of rows that match the given date but i'm unsure how to write this query.
Any help would be much appreciated.
Thanks in advance
Use
select count(*)
from tableA
where date(date_entered) = '2012-11-15'
to count all rows for that date even if the records contain NULL values.
Or use count(specific_column) to count rows that don't contain NULL values for that column.
You can do this:
SELECT COUNT(*), DATE(date_entered) AS date_entered
FROM TableA
GROUP BY date_entered
And change it to:
SELECT COUNT(*), DATE(date_entered) AS date_entered
FROM TableA
WHERE date(date_entered) = <whatever you want>
GROUP BY date_entered
For a specific day.
try something like
SELECT count(*) FROM tableName GROUP BY datefield
OR
SELECT COUNT(*) AS same_date
FROM TableName
GROUP BY datefield
HAVING count(*) >1 ;