Choose from more than one option?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Choose from more than one option?

  1. #1
    JAN Guest

    Choose from more than one option?


    Hi, i need to do something like:

    select
    decode(table.a,'A', sum(this)) debt
    decode(table.a,'B', sum(this)) cred
    sum(this) total
    from
    table

    But i need that 'A' is not only one data of the item, for example i want
    that the query do the sum
    if table.a is equal to 'A', 'C', 'D'. Something like this.

    select
    if table.a in ('A','B','C') sum(this) debt
    else sum(this) cred
    sum(this) total
    from
    table

    but i dont know how to do this in sql, any help please


  2. #2
    D. Patrick Hoerter Guest

    Re: Choose from more than one option?

    JAN,

    Your post seems to make a mutually exclusive request...

    Can you clarify some?

    Regards,
    D. Patrick Hoerter

    JAN <jfelix@correo.mty.itesm.mx> wrote in message
    news:3a146d47$1@news.devx.com...
    >
    > Hi, i need to do something like:
    >
    > select
    > decode(table.a,'A', sum(this)) debt
    > decode(table.a,'B', sum(this)) cred
    > sum(this) total
    > from
    > table
    >
    > But i need that 'A' is not only one data of the item, for example i want
    > that the query do the sum
    > if table.a is equal to 'A', 'C', 'D'. Something like this.
    >
    > select
    > if table.a in ('A','B','C') sum(this) debt
    > else sum(this) cred
    > sum(this) total
    > from
    > table
    >
    > but i dont know how to do this in sql, any help please
    >




  3. #3
    Michael Levy Guest

    Re: Choose from more than one option?

    I think you're looking for something like this:

    SELECT
    SUM(CASE WHEN table.a IN ('A', 'C', 'D') THEN this ELSE 0) AS debt,
    SUM(CASE WHEN table.a = 'B' THEN this ELSE 0) AS crdt,
    SUM(this) AS total
    FROM table

    -Mike
    --
    Michael Levy MCDBA, MCSD, MCT
    michaell@gasullivan.com



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center