<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>Question Re: Ordering distinct statement in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/ordering-distinct-statement/qaa-p/13838863#M4869706</link>
    <description>&lt;P&gt;the first approach is great!&lt;/P&gt;
&lt;P&gt;I did not expect that this line would generally work (without the need of group by):
&lt;/P&gt;&lt;PRE&gt;select distinct cityid, cityname from mycities
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;
&lt;P&gt;I did not also know that there is another Berlin in the USA &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 04 Jul 2024 10:47:16 GMT</pubDate>
    <dc:creator>Baron</dc:creator>
    <dc:date>2024-07-04T10:47:16Z</dc:date>
    <item>
      <title>Ordering distinct statement</title>
      <link>https://community.sap.com/t5/technology-q-a/ordering-distinct-statement/qaq-p/13838859</link>
      <description>&lt;P&gt;Is it possible to sort a distinct statement based on another column (rather than the distinced one)?&lt;/P&gt;
&lt;P&gt;For Example I have this table:
&lt;/P&gt;&lt;PRE&gt;create or replace table mycities (cityid int, cityname varchar(40));
insert into mycities values
(4, 'Vienna'),
(2, 'Cairo'),
(1, 'Yerevan'),
(2, 'Cairo'),
(3, 'Berlin');
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;
&lt;P&gt;And I want to execute something like this, but I get an error.
&lt;/P&gt;&lt;PRE&gt;select distinct cityname from mycities order by cityid;
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 04 Jul 2024 09:24:17 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/ordering-distinct-statement/qaq-p/13838859</guid>
      <dc:creator>Baron</dc:creator>
      <dc:date>2024-07-04T09:24:17Z</dc:date>
    </item>
    <item>
      <title>Re: Ordering distinct statement</title>
      <link>https://community.sap.com/t5/technology-q-a/ordering-distinct-statement/qaa-p/13838860#M4869703</link>
      <description>&lt;P&gt;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 &lt;A href="https://help.sap.com/docs/SAP_SQL_Anywhere/8534f27c56354283bd00fc8f77adaf94/80e997d26ce21014be5aa92d31e38b49.html"&gt;documentation for the -854 error&lt;/A&gt; : &lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;Reg&lt;/P&gt;</description>
      <pubDate>Thu, 04 Jul 2024 09:34:17 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/ordering-distinct-statement/qaa-p/13838860#M4869703</guid>
      <dc:creator>regdomaratzki</dc:creator>
      <dc:date>2024-07-04T09:34:17Z</dc:date>
    </item>
    <item>
      <title>Re: Ordering distinct statement</title>
      <link>https://community.sap.com/t5/technology-q-a/ordering-distinct-statement/qaa-p/13838862#M4869705</link>
      <description>&lt;P&gt;How could the query below be properly sorted if this restriction did not exist?&lt;/P&gt;
&lt;PRE&gt;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;
&lt;/PRE&gt;</description>
      <pubDate>Thu, 04 Jul 2024 09:46:14 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/ordering-distinct-statement/qaa-p/13838862#M4869705</guid>
      <dc:creator>regdomaratzki</dc:creator>
      <dc:date>2024-07-04T09:46:14Z</dc:date>
    </item>
    <item>
      <title>Re: Ordering distinct statement</title>
      <link>https://community.sap.com/t5/technology-q-a/ordering-distinct-statement/qaa-p/13838861#M4869704</link>
      <description>&lt;P&gt;What order would you expect, in the general case the other column (cityid) would have different values for identical cityname values?&lt;/P&gt;
&lt;P&gt;In your particular case, as you have simply duplicates in the table, a derived table with distinct does the trick:&lt;/P&gt;
&lt;PRE&gt;select cityname
from
   (select distinct cityid, cityname from mycities) DT
order by cityid;
&lt;/PRE&gt;

&lt;P&gt;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&lt;/P&gt;
&lt;PRE&gt;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 &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 04 Jul 2024 10:22:25 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/ordering-distinct-statement/qaa-p/13838861#M4869704</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2024-07-04T10:22:25Z</dc:date>
    </item>
    <item>
      <title>Re: Ordering distinct statement</title>
      <link>https://community.sap.com/t5/technology-q-a/ordering-distinct-statement/qaa-p/13838863#M4869706</link>
      <description>&lt;P&gt;the first approach is great!&lt;/P&gt;
&lt;P&gt;I did not expect that this line would generally work (without the need of group by):
&lt;/P&gt;&lt;PRE&gt;select distinct cityid, cityname from mycities
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;
&lt;P&gt;I did not also know that there is another Berlin in the USA &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 04 Jul 2024 10:47:16 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/ordering-distinct-statement/qaa-p/13838863#M4869706</guid>
      <dc:creator>Baron</dc:creator>
      <dc:date>2024-07-04T10:47:16Z</dc:date>
    </item>
    <item>
      <title>Re: Ordering distinct statement</title>
      <link>https://community.sap.com/t5/technology-q-a/ordering-distinct-statement/qaa-p/13838864#M4869707</link>
      <description>&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;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. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 04 Jul 2024 11:01:40 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/ordering-distinct-statement/qaa-p/13838864#M4869707</guid>
      <dc:creator>regdomaratzki</dc:creator>
      <dc:date>2024-07-04T11:01:40Z</dc:date>
    </item>
    <item>
      <title>Re: Ordering distinct statement</title>
      <link>https://community.sap.com/t5/technology-q-a/ordering-distinct-statement/qaa-p/13838865#M4869708</link>
      <description>&lt;P&gt;Using city/country names is always dangerous and it leads to another discussions &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;I dont know why I used City names in my example, but I liked the first solution of Volker because it is simple and applies to my case.&lt;/P&gt;</description>
      <pubDate>Thu, 04 Jul 2024 11:24:41 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/ordering-distinct-statement/qaa-p/13838865#M4869708</guid>
      <dc:creator>Baron</dc:creator>
      <dc:date>2024-07-04T11:24:41Z</dc:date>
    </item>
    <item>
      <title>Re: Ordering distinct statement</title>
      <link>https://community.sap.com/t5/technology-q-a/ordering-distinct-statement/qaa-p/13838866#M4869709</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;I did not also know that there is another Berlin in the USA &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;  &lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Oh, &lt;A href="https://en.wikipedia.org/wiki/Berlin_(disambiguation)"&gt;there are quite a few&lt;/A&gt;...&lt;/P&gt;
&lt;P&gt;In contrast, I wasn't aware of the English name of the Armenian capital, this forum is helpful for anyone. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 04 Jul 2024 11:26:28 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/ordering-distinct-statement/qaa-p/13838866#M4869709</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2024-07-04T11:26:28Z</dc:date>
    </item>
    <item>
      <title>Re: Ordering distinct statement</title>
      <link>https://community.sap.com/t5/technology-q-a/ordering-distinct-statement/qaa-p/13838867#M4869710</link>
      <description>&lt;P&gt;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". &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Jul 2024 10:26:24 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/ordering-distinct-statement/qaa-p/13838867#M4869710</guid>
      <dc:creator>jack_schueler</dc:creator>
      <dc:date>2024-07-05T10:26:24Z</dc:date>
    </item>
  </channel>
</rss>

