on 2024 Jul 04 10:24 AM
Is it possible to sort a distinct statement based on another column (rather than the distinced one)?
For Example I have this table:
create or replace table mycities (cityid int, cityname varchar(40)); insert into mycities values (4, 'Vienna'), (2, 'Cairo'), (1, 'Yerevan'), (2, 'Cairo'), (3, 'Berlin');
And I want to execute something like this, but I get an error.
select distinct cityname from mycities order by cityid;
Request clarification before answering.
What order would you expect, in the general case the other column (cityid) would have different values for identical cityname values?
In your particular case, as you have simply duplicates in the table, a derived table with distinct does the trick:
select cityname from (select distinct cityid, cityname from mycities) DT order by cityid;
In the general case, you might need to apply an aggregate like MIN or MAX or the like on the "other column" and group by the "distinct column" to specify an order, such as in this sample with city names in different countries
create or replace table mycities2 (cityname varchar(40), countryname varchar(40));
insert into mycities2 values
('Vienna', 'Austria'),
('Cairo', 'Egypt'),
('Yerevan', 'Armenia'),
('Cairo', 'USA'),
('Berlin', 'Germany'),
('Berlin', 'USA'); -- (*)
select cityname
from
(select cityname, min(countryname) as min_countryname
from mycities2
group by cityname
) DT
order by min_countryname;
-- (*) could have used Canada but it's been re-named to Kitchener for obvious reasons 😉
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Both Volker and I came up with examples on why you're getting the error, but Volker went the extra step and showed how to use a derived table in the query that forced the decision on how to sort the results.
Extra points for attempting to bring in the location of the Watcom headquarters in Kitchener/Waterloo into the example, but history seems to have gotten in the way. 🙂
I did not also know that there is another Berlin in the USA 😉
Oh, there are quite a few...
In contrast, I wasn't aware of the English name of the Armenian capital, this forum is helpful for anyone. 🙂
I was surprised to see that Volker knew something of the history of our sister city Kitchener. Our family owns a Pequegnat mantel clock that was made in Berlin (Ontario, Canada) circa 1912. Since Waterloo and Kitchener are glued at the hip, there have been many calls over the years that the two cities should be united. Perhaps they'll rename the combined cities "Berlin". 🙂
If you have a distinct clause in the query, the order by clause can only include columns in the query's select list. From the documentation for the -854 error :
Probable cause You specified a function or column reference in the ORDER BY clause that is semantically invalid. For example, for DISTINCT queries the ORDER BY clause may only refer to items in the query's SELECT list.
Reg
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
How could the query below be properly sorted if this restriction did not exist?
create or replace table mycities (cityid int, cityname varchar(40)); insert into mycities values (1, 'Cairo'), (2, 'Vienna'), (3, 'Cairo'), select distinct cityname from mycities order by cityid;
User | Count |
---|---|
61 | |
8 | |
7 | |
6 | |
6 | |
4 | |
4 | |
4 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.