Sorting in access database with multiple columns - ms-access

Im trying to sort a database in access and I can't get it done. First the city with the lowest price and all the prices of that city, then the city with the second lowest price, etc.
This is the example database:
Country City Price Departure_date Return_date
Peru Lima 360$ xxxx xxxxx
Peru Lima 420$ xxxx xxxxx
Mexico CMX 300$ xxxx xxxxx
Mexico CMX 400$ xxxx xxxxx
Mexico Cancun 350$ xxxx xxxxx
Mexico Cancun 500$ xxxx xxxxx
Peru Cusco 50$ xxxx xxxxx
Peru Cusco 60$ xxxx xxxxx
It has to be sorted this way:
Country City Price Departure_date Return_date
Peru Cusco 50$ xxxx xxxxx
Peru Cusco 60$ xxxx xxxxx
Mexico CMX 300$ xxxx xxxxx
Mexico CMX 400$ xxxx xxxxx
Mexico Cancun 350$ xxxx xxxxx
Mexico Cancun 500$ xxxx xxxxx
Peru Lima 360$ xxxx xxxxx
Peru Lima 420$ xxxx xxxxx
Second Part:
I need it to only use the rows with latest date.
This is the example database:
Date Country City Price Departure_date Return_date
05-06-2019 Peru Lima 360$ xxxx xxxxx
05-06-2019 Peru Lima 420$ xxxx xxxxx
05-06-2019 Mexico CMX 300$ xxxx xxxxx
05-06-2019 Mexico CMX 400$ xxxx xxxxx
05-06-2019 Mexico Cancun 350$ xxxx xxxxx
05-06-2019 Mexico Cancun 500$ xxxx xxxxx
05-06-2019 Peru Cusco 50$ xxxx xxxxx
05-06-2019 Peru Cusco 60$ xxxx xxxxx
04-06-2017 Mexico Cancun 300$ xxxx xxxxx
04-06-2017 Peru Cusco 70$ xxxx xxxxx
04-06-2017 Peru Cusco 30$ xxxx xxxxx
It has to be sorted this way:
Date Country City Price Departure_date Return_date
05-06-2019 Peru Cusco 50$ xxxx xxxxx
05-06-2019 Peru Cusco 60$ xxxx xxxxx
05-06-2019 Mexico CMX 300$ xxxx xxxxx
05-06-2019 Mexico CMX 400$ xxxx xxxxx
05-06-2019 Mexico Cancun 350$ xxxx xxxxx
05-06-2019 Mexico Cancun 500$ xxxx xxxxx
05-06-2019 Peru Lima 360$ xxxx xxxxx
05-06-2019 Peru Lima 420$ xxxx xxxxx

Try this:
SELECT t1.*
FROM Table1 AS t1 INNER JOIN
(SELECT Table1.City, Min(Table1.Price) AS min_price
FROM Table1
GROUP BY Table1.City) AS t2
ON t1.City = t2.City
ORDER BY t2.min_price, t1.City, t1.Price
To accommodate second part, include WHERE clause:
SELECT t1.*
FROM Table1 AS t1 INNER JOIN
(SELECT Table1.City, Min(Table1.Price) AS min_price
FROM Table1
WHERE DateEnter=(SELECT Max(DateEnter) AS MaxDate FROM Table1)
GROUP BY Table1.City) AS t2
ON t1.City = t2.City
WHERE DateEnter=(SELECT Max(DateEnter) AS MaxDate FROM Table1)
ORDER BY t2.min_price, t1.City, t1.Price;

Related

How to filter by comparing the dates in MySQL Workbench

I have a table look like this:
ID B C date01 date02 date03
11 xxx xxxx 2020-05-01 2020-05-02 2020-06-02
11 xxx xxxx 2020-06-01 2020-06-03 2020-05-02
11 xxx xxxx 2020-07-01 2020-07-03 2020-06-30
11 xxx xxxx 2020-07-01 2020-06-03 2020-06-30
11 xxx xxxx 2020-01-01 2020-01-08 2020-05-02
11 xxx xxxx 2020-02-01 2020-01-31 2020-05-02
22 xxx xxxx 2020-05-01 2020-05-02 2020-06-02
22 xxx xxxx 2020-06-01 2020-06-03 2020-05-02
22 xxx xxxx 2020-07-01 2020-07-03 2020-06-30
22 xxx xxxx 2020-07-01 2020-06-03 2020-06-30
22 xxx xxxx 2020-01-01 2020-01-08 2020-05-02
22 xxx xxxx 2020-02-01 2020-01-31 2020-05-02
I want to return everything but with a latest date of those three dates for each ID, and date02 cannot be later than date03, my current output will give me this where date02 > date03:
11 xxx xxxx 2020-07-01 2020-07-03 2020-06-30
Expected output:
11 xxx xxxx 2020-07-01 2020-06-03 2020-06-30
22 xxx xxxx 2020-07-01 2020-06-03 2020-06-30
I tried this:
SELECT
id,
B,
C,
max(date01),
max(date02),
max(date03),
FROM
table
WHERE
'date02' < 'date03'
GROUP BY id
I've added WHERE 'date02' < 'date03' but why the output still have the records where date02>date03?? I'm very new to SQL, please help...
You can do this with a correlated subquery and tuple equality:
select t.*
from mytable t
where (t.date01, t.date02, t.date03) = (
select t1.date01, t1.date02, t1.date03
from mytable t1
where t1.id = t.id
order by t1.date01 desc, t1.date02 desc, t1.date03 desc
limit 1
)
For performance with this query, you can create a compound index on (id, date01, date02, date03).
You can also use row_number(), if you are running MySQL 8.0:
select *
from (
select
t.*,
row_number() over(partition by id order by date01 desc, date02 desc, date03 desc) rn
from mytable t
) t
where rn = 1
Demo on DB Fiddle:
ID | B | C | date01 | date02 | date03
-: | :-- | :--- | :--------- | :--------- | :---------
11 | xxx | xxxx | 2020-07-01 | 2020-07-03 | 2020-06-30
22 | xxx | xxxx | 2020-07-01 | 2020-07-03 | 2020-06-30

How to count number of occurrence in a filed table

if I have a table with a field City like this:
City
------
London
Los Angeles
London
Athens
Rome
Paris
Paris
How could I get this result?
City | CityNumb
-------------------
London 2
Athens 1
Los Angeles 1
Paris 2
Rome 1
select City, count(*) as CityNumb
from your_table
group by City
Above image shows the SQL query and results for above task

MySQL - assort and count items

My table has three columns 'name', 'city' and 'country'.
Now I want to make a list with only those countries, which has at least 3 times the SAME city.
name city country
---- ---- -------
Smith Boston USA
Wayne St. Louis USA
Miller Houston USA
Joseph Houston USA
Obama Washington USA
Jones Houston USA
Sarkozy Paris France
Merle Paris France
Gabin Marseille France
Delon Avignon France
Deneuve Avignon France
Trappatoni Rome Italy
Linguini Milano Italy
Mastroianni Rome Italy
Meier Hamburg Germany
Müller Munich Germany
Schmidt Hamburg Germany
Böttcher Hamburg Germany
Kunau Hannover Germany
Wilhelm Munich Germany
-------------------------------
USA
Result:
Germany
I tried it with distinct, count, group by etc. But without results.
Group by country and city then take only those having at least 3 entries each.
select distinct country
from your_table
group by country, city
having count(*) >= 3

MySQL query for grouping data

can anyone please guide me with writing MySQL query for following scenario.
The data in table is like this,
Table Name: user
user_id country city age
----------------------------------------------
1 India Mumbai 22
2 India Mumbai 22
3 India Delhi 22
4 India Delhi 23
5 India Chennai 23
6 China Beijing 20
7 China Beijing 20
8 China Shanghai 20
9 USA New York 30
10 USA New York 30
11 USA New York 30
12 USA Los Angeles 31
13 USA Los Angeles 31
14 USA Los Angeles 40
I want result to be like this which is basically sum of all users in particular country's city having same age.
country city age age_count
----------------------------------------------
India Mumbai 22 2
India Delhi 22 1
India Delhi 23 1
India Chennai 23 1
China Beijing 20 2
China Shanghai 20 1
USA New York 30 3
USA Los Angeles 31 2
USA Los Angeles 40 1
Try this :;
SELECT country,
city,
age,
count(user_id) AS age_count
FROM user
GROUP BY country,
city,
round(age)
select country, city, age, count(*) age_count
from user
group by country, city, age

Count occurrences in a table then use for finding average

table 1
categoryid categoryname categorydescription
1 a zzzzzzz
2 b yyyyyy
3 c uuuuu
table 2
carid caryear carmodel carprice catid(foreign key to category id )
1 xxxx aaaa xxxx 1
2 xxxx bbbb xxxx 3
3 xxxx cccc xxxx 4
3 xxxx dddd xxxx 3
4 xxxxx eeee xxxx 1
results
categoryname averageprice total cars
a sum price of same category car / no of same category cars 1
b sum price of same category car / no of same category cars 2
c sum price of same category car / no of same category cars 2
You can write:
SELECT category.categoryname,
AVG(car.carprice) AS "averageprice",
COUNT(car.carid) AS "total cars"
FROM category
LEFT
OUTER
JOIN car
ON car.catid = category.categoryid
GROUP
BY category.categoryname
;
Notes:
You didn't mention the names of your tables, so I had to guess.
This will include categories that don't have any cars. If you'd only like to include categories that have at least one car, drop the LEFT OUTER part.