This question already has an answer here:
MySQL order by multiple case statements
(1 answer)
Closed 10 months ago.
I have a shipment table in the following format:
Shipment
Source
Destination
shipment 1
Spain
France
shipment 2
Landon
Germany
shipment 3
Netherlands
Sweden
shipment 4
Finland
France
shipment 6
Landon
Belgium
shipment 7
Landon
France
shipment 8
Germany
France
shipment 9
Landon
France
shipment 10
Landon
France
shipment 11
Germany
France
How I can sort the above table with all the Germany to France appear first, then Landon to France followed by Landon to Germany, then the remaining shipments.
Shipment
Source
Destination
shipment 11
Germany
France
shipment 8
Germany
France
shipment 7
Landon
France
shipment 9
Landon
France
shipment 10
Landon
France
shipment 2
Landon
Germany
shipment 1
Spain
France
shipment 3
Netherlands
Sweden
shipment 4
Finland
France
shipment 6
Landon
Belgium
Thanks for the help!
Here's the idea of using a CASE statement inside the ORDER BY clause, as already suggested in the comments:
SELECT
*
FROM
Shipments
ORDER BY
CASE WHEN `Source` = 'Germany' AND `Destination` = 'France' THEN 1
WHEN `Source` = 'Landon' AND `Destination` = 'France' THEN 2
WHEN `Source` = 'Landon' AND `Destination` = 'Germany' THEN 3
ELSE 4
END
Here's a fiddle too: https://www.db-fiddle.com/f/58b3pWrUAeobtNd7yyUwwq/0.
Related
A have the following data
id user_id visited_country
1 12 Spain
2 12 France
3 14 England
4 14 France
5 16 Canada
6 14 Spain
7 16 Mexico
I want to select all users who have visited both Spain and France, excluding those who have visited England. How can I do that in MySQL?
One aggregation approach might be:
SELECT user_id
FROM yourTable
GROUP BY user_id
HAVING SUM(visited_country = 'Spain') > 0 AND
SUM(visited_country = 'France') > 0 AND
SUM(visited_country = 'England') = 0;
So, I'm managing a table where it's stored the scores of a particular competition.
The table looks like this:
ENTRY_ID TEAM_ID DATE PLACE SCORE
1 1 2021-10-12 Ireland 64
2 2 2021-10-12 Ireland 31
3 3 2021-10-12 France 137
4 2 2021-10-12 France 61
5 5 2021-10-12 France 38
6 1 2021-10-12 France 66
7 2 2021-10-12 Italy 17
8 3 2021-10-12 Italy 61
9 1 2021-10-12 Italy 74
The competition is held at three different places at the same time, with technically all teams being able to have teams in all of them.
Each team however can only win one point so, in the example, it's possible to see that Team 1 would win both in Italy and Ireland, but it should be awarded only one point for the highest score, so only Italy. The point in Ireland should go to the second place.
The query I was trying to get the results is:
SELECT `TEAM_ID`, `PLACE`
FROM `COMPETITION`
WHERE `date` = "2021-10-12"
GROUP BY `PLACE`
ORDER BY `SCORE` DESC, `id` ASC
LIMIT 3
So I could retrieve all three winners with no further processing.
The results I'm trying to achieve should repeat neither the TEAM_ID nor PLACE, in this particular example it should output:
3 FRANCE (Since it has the highest score in France at 137)
1 ITALY (For the highest score in Italy at 74)
2 IRELAND (For the second-highest score in Ireland, since Team 1 already won in Italy)
The production model of this table has far more entries so it's unlikely there would be any clashes with too many second-places.
How can I achieve that?
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
I'm creating a SSRS report and have the following data set.
ID City Country
------------------------------
1 London England
2 Glasgow Scotland
3 Aberdeen Scotland
4 Swansea Wales
5 London England
6 Glasgow Scotland
7 Glasgow Scotland
8 Manchester England
I have a COUNT on the 'City' and grouped by City and Country. This is how I would like it to appear
City Country Total
--------------------------------------------
Glasgow Scotland 3
London England 2
Swansea Wales 1
Aberdeen Scotland 1
Manchester England 1
However, this is how it's appearing
City Country Total
--------------------------------------------
Swansea Wales 1
Glasgow Scotland 3
3
3
Aberdeen Scotland 1
Manchester England 1
London England 2
2
So I need to group by the aggregate total column and also order by that column but not sure how to do it. All help appreciated, thanks.
Have tried Sanjays answer below, unless I'm doing it wrong I'm getting this error.
here
we need to create city and country as group and delete detail section from row groups as like below
so table look like after above changes
and set Count(Fields!ID.Value,"CITY") sd sorting expression for first group city1 with order Z to A
There are multiple ways you could go about this. One option would be to handle the grouping in the query and simply display the results in SSRS. So your query would look something like this:
SELECT ID, City, Country, COUNT(*) as Total
FROM MyTable
GROUP BY ID, City, Country
Now in SSRS you can sort by the Total column since it's not being aggregated in the report.
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