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 ;
Related
I am trying to pickup Account with End Date NULL first then latest date if there are more accounts with the same item
Table Sample
Result expected
Select distinct *
from Sample
where End Date is null
Need help to display the output.
Select *
from Sample
order by End_Date is not null, End_date desc
According to sample it seems to me you need union and not exists corelate subquery
select * from table_name t where t.enddate is null
union
select * from table_name t
where t.endate=( select max(enddate) from table_name t1 where t1.Item=t.Item and t1.Account=t.Account)
and not exists ( select 1 from table_name t2 where enddate is null and
t1 where t2.item=t.item
)
SELECT * FROM YourTable ORDER BY End_Date IS NOT NULL, End_Date DESC
In a Derived Table, you can determine the end_date_to_consider for every Item (using GROUP BY Item). IF() the MIN() date is NULL, then we consider NULL, else we consider the MAX() date.
Now, we can join this back to the main table on Item and the end_date to get the required rows.
Try:
SELECT t.*
FROM
Sample AS t
JOIN
(
SELECT
Item,
IF(MIN(end_date) IS NULL,
NULL,
MAX(end_date)) AS end_date_to_consider
FROM Sample
GROUP BY Item
) AS dt
ON dt.Item = t.Item AND
(dt.end_date_to_consider = t.end_date OR
(dt.end_date_to_consider IS NULL AND
t.end_date IS NULL)
)
First of all you should state clearly which result rows you want: You want one result row per Item and TOU. For each Item/TOU pair you want the row with highest date, with null having precedence (i.e. being considered the highest possible date).
Is this correct? Does that work with your real accounts? In your example it is always that all rows for one account have a higher date than all other account rows. If that is not the case with your real accounts, you need something more sophisticated than the following solution.
The highest date you can store in MySQL is 9999-12-31. Use this to treat the null dates as desired. Then it's just two steps:
Get the highest date per item and tou.
Get the row for these item, tou and date.
The query:
select * from
sample
where (item, tou, coalesce(enddate, date '9999-12-31') in
(
select item, tou, max(coalesce(enddate, date '9999-12-31'))
from sample
group by item, tou
)
order by item, tou;
(If it is possible for your enddate to have the value 9999-12-31 and you want null have precedence over this, then you must consider this in the query, i.e. you can no longer simply use this date in case of null, and the query will get more complicated.)
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);
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 "users" table with fields
user_name, user_id
I have data tables like
data_table_2012_10
data_table_2012_11
data_table_2012_12
data_table_2013_01
data_table_2013_02
each table contains the following fields
user_id, type ('ALARM', 'EMERGENCY', 'ALIVE', 'DEAD'), date_time
There will be millions of records in each table.
I have to select the count of type from the data_tables within the time frame given by the user, as well as have to get the corresponding name of the user with the help of user_id.
Can some one help me out with the best solution.
Try this query where DATE1 and DATE2 is your date range. You should union all tables in the inner query. Also you can try to make a query dynamically to include in the inner query only those tables that are in a date range you use:
select t.user_id,t.type, MAX(users.user_name), SUM(t.cnt)
from
(
select user_id,type,count(*) cnt
from data_table_2012_10 where date_time between DATE1 and DATE2
group by user_id,type
union all
select user_id,type,count(*) cnt
from data_table_2012_11 where date_time between DATE1 and DATE2
group by user_id,type
union all
.........................................
union all
select user_id,type,count(*) cnt
from data_table_2013_02 where date_time between DATE1 and DATE2
group by user_id,type
) t
left join users on (t.user_id=users.user_id)
group by t.user_id,t.type
Remember not to use UNION, but UNION ALL as UNION will return only merge similar rows into one and that may cause problem