Professional Web Applications Themes

DB2 UDB 8.1 Grouping Question, sort of... - IBM DB2

I have a table test.sub_chnl_ref which has the following structure: chnl_id | chnl_desc | sub_chnl_id | sub_chnl_desc | --------|---------------|---------------|-------------------| 1 | Domestic | 1 | Dom. Direct Mail | 1 | Domestic | 2 | Dom. Retail | 2 | International | 3 | Canada Direct Mail| 2 | International | 4 | Canada Retail | 2 | International | 5 | Other Direct Mail | etc ... ... ... Is it possible to create a query where the result set looks like the following: chnl_id chnl_desc sub_chnl_id sub_chnl_desc ---------------------------------------- ------------------- 1 Domestic 1 Dom. Direct Mail 2 Dom. Retail ...

  1. #1

    Default DB2 UDB 8.1 Grouping Question, sort of...

    I have a table test.sub_chnl_ref which has the following structure:

    chnl_id | chnl_desc | sub_chnl_id | sub_chnl_desc |
    --------|---------------|---------------|-------------------|
    1 | Domestic | 1 | Dom. Direct Mail |
    1 | Domestic | 2 | Dom. Retail |
    2 | International | 3 | Canada Direct Mail|
    2 | International | 4 | Canada Retail |
    2 | International | 5 | Other Direct Mail |

    etc ... ... ...

    Is it possible to create a query where the result set looks like the
    following:

    chnl_id chnl_desc sub_chnl_id sub_chnl_desc
    ---------------------------------------- -------------------
    1 Domestic 1 Dom. Direct Mail
    2 Dom. Retail
    2 International 3 Canada Direct Mail
    4 Canada Retail
    5 Other Direct Mail


    i.e. I want the first two columns to have a value only for the first
    row returned for those particular values. I know I can do this sort
    of thing with some basic reporting tool, but I would like to do it in
    a single SQL query if possible, as it would be easier for me to
    integrate with other work I am doing.

    - Bebe
    Brian Bouchard Guest

  2. #2

    Default Re: DB2 UDB 8.1 Grouping Question, sort of...

    How about this?
    SELECT CASE ROWNUMBER() OVER(PARTITION BY chnl_id ORDER BY sub_chnl_id)
    WHEN 1 THEN chnl_id
    ELSE ''
    END AS "Chnl_id "
    , CASE ROWNUMBER() OVER(PARTITION BY chnl_id ORDER BY sub_chnl_id)
    WHEN 1 THEN chnl_desc
    ELSE ''
    END AS chnl_desc
    , sub_chnl_id , sub_chnl_desc
    FROM test.sub_chnl_ref
    ORDER BY chnl_id, sub_chnl_id
    Tokunaga T. Guest

Similar Threads

  1. CFReport Builder Grouping Question--Urgent!!
    By aeiueo in forum Coldfusion Flash Integration
    Replies: 2
    Last Post: July 3rd, 09:24 PM
  2. query/grouping question
    By coolidge in forum Coldfusion - Getting Started
    Replies: 1
    Last Post: April 8th, 11:57 PM
  3. Replies: 1
    Last Post: September 9th, 07:16 AM
  4. sort question
    By Gunnar Hjalmarsson in forum PERL Miscellaneous
    Replies: 4
    Last Post: August 10th, 05:32 PM
  5. Sort of an iPhoto question
    By Fred G in forum Mac Applications & Software
    Replies: 3
    Last Post: July 2nd, 04:37 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139