I have the following query which I did not write to produce the results:
select distinct "ID","Name"
from (
select myview.ID as "ID", myview.Name as "Name",
rank() over (partition by myview.ID order by myview.OTHER_ID DESC, rownum) rnk
from my_view myview
)where rnk = 1
ORDER BY "Name" DESC;
I need to order by the name attribute which is a string, but it is not ordering even though I have the order by name statement. How can I order it by name?
So for the name
column the data it returns the data in this order:
368838, "AUSER03232, JOHN"
368532, "BUSER000417, JANE"
20252, "BUSER108276, JANE"
75235, "AUSER01809, JANE"
答案 0 :(得分:0)
The distinct
is not necessary (although it shouldn't affect the order by
). Your rank()
includes rownum
-- which should make each row unique. Hence, just use row_number()
instead and get rid of the distinct
:
select "ID", "Name"
from (select myview.ID as "ID", myview.Name as "Name",
row_number() over (partition by myview.ID order by myview.OTHER_ID DESC) as seqnum
from my_view myview
) v
where seqnum = 1
order by "Name" DESC;
This saves the overhead of the duplicate removal.
One thing that would cause your problem is if you used single quotes rather than double quotes in the order by
: order by "Name"
is quite different from order by 'Name'
.
答案 1 :(得分:0)
Your output data isn't consistent with that query.
Using your "output" data .. and feeding it back into the query, and running results in a totally different output .. so there's something you're making up - I think.
Please, please PLEASE .. post a COMPLETE WORKING EXAMPLE .. from start to finish .. include table creation, or input data like I have ... run it yourself .. and show the actual results .. copy/pasted .. do NOT massage it manually to try to make it "look more like the problem". If it doesn't re-create your "problem", then it's not a good example ;)
with w_data as (
select 368838 ID, 'AUSER03232, JOHN' Name from dual union all
select 368532 ID, 'BUSER000417, JANE' Name from dual union all
select 20252 ID, 'BUSER108276, JANE' Name from dual union all
select 75235 ID, 'AUSER01809, JANE' Name from dual
)
select distinct ID, Name
from (
select myview.ID as ID, myview.Name as Name,
rank() over (partition by myview.ID order by myview.ID DESC, rownum) rnk
from w_data myview
)
where rnk = 1
ORDER BY Name DESC;
ID NAME
---------- -----------------
20252 BUSER108276, JANE
368532 BUSER000417, JANE
368838 AUSER03232, JOHN
75235 AUSER01809, JANE
SQL>
Note I have provided a query in which ANYONE can now copy/paste and run on their server, easily, and repeatably. They can easily confirm if it generates the same results I have, or something different, and then easily tell what I did wrong (if anything) .. ;)