Lets say we have this table
id brand date
1 audi ....
2 toyota .....
3 toyota ....
4 audi ....
5 audi .....
when i use group by on brand(select * from table group by brand) it returns first occurances which is
id brand date
1 audi ....
2 toyota .....
Is it possible to get last occurances with GROUP BY clause. Mwith last occurance i mean latest id and date just like
id brand date
5 audi ....
3 toyota .....
select brand, max(date) from table group by brand;
Related
user_car
car_id model_year brand_id
1 2006 1
2 2010 3
3 2015 2
4 2015 1
5 2015 1
And
car_brand
brand_id name
1 BMW
2 MAZADA
3 JEEP
If I search "BMW2015" OR "2015BMW" OR "2015 BMW"
I can't get the result in above sql query, how can i search in proper way?
car_id brand_id model_year name
4 1 2015 BMW
5 1 2015 BMW
If I search "BMW2015" OR "2015BMW" means you will be getting no rows only, if you search "BMW 2015" or "2015 BMW" means you manually split with " " and try the below query.
select * from user_car u inner join car_brand c on u.brand_id=c.brand_id where model_year like "%2015%" or model_year like "%BMW%" or name like "%2015%" or or name like "%BMW%" group by car_id order by car_id
I'm trying to return a list of vehicles where I know they are not already booked for a current date range. I have a tblVehicles table:
ID Name
1 BMW
2 Merc
3 Land Rover
4 Hummer
5 Convertable
I also have a table (tblDiary) that holds all the dates that vehicle is being used:
ID VehicleId startDate endDate
1 1 2016-06-20 2016-06-22
2 1 2016-06-24 2016-06-24
3 2 2016-05-01 2016-06-05
So I'm trying to write a query where I want to find vehicles that are available between 2016-06-21 & 2016-06-23. So, in this instance the desired result would be:
ID Name
2 Merc
3 Land Rover
4 Hummer
5 Convertable
What the most efficient way of doing this?
SELECT *
FROM tblVehicles v
WHERE NOT EXISTS
(
SELECT NULL
FROM tblDiary d
WHERE d.vehicleId = v.id
AND endDate >= '2016-06-21'
AND startDate <= '2016-06-23'
)
I have a table like this
id productName amount
1 Abc 10
2 xyz 20
3 Abc 10
4 Abc 10
5 Abc 10
6 Abc 10
7 xyz 20
8 xyz 20
9 xyz 20
10 xyz 20
I want to sum Amount and Count No. of rows for each products in mysql. Output like below
productName noQty totalAmount
ABC 5 50
xyz 5 100
select product_name,sum(amount)as totalamount,count(productName)as noQty
from table_name
group by productname
by using count you can get COUNT from your table and using SUM you can get sum of all same product name.
SELECT productName,
count(productName) AS noQty,
SUM(amount) AS totalAmount
FROM tableName
GROUP BY productName
select productname,count(productname) as qty,sum(amount)
from table_name
group by productname
This is probably a easy one, but for the life of me I can't seem to figure it out.
This is my table:
uid | userID | trackID | carID | highscoreDate | highscore
-----------------------------------------------------------------
1 1 1 1 [date] 123
2 1 1 1 [date] 44
3 2 2 1 [date] 222
4 2 1 1 [date] 28
5 1 2 1 [date] 17
I would like to get the SUM of the highest highscores for each user and track. In the data above that would give:
user 1: 140
user 2: 250
How about using a subselect first.
Something like
SELECT userID, SUM(highscore)
FROM (
SELECT userID, trackID, MAX(highscore) highscore
FROM MyTable
GROUP BY userID, trackID
) s
GROUP BY userID
SQL Fiddle DEMO
I have a table with the following fields:
id
source_id
title
date
I want to select the 25 most recent items, so SELECT * FROM table ORDER BY date DESC LIMIT 50
The extra requirement is to select only the 3 most recent from every source_id.
So if the records look something like that,
id | source_id | title | date
----+-----------+-------+---------
1 2 aaa 2012-1-1
2 2 aaa 2012-1-2
3 2 aaa 2012-1-3
4 2 aaa 2012-1-4
5 3 aaa 2012-1-5
6 4 aaa 2012-1-6
I want my query to return items 4,3,2,5,6
So just the 3 most recent of every source with an over all limit of 25.
I'm not sure it's clear enough so please ask if you need more details.
Here you go:
SELECT *
FROM your_table t1
WHERE
(
SELECT COUNT(*)
FROM your_table t2
WHERE
t1.source_id = t2.source_id
AND t1.date < t2.date
) < 3
ORDER BY source_id, date DESC
Result:
4 2 aaa 2012-01-04
3 2 aaa 2012-01-03
2 2 aaa 2012-01-02
5 3 aaa 2012-01-05
6 4 aaa 2012-01-06
In plain English: take only rows that have less than 3 newer rows with the same source_id.
NOTE: This could select more than 3 rows per source_id if the third newest date (for the same source_id) happens to be shared by more than one row. Let me know what "3 newest" means in this context if that's a problem...
select * from table where source_id in
(select distinct source_id from table order by date limit 3)
LIMIT 25