Question

Weird sorting problem. Good question.

Asked by: mathieu_cupryk

I have the following query that is not sort properly.

No.1 Canada Western Amber Durum Ac                                              
should come after

No.1 Canada Western Amber Durum Ac Strongfield      
Please see attached file.                          

Why does the following oracle statement does not order it properly.

PROCEDURE get_Grades          (cur_out                IN OUT      t_cursor,
                               p_price_list_hdr_id    IN          price_list_dtl.price_list_hdr_id%TYPE,
                               p_status               OUT NOCOPY  varchar2)

-- for the Grade Codes spreadsheet

IS

BEGIN
  p_status := 'Success';
  OPEN cur_out
    FOR
       -- ALL 4 GRAINS
       SELECT C.grade_pool_code, C.grain_class_name, C.grade_protein_pct, B.strght_price_amt - A.old_price_amt delta_price,
              D.strght_code, B.strght_price_amt, D.tough_code, D.damp_code, D.stone_code, D.tough_stone_code, D.damp_stone_code, G.sct_effctv_dtm,
              G.pool_part_code
              FROM   (select E.grade_code_dtl_id, max(e.strght_price_amt) old_price_amt
                             FROM     price_list_dtl E, price_list_hdr F
                             WHERE    F.price_list_hdr_id = F.price_list_hdr_id
                           --  AND      F.price_list_status_type_name = 'Approved'
                           --  AND      F.load_type_name in ('Initial','Adjustment')
                           --  and      e.price_list_hdr_id < p_price_list_hdr_id
                             and      e.price_list_hdr_id = p_price_list_hdr_id
                             GROUP BY e.grade_code_dtl_id) A, price_list_dtl B, grade_code_dtl C, price_list_hdr G,
              (Select grade_code_dtl_id,
                          max(CASE code_type_name when 'STRAIGHT' then disc_code else null end) as  Strght_Code,
                          max(CASE code_type_name when 'TOUGH' then disc_code else null end) as  Tough_Code,
                          max(CASE code_type_name when 'DAMP' then disc_code else null end) as  Damp_Code,
                          max(CASE code_type_name when 'STONE' then disc_code else null end) as  Stone_Code,
                          max(CASE code_type_name when 'TOUGH STONE' then disc_code else null end) as  Tough_Stone_Code,
                          max(CASE code_type_name when 'DAMP STONE' then disc_code else null end) as  Damp_Stone_Code
                          from grade_item
                          group by grade_code_dtl_id) D
              WHERE  B.price_list_hdr_id = p_price_list_hdr_id
              AND    C.grade_code_dtl_id = B.grade_code_dtl_id
              AND    A.grade_code_dtl_id = C.grade_code_dtl_id
              AND    D.grade_code_dtl_id = C.grade_code_dtl_id
              AND    G.price_list_hdr_id = b.price_list_hdr_id
      ORDER BY grade_pool_code,grade_name;
 

  EXCEPTION
    WHEN OTHERS THEN
         p_status := 'Failure: ' || SQLERRM;
END get_Grades;

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2008-08-25 at 11:39:38ID23676178
Topics

PL / SQL

,

Oracle Database

,

Oracle 10.x

Participating Experts
3
Points
500
Comments
38

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. FLOPPY DISC
    I BACK UP SOME FILES TO FLOPPY. I RECENTLY TRIED TO BACKUP TO A DISC AND MY COMPUTER SAYS THAT MY DISC IS UNFORMATTED. THE DISC IS FORMATTED. I AM UNABLE TO ACCESS THE INFO ON THE DISC. I TRIED OTHER DISCS AND THEY ALL SAY THAT THE DISC IS UNFORMATTED.IS THIS A PROBLEM THAT C...
  2. sorting spreadsheet
    is there a way to sort spreadsheets in a file? and is there any good web site which i can learn programming in excel?
  3. Western Digital HD causing port errors?
    Hi, a friend of mine asked me to look at an old HDD which has some low-level problems. I have some apps from the Western Digital website with which I could probably fix it. However, when I connect it and start up I get the error: Conflict I/O ports: 2F8 Even though I've done ...
  4. invalid system disc
    Hi guys, can anyone help me with this error I get at boot up. Searching for boot from IDE 0 Invalid system disc Please insert disc and press any key. Everything has been alright before this and now I cannot boot into windows. I hope this is the right place to ask this questio...
  5. Efficient sorting of a file on disc
    Hi guys, I've got a program in which details of albums and singles in the music charts are tracked, during the week new releases are added to the end of the file and then the file on disc is sorted into descending order of sales. The problem I have is that the sorting takes...
  6. maximum size of VARCHAR2()
    What is the maximum size of VARCHAR2(), Is it 3000? If yes, If I create table with VARCHAR2(3000) is it wasting the memory or it will cover only what space I used? For example, Create table x (name VARCHAR2(3000)); and Insert into x (name) values (‘S...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: sdstuberPosted on 2008-08-25 at 11:49:04ID: 22308014

why would

No.1 Canada Western Amber Durum Ac

come after

No.1 Canada Western Amber Durum Ac Strongfield


alphabetically it should come first,  however,  I'm not sure based on your spreadsheet columns and your query columns  what your order by is supposed to be saying

 

by: mathieu_cuprykPosted on 2008-08-25 at 11:54:59ID: 22308059

If I sort it in asc in excel it is ok.
What is wrong. Pool Code is 02 a
and then the GRADE is the grade name.
No.1 Canada Western Amber Durum Ac

 

by: mathieu_cuprykPosted on 2008-08-25 at 12:06:20ID: 22308157

it is ORDER BY grade_pool_code,grade_name;

 

by: mathieu_cuprykPosted on 2008-08-25 at 12:19:50ID: 22308270

This is done sorted with respect to column E asc and then column A asc

 

by: sdstuberPosted on 2008-08-25 at 12:24:03ID: 22308304

but that doesn't put

No.1 Canada Western Amber Durum Ac
after
No.1 Canada Western Amber Durum Ac Strongfield

that sorts as I would expect.

What happens when you just run the query in sqlplus?  What is the ordering then?
maybe it's something in the loading process into the xls.

 

by: mmoorePosted on 2008-08-25 at 12:34:57ID: 22308405

Nulls will sort last, if you want the nulls to sort first, substitute a low character value in place of the null. the two examples below will demonstrate.

SELECT   grade_pool_code, grade_name
 FROM   (SELECT   'No.1 Canada Western Amber Durum' grade_pool_code
                , 'Strongfield' grade_name
           FROM   DUAL
         UNION ALL
         SELECT   'No.1 Canada Western Amber Durum' grade_pool_code, '' grade_name
           FROM   DUAL)
order by grade_pool_code, grade_name;
           
SELECT   grade_pool_code, grade_name
 FROM   (SELECT   'No.1 Canada Western Amber Durum' grade_pool_code
                , 'Strongfield' grade_name
           FROM   DUAL
         UNION ALL
         SELECT   'No.1 Canada Western Amber Durum' grade_pool_code, '' grade_name
           FROM   DUAL)
order by grade_pool_code, nvl(grade_name,chr(30));

SELECT   grade_pool_code, grade_name
  FROM   (SELECT   'No.1 Canada Western Amber Durum' grade_pool_code
                 , 'Strongfield' grade_name
            FROM   DUAL
          UNION ALL
          SELECT   'No.1 Canada Western Amber Durum' grade_pool_code, '' grade_name
            FROM   DUAL)
 order by grade_pool_code, grade_name;
            
SELECT   grade_pool_code, grade_name
  FROM   (SELECT   'No.1 Canada Western Amber Durum' grade_pool_code
                 , 'Strongfield' grade_name
            FROM   DUAL
          UNION ALL
          SELECT   'No.1 Canada Western Amber Durum' grade_pool_code, '' grade_name
            FROM   DUAL)
 order by grade_pool_code, nvl(grade_name,chr(30));

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:

Select allOpen in new window

 

by: sdstuberPosted on 2008-08-25 at 12:36:02ID: 22308418

there aren't any nulls involved in this question

 

by: mathieu_cuprykPosted on 2008-08-25 at 12:42:51ID: 22308497

I am lost so what should i do?

 

by: mathieu_cuprykPosted on 2008-08-25 at 12:54:19ID: 22308619

Look at the spreadsheet.

 

by: mmoorePosted on 2008-08-25 at 13:03:00ID: 22308709

sdstuber - you are right, thanks for the correction, sorry for the noise.


 

by: sdstuberPosted on 2008-08-25 at 13:14:39ID: 22308836

that last spreadsheet looks correct  what would you want it to be?

 

by: mathieu_cuprykPosted on 2008-08-25 at 13:22:55ID: 22308911

I sorted that one in excel. Look at this.

 

by: sdstuberPosted on 2008-08-25 at 13:23:57ID: 22308916

if you're looking for a sorting rule that will identify an arbitrary string and put it in a special place different from the rest of the sorting rules,  you'll need to provide some way to identify that arbitrary string.

so, if you want 'No.1 Canada Western Amber Durum Ac' to have a special location independent of the alphabetical sorting of the rest of column then you'll have to search for that one string and give it a rule.  Something like this...

  SELECT   c.grade_pool_code,
           c.grain_class_name,
           c.grade_protein_pct,
           b.strght_price_amt - a.old_price_amt delta_price,
           d.strght_code,
           b.strght_price_amt,
           d.tough_code,
           d.damp_code,
           d.stone_code,
           d.tough_stone_code,
           d.damp_stone_code,
           g.sct_effctv_dtm,
           g.pool_part_code
    FROM   (  SELECT   e.grade_code_dtl_id, MAX(e.strght_price_amt) old_price_amt
                FROM   price_list_dtl e, price_list_hdr f
               WHERE   f.price_list_hdr_id = f.price_list_hdr_id --  AND      F.price_list_status_type_name = 'Approved'
--  AND      F.load_type_name in ('Initial','Adjustment')
--  and      e.price_list_hdr_id < p_price_list_hdr_id
                       AND e.price_list_hdr_id = p_price_list_hdr_id
            GROUP BY   e.grade_code_dtl_id) a,
           price_list_dtl b,
           grade_code_dtl c,
           price_list_hdr g,
           (  SELECT   grade_code_dtl_id,
                       MAX(CASE code_type_name WHEN 'STRAIGHT' THEN disc_code ELSE NULL END)
                           AS strght_code,
                       MAX(CASE code_type_name WHEN 'TOUGH' THEN disc_code ELSE NULL END) AS tough_code,
                       MAX(CASE code_type_name WHEN 'DAMP' THEN disc_code ELSE NULL END) AS damp_code,
                       MAX(CASE code_type_name WHEN 'STONE' THEN disc_code ELSE NULL END) AS stone_code,
                       MAX(CASE code_type_name WHEN 'TOUGH STONE' THEN disc_code ELSE NULL END)
                           AS tough_stone_code,
                       MAX(CASE code_type_name WHEN 'DAMP STONE' THEN disc_code ELSE NULL END)
                           AS damp_stone_code
                FROM   grade_item
            GROUP BY   grade_code_dtl_id) d
   WHERE       b.price_list_hdr_id = p_price_list_hdr_id
           AND c.grade_code_dtl_id = b.grade_code_dtl_id
           AND a.grade_code_dtl_id = c.grade_code_dtl_id
           AND d.grade_code_dtl_id = c.grade_code_dtl_id
           AND g.price_list_hdr_id = b.price_list_hdr_id
ORDER BY   grade_pool_code,
           CASE
               WHEN grade_name LIKE 'No.1 Canada Western Amber Durum Ac%'
               THEN
                   'No.1 Canada Western Amber Durum Ac'
               ELSE
                   grade_name
           END,
           CASE
               WHEN grade_name = 'No.1 Canada Western Amber Durum Ac' THEN 1
               WHEN grade_name LIKE 'No.1 Canada Western Amber Durum Ac%' THEN 0
               ELSE NULL
           END

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:

Select allOpen in new window

 

by: sdstuberPosted on 2008-08-25 at 13:25:41ID: 22308937

ok,  how is that data getting into the spreadsheet?

what happens when you run the query in sqlplus like I asked before?

 

by: mathieu_cuprykPosted on 2008-08-25 at 16:13:24ID: 22310193

let me run it tomorrow.

 

by: sdstuberPosted on 2008-08-25 at 17:43:17ID: 22310646

I wrote the last query based on what you had originally posted, not on what you have in the spreadsheets.

So, run your query in sqlplus, not mine.

 

by: mathieu_cuprykPosted on 2008-08-26 at 06:20:53ID: 22314273

I did I still get the same output.

 

by: sdstuberPosted on 2008-08-26 at 06:35:39ID: 22314434

can you send your sqlplus output?

spool the query and output to a file and post that

 

by: mathieu_cuprykPosted on 2008-08-26 at 07:18:43ID: 22314962

Here is attached dataset.

 

by: sdstuberPosted on 2008-08-26 at 07:21:07ID: 22314993

can you send your sqlplus output?

spool the query and output to a file and post that

 

by: sdstuberPosted on 2008-08-26 at 07:24:02ID: 22315029

sqlplus youruser/yourpassword@yourdba

SQL> spool ee_test.txt
SQL> SELECT   c.grade_pool_code,   .....   {{{ run your sql here }}}

{{{{output will apear here }}}}

SQL> exit

then post ee_test.txt


         

 

by: mathieu_cuprykPosted on 2008-08-26 at 08:07:04ID: 22315595

I don't have sqlplus on my machine.
;-(

 

by: mathieu_cuprykPosted on 2008-08-26 at 08:25:20ID: 22315822

It could be an error on the order by
it maybe grain_class_name

 

by: sdstuberPosted on 2008-08-26 at 08:27:32ID: 22315851

that's what I was trying to determine earlier and with spooling.

I wanted to see if the columns you were returning in your query were the same as what was in the report and if those were what were being used to sort by also

 

by: slightwvPosted on 2008-08-26 at 10:54:37ID: 22317416

mathieu_cupryk,

Trying to get the experts here to understand the full scope of your setup can be problematic at times.  I think this is one of those times.

I find that creating as simplified test case as possible works best.  I suggest you post a small test case that includes: table definition, sample data and expected results.

For example:

Given the following script I need code 2 to come after code 1 because <insert reason here>.

create table test_table (grade_pool_code number,grade_name varchar2(50));
 
insert into test_table values(1,'some value');
insert into test_table values(2,'some other value');
commit;
 
expected results:
-----------------
some value
some other value
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:

Select allOpen in new window

 

by: sdstuberPosted on 2008-08-26 at 11:05:28ID: 22317535

the problem here is capturing the difference between what the query is returning and what shows up on the spreadsheet.  The columns have different names so, it appears the sorting is happening by something other than what appears in column "A" of the spreadsheet.

I think from the last post, the jpeg,  assuming that is a direct dump of what I hoped would come from sqlplus,  then yes, it looks like the sorting column is grain_class_name
not grade_name

 

by: mathieu_cuprykPosted on 2008-08-26 at 11:30:58ID: 22317804

it was the wrong field thanks. My head is going to explode.

 

by: sdstuberPosted on 2008-08-26 at 11:34:19ID: 22317842

glad I could help you walk through it!  

I suggest getting sqlplus installed on your machine.  It'll help with future questions.

 

by: mathieu_cuprykPosted on 2008-08-26 at 12:05:38ID: 22318206

U know company is too cheap and I don't have admin prev. so this is tough.

 

by: sdstuberPosted on 2008-08-26 at 12:16:21ID: 22318322

you should be able to download and install the SQL*Plus instant client from otn.

No Cost.  (http://www.oracle.com/technology/tech/oci/instantclient/collateral/Instant_Client_WP.pdf)

and you shouldn't need to be an admin to install it.

 

by: slightwvPosted on 2008-08-26 at 12:27:08ID: 22318440

>>and you shouldn't need to be an admin to install it.

All (I figure there's 1 or 2 that don't but I'm saying all anyway) Oracle installs need to update the registry.  Some shops even restrict writes to the 'C' drive.  Oracle writes there as well.

 

by: sdstuberPosted on 2008-08-26 at 12:28:28ID: 22318455

the instant client shouldn't need to do that.

 

by: slightwvPosted on 2008-08-26 at 12:32:54ID: 22318499

Sorry.  I missed the instant client piece.  I didn't know SQL*Plus was part of the instant client.

 

by: sdstuberPosted on 2008-08-26 at 12:36:45ID: 22318531

it's one flavor of the instant client

http://www.oracle.com/technology/tech/sql_plus/index.html

 

by: sdstuberPosted on 2008-08-26 at 12:38:20ID: 22318547

or better yet here, you can see all the different variations of the instant client


http://www.oracle.com/technology/software/tech/oci/instantclient/htdocs/winsoft.html

 

by: slightwvPosted on 2008-08-26 at 12:42:39ID: 22318591

Cool.  That's why I love this site.  I tend to learn as much if not more I teach.

I was all excited until I saw it wasn't sqlplusw...  They discontinued the windows version in 11g and I'm not happy about it.

 

by: sdstuberPosted on 2008-08-26 at 12:46:55ID: 22318637

glad I could help you too!  two-for-one  :)

 

by: mathieu_cuprykPosted on 2008-08-26 at 12:59:40ID: 22318763

well I need to know where should I unzip this too. I have some of the client for 10G.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...