• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • More Conditional Sorting with SQL

    November 10, 2004 Hey, Ted

    Your tip “Conditional Sorting with SQL” is one cool tip! May I add two more examples?

    My examples are in RPG, but I assume they will work with any language. In both examples, I use host variables to control the sort.

    Host variable SortOption controls the sort in the first example. If host variable SortOption has a value of 1, the data is retrieved in order by customer name. If two customers have the same name, they are retrieved in customer number sequence. If SortOption has any other value, the data is retrieved in customer number sequence.

    D SortOption      S              1P 0
    
    C/exec sql
    C+
    C+   Declare C1 Cursor for
    C+   select cusnum, lstnam, init
    C+   from qiws/qcustcdt
    C+   order by
    C+      case when :SortOption = 1
    C+             then lstnam
    C+             else digits(cusnum) end,
    C+      case when :SortOption = 1
    C+             then cusnum
    C+             else 0 end
    C+
    C/end-exec
    

    In the second example, the data may be sorted by state, in either ascending or descending sequence. If host variable SortSeq has a value of A, the data is returned in ascending order by state. Within state, the data is sorted by last name and initials. A SortSeq value of D causes the states to be sorted in descending order, but the last name and initial fields are sorted in ascending order. If SortSeq has some other value, state is not used for sorting.

    D SortSeq         S              1A
    
    C/exec sql
    C+
    C+   Declare C1 Cursor for
    C+   select state, lstnam, init from qiws/qcustcdt
    C+   order by
    C+      case when :SortSeq = 'A' then state else ' ' end,
    C+      case when :SortSeq = 'D' then state else ' ' end desc,
    C+      lstnam,
    C+      init
    C+
    C/end-exec
    

    –John

    Thanks for the ideas, John. Several readers wrote in to say that they liked the tip, but none of them sent examples.


    Using host variables to affect the behavior of SQL commands is one way to add flexibility to an application. For more information, see these articles from previous editions of this newsletter.

    “Alternate SQL Row-Selection Criteria”

    “Alternate SQL Row-Selection Criteria, Take 2”

    –Ted

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    Manta Technologies

    The Leader in IBM i Education!
    Need training on anything i?
    Manta is all you need.

    Spring Sale! Save 20% off any Manta Combination Package, including the complete IBM i Training Library. Now through April 30.

    130 courses and competency exams on:
    · IBM i operations
    · System Management and Security
    · IBM i Programming Tools
    · Programming in RPG, COBOL, CL, Java
    · Web Development
    · SQL, DB2, Query

    Product features:
    · Runs in every popular browser
    · Available 24/7/365
    · Free Student Reference Guides
    · Free Student Administration
    · Concurrent User License
    · Built-In IBM i Simulator

    You can download our 200-page catalog and take sample sessions at MantaTech.com.

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Symantec Adds Regulatory Compliance to Security Management Tool How the i5s Compare with Other Big Boxes

    Leave a Reply Cancel reply

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • LANSA Developing Business Intelligence Tool
  • Blazing The Trail For VTL In The Cloud
  • Data De-Dupe Gives VTL Customers More Options
  • Four Hundred Monitor, March 29
  • The Big Spending On IT Security Is Only Going To Get Bigger
  • IBM Tweaks Some Power Systems Prices Down, Others Up
  • Disaster Recovery: From OS/400 V5R3 To IBM i 7.4 In 36 Hours
  • The Disconnect In Modernization Planning And Execution
  • Superior Support: One Of The Reasons You Pay The Power Systems Premium
  • IBM i PTF Guide, Volume 25, Number 13

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2023 IT Jungle