Professional Web Applications Themes

Finding grand total - IBM DB2

Hi, I have the following table: NAME varchar(10) V1 integer V2 integer V3 integer Values in the table are like: NAME V1 V2 V3 ---------- ----------- ----------- ----------- a 11 16 11 b 20 20 12 c 25 35 13 c 25 35 14 c 25 35 15 I run the SQL: select name, v2-v1 as difference from road.aa where v3>10 group by name, v1, v2 The group by clause is used to avoid retrieving duplicate values (V2-V1 can have duplicate records). The result is: NAME DIFFERENCE ---------- ----------- a 5 b 0 c 10 I now need to find ...

  1. #1

    Default Finding grand total

    Hi,

    I have the following table:

    NAME varchar(10)
    V1 integer
    V2 integer
    V3 integer

    Values in the table are like:
    NAME V1 V2 V3
    ---------- ----------- ----------- -----------
    a 11 16 11
    b 20 20 12
    c 25 35 13
    c 25 35 14
    c 25 35 15

    I run the SQL:

    select name, v2-v1 as difference
    from road.aa
    where v3>10
    group by name, v1, v2

    The group by clause is used to avoid retrieving duplicate values
    (V2-V1 can have duplicate records).

    The result is:

    NAME DIFFERENCE
    ---------- -----------
    a 5
    b 0
    c 10

    I now need to find the total for the column DIFFERENCE. How can I do
    this?

    If use GROUP BY GROUPING SETS and put a SUM(V2-V1) it calculates the
    SUM of the duplicate recrods as well (which I want to avoid). Please
    advice.

    Regards,
    Ashish
    Ashish Guest

  2. #2

    Default Re: Finding grand total

    Ashish <ashishhcl> wrote:
    > Hi,
    >
    > I have the following table:
    >
    > NAME varchar(10)
    > V1 integer
    > V2 integer
    > V3 integer
    >
    > Values in the table are like:
    > NAME V1 V2 V3
    > ---------- ----------- ----------- -----------
    > a 11 16 11
    > b 20 20 12
    > c 25 35 13
    > c 25 35 14
    > c 25 35 15
    >
    > I run the SQL:
    >
    > select name, v2-v1 as difference
    > from road.aa
    > where v3>10
    > group by name, v1, v2
    >
    > The group by clause is used to avoid retrieving duplicate values
    > (V2-V1 can have duplicate records).
    >
    > The result is:
    >
    > NAME DIFFERENCE
    > ---------- -----------
    > a 5
    > b 0
    > c 10
    >
    > I now need to find the total for the column DIFFERENCE. How can I do
    > this?
    SELECT SUM(difference)
    FROM ( select name, v2-v1 as difference
    from road.aa
    where v3>10
    group by name, v1, v2 ) AS n

    --
    Knut Stolze
    Information Integration
    IBM Germany / University of Jena
    Knut Stolze Guest

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