Discussions
Categories
- 385.5K All Categories
- 5.1K Data
- 2.5K Big Data Appliance
- 2.5K Data Science
- 453.4K Databases
- 223.2K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 47 Multilingual Engine
- 606 MySQL Community Space
- 486 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.2K ORDS, SODA & JSON in the Database
- 584 SQLcl
- 4K SQL Developer Data Modeler
- 188K SQL & PL/SQL
- 21.5K SQL Developer
- 45 Data Integration
- 45 GoldenGate
- 298.4K Development
- 4 Application Development
- 20 Developer Projects
- 166 Programming Languages
- 295K Development Tools
- 150 DevOps
- 3.1K QA/Testing
- 646.7K Java
- 37 Java Learning Subscription
- 37.1K Database Connectivity
- 201 Java Community Process
- 108 Java 25
- 22.2K Java APIs
- 138.3K Java Development Tools
- 165.4K Java EE (Java Enterprise Edition)
- 22 Java Essentials
- 176 Java 8 Questions
- 86K Java Programming
- 82 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 208 Java User Groups
- 25 JavaScript - Nashorn
- Programs
- 666 LiveLabs
- 41 Workshops
- 10.3K Software
- 6.7K Berkeley DB Family
- 3.6K JHeadstart
- 6K Other Languages
- 2.3K Chinese
- 207 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 474 Portuguese
Sorting the data set

Hi Experts,
I have the below data set for sales order and sequence in a table. This table is at vendor location and I access the data via dblink. I can only select these two columns. Now i need to select in such a way that i select distinct so numbers but it should be in the sorted as per below table ,i.e. 7037561280,7037567671,7037633589,7037630443,7037627397,7037601762 ....7037619156. If i do a select distinct or group by so number , i get distinct values but they are not in the same order as below . I tried using RANK and DENSE_RANK function as below to check if i can use these to get the desired output. But it does not work because when i do a partition by so-number , the rank again begins with 1.
Hence can you help me/guide me to get the sorted so_number from below data set. I used below script
SELECT so_number
,seq_no,
DENSE_RANK() OVER (PARTITION BY so_number ORDER BY seq_no) AS myrank
FROM
xx_om_sellout_data
order by seq_no
so_number | seq_no |
---|---|
7037561280 | 2 |
7037561280 | 3 |
7037567671 | 4 |
7037567671 | 5 |
7037567671 | 6 |
7037567671 | 7 |
7037633589 | 8 |
7037633589 | 9 |
7037630443 | 10 |
7037630443 | 11 |
7037627397 | 12 |
7037627397 | 13 |
7037601762 | 14 |
7037601762 | 15 |
7037596364 | 16 |
7037596364 | 17 |
7037619156 | 18 |
7037619156 | 19 |
Regards,
Best Answer
-
SELECT SO_NUMBER
FROM XX_OM_SELLOUT_DATA
GROUP BY SO_NUMBER
ORDER BY MIN(SEQ_NO)
/
SY.
Answers
-
So, do you want to have unique so_number values and no duplicate so_number? And if you want unique so_number, would you desire the max(seq_no) or min(seq_no)?
You may use group by:
select
so_number
,max(seq_no)
,min(seq_no)
from
FROM xx_om_sellout_data
group by so_number
order by so_number
;
Or what?
-
SELECT SO_NUMBER
FROM XX_OM_SELLOUT_DATA
GROUP BY SO_NUMBER
ORDER BY MIN(SEQ_NO)
/
SY.
-
Hi Solomon,
Thanks for the response this works. However if possible request you kindly explain or share any link of how does order by min (column) / max(column) sorts the data set. I am asking this because my understanding was to use order by clause the column should be present in the selected list of columns.
Regards,
-
GROUP BY returns one row per SO_NUMBER. MIN(SEQ) provides smallest SEQ_NO for each SO_NUMBER. ORDER BY sorts SO_NUMBERS according to their smallest SEQ_NO.
SY.
-
thank you so much for your time and help !
-
You can order by columns not on the SELECT, just do the following:
WITH vData AS
( SELECT 1 AS Id, 'John' AS Name, 10 AS Priority FROM dual
UNION ALL
SELECT 2 AS Id, 'Peter' AS Name, 5 AS Priority FROM dual
UNION ALL
SELECT 3 AS Id, 'Jane' AS Name, 50 AS Priority FROM dual
UNION ALL
SELECT 4 AS Id, 'Mary' AS Name, 2 AS Priority FROM dual
UNION ALL
SELECT 5 AS Id, 'Adam' AS Name, 20 AS Priority FROM dual
)
SELECT Id, Name
FROM vData
ORDER BY Priority;
Results:
The problem is that, although there is an ORDER in the data, a user cannot see it. This kind of behavior is useful when a process needs to access data in a particular order but does not uses that piece of data, but for humans it is always a good option to sort by something that you can actually SEE.
-
L. Fernigrini wrote:The problem is that, although there is an ORDER in the data, a user cannot see it. This kind of behavior is useful when a process needs to access data in a particular order but does not uses that piece of data, but for humans it is always a good option to sort by something that you can actually SEE.
OP needs DISTINCT values sorted, therefore ORDER BY can't contain other columns like PRIORITY in your example.
SY.
-
Yes, I was answering to this comment in OP response to your answer:
I am asking this because my understanding was to use order by clause the column should be present in the selected list of columns.
I was not providing a complete solution to his problem (you had already done and OP had already selected it as correct). I was just letting OP know that you can order by something not on the SELECT clause.,