I have a services table with the following column-rows on a MySQL database
`id` `service_rendered` `created_at`
1 repair 2016-11-19 14:40:56
2 install 2016-11-19 14:40:58
3 repair 2016-11-19 14:44:27
4 install 2016-11-19 14:50:35
I'm trying to count the number of services and the last date/time it was rendered. Column created_at is a datetime type.
The SQL statement that I'm running is:
SELECT COUNT(`service_rendered`) as `count_service`, `service_rendered`, `created_at` as `last_rendered`
FROM services
WHERE `id` IN (1,2,3)
GROUP BY `service_rendered`
ORDER BY `created_at` DESC
But what I'm getting is:
`count_service` `service_rendered` `last_rendered`
2 repair 2016-11-19 14:40:56
1 install 2016-11-19 14:40:58
How do I write my SQL so that I am able to get 2016-11-19 14:44:27 on repair?
You can use the following solution using MAX on the created_at column:
SELECT COUNT(`service_rendered`) AS `count_service`, `service_rendered`, MAX(`created_at`) AS `last_rendered`
FROM services
WHERE `id` IN (1,2,3)
GROUP BY `service_rendered`
ORDER BY `last_rendered` DESC
demo: http://sqlfiddle.com/#!9/3906b7/2
You want to ORDER BY created_at DESC?
You have to use the alias of the column last_rendered, so replace ORDER BY created DESC with ORDER BY last_rendered DESC
You have to select the max(created_date) instead of created_date for last_rendered.
Then your query will become
select
count(service_rendered) as count_service,
service_rendered,
max(created_at) as last_rendered
from
services
group by service_rendered
order by created_at desc
Related
users table
id
name
company_id
1
user 1
1
2
user 2
2
3
user 3
1
How can I select all the company_id uniquely while ordering them by user name ?
I have tried below query in ONLY_FULL_GROUP_BY false mode
select
distinct `company_id`
from
`users`
order by
`name` DESC
But this messed up my ordering.
So based on your requirements you'd need to do something like:
select `company_id`
from
`users`
group by `company_id`
order by MAX(`name`) DESC
This should group company ids and order them based on the descending order of the last name that appears in that group.
I have the following mysql 5.5 query:
select count(original_url), original_url from page_views group by original_url order by count(original_url) desc;
but would also like to group by each day (like 2014-06-23) so that I can see which day has the most page_views per url. It's a rails app so there is a created_at. I was thinking something like:
select count(original_url), original_url, created_at from page_views group by original_url, created_at order by count(original_url) desc;
but that selects and groups by hour:min:sec and I just want it to be by day like YEAR-MONTH-DAY. How would I do that?
Try the Date function:
select count(original_url), original_url, created_at from page_views group by original_url, DATE(created_at) order by count(original_url)
I have a select statement that I would like to select 1 record from a table. The structure is:
id | start_time
--------------
1 NULL
2 2014-08-23
3 2014-09-01
I would like to select the item with a NULL start time, but if that does not exist I would like it to select the latest start_time. I have tried using ORDER with LIMIT 1, but using ORDER BY start_time either gives NULL first followed by the earliest starting, or latest starting then NULL. Is it possible to have result order 1,3,2 ?
You can use two sort expressions to get the ordering you want:
select t.*
from table t
order by (start_time is null) desc,
start_time desc
limit 1;
You can have two different ORDER BY expressions:
SELECT * from table ORDER BY (start_time IS NULL) DESC, start_time DESC;
tbl_contacts: -
user_id - int
contact_id - int
first_name - varchar
last_name - varchar
date_backup - TIMESTAMP
I am having lots of data and i want to get the latest one from the database.
Currently I am having data of 2 different dates, 1 is 2014-02-12 04:47:39 and another is 2014-01-12 04:47:39. I am having total 125 records from which 5 are of 2014-01-12 04:47:39 date and rest are of 2014-02-12 04:47:39. I am using below query to get the latest date data but its returning all the data somehow. I am trying since long and unable to successfully achieve my goal. If anyone has any idea please kindly help me.
Query
SELECT `contact_id`, `user_id`, `date_backup`, `first_name`, `last_name`
FROM tbl_contacts WHERE `date_backup` IN (
SELECT MAX(`date_backup`)
FROM tbl_contacts WHERE `user_id`= 1 GROUP BY `contact_id`
)
ORDER BY `contact_id`ASC, `date_backup` DESC
By using ORDER BY date_backup DESC, I am getting the old data at the end of list. But i just don't want the old date record at all if new date record is available.
user MySql UNIX_TIMESTAMP() with ORDER BY Clause.
SELECT `contact_id`, `user_id`, `date_backup`, `first_name`, `last_name`
FROM tbl_contacts WHERE `date_backup` IN (
SELECT MAX(`date_backup`)
FROM tbl_contacts WHERE `user_id`= 1 GROUP BY `contact_id`
)
ORDER BY UNIX_TIMESTAMP(`date_backup`) DESC, `contact_id`ASC
If require change all date_backup with UNIX_TIMESTAMP(`date_backup`)
LIMIT will do the trick in MySQL database (in other databases it would probably be TOP clause). So use LIMIT 10:
SELECT TOP 1 `contact_id`, `user_id`, `date_backup`, `first_name`, `last_name`
FROM tbl_contacts WHERE `date_backup` IN (
SELECT MAX(`date_backup`)
FROM tbl_contacts WHERE `user_id`= 1 GROUP BY `contact_id`
)
ORDER BY `contact_id`ASC, `date_backup` DESC
LIMIT 10
If you want ten of the most recent ones.
The guide for the LIMIT clause can be found at MySQL reference
Similar kind of issue
Just add LIMIT 1 at the end of your query to select only the first line of results.
I am newbie to Mysql Can any one help me for this?
I need to fetch Mysql record ORDER BY User_ID DESC and ORDER BY Created_date ASC
Is it possible?
ORDER BY accepts a comma-separated list of columns. So, simply put ORDER BY User_ID DESC, Created_date ASC
Yes it is possible you can run query like this SELECT ------ ORDER BY User_ID DESC, CREATED DATE ASC