Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8537033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:48:08+00:00 2026-06-11T10:48:08+00:00

Is it possible to have each WHEN in a case statement be output in

  • 0

Is it possible to have each WHEN in a case statement be output in its own column? I tried it but with no luck. The closest I have come to what I want is below.

I currently have a query as follows:

select BH.BATCH                 as batch_number, 
       BS.TBS_DESCRIPTION       as batch_description,
       BH.NUM_VOUCHERS          as "NUM_VOUCHERS(BATCH)",
       case when VS.TVS_CODE = 1 then count(*) else 0 end as in_stock,
       case when VS.TVS_CODE = 3 then count(*) else 0 end as terminate,
       case when VS.TVS_CODE = 5 then count(*) else 0 end as in_progress,
       case when VS.TVS_CODE = 6 then count(*) else 0 end as used,
       case when VS.TVS_CODE = 8 then count(*) else 0 end as deactivate
from BATCH_HIST   bh,
     BATCH_STATUS bs,
     VOUCH_HIST   vh,
     VOUCH_STATUS vs
where substr(BH.ALLOCAT_DATE, 1, 6) = 201207
and   to_char(BH.STATUS) = BS.TBS_CODE
and   BH.BATCH = VH.BATCH
and   to_char(VH.STATUS) = VS.TVS_CODE
group by BH.BATCH,
         BS.TBS_DESCRIPTION,
         BH.NUM_VOUCHERS,
         VS.TVS_CODE
order by 1,2,3;

This gives me the following output, with each STATUS as a different record:

BATCH_NUMBER  | BATCH_DESCRIPTION  | NUM_VOUCHERS(BATCH)  | IN_STOCK  | TERMINATE  | IN_PROGRESS  | USED  | DEACTIVATE
------------------------------------------------------------------------------------------------------------------------------
4413565       |  Allocate          |  100                 |  67       |  0         |  0           |  0    |  0
4413565       |  Allocate          |  100                 |  0        |  0         |  0           |  33   |  0
4413566       |  Allocate          |  100                 |  63       |  0         |  0           |  0    |  0
4413566       |  Allocate          |  100                 |  0        |  0         |  0           |  37   |  0

I want this grouped together, so that there is one record per BATCH_NUMBER:

BATCH_NUMBER  | BATCH_DESCRIPTION  | NUM_VOUCHERS(BATCH)  | IN_STOCK  | TERMINATE  | IN_PROGRESS  | USED  | DEACTIVATE
------------------------------------------------------------------------------------------------------------------------------
4413565       |  Allocate          |  100                 |  67       |  0         |  0           |  33   |  0
4413566       |  Allocate          |  100                 |  63       |  0         |  0           |  37   |  0
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T10:48:10+00:00Added an answer on June 11, 2026 at 10:48 am

    Expression, as you have written it, requires group by, but if you sum matches you can eliminate tvs_code from group by:

    select BH.BATCH                 as batch_number, 
           BS.TBS_DESCRIPTION       as batch_description,
           BH.NUM_VOUCHERS          as "NUM_VOUCHERS(BATCH)",
           sum (case when VS.TVS_CODE = 1 then 1 else 0 end) as in_stock,
           sum (case when VS.TVS_CODE = 3 then 1 else 0 end) as terminate,
           sum (case when VS.TVS_CODE = 5 then 1 else 0 end) as in_progress,
           sum (case when VS.TVS_CODE = 6 then 1 else 0 end) as used,
           sum (case when VS.TVS_CODE = 8 then 1 else 0 end) as deactivate
    from BATCH_HIST   bh,
         BATCH_STATUS bs,
         VOUCH_HIST   vh,
         VOUCH_STATUS vs
    where substr(BH.ALLOCAT_DATE, 1, 6) = 201207
    and   to_char(BH.STATUS) = BS.TBS_CODE
    and   BH.BATCH = VH.BATCH
    and   to_char(VH.STATUS) = VS.TVS_CODE
    group by BH.BATCH,
             BS.TBS_DESCRIPTION,
             BH.NUM_VOUCHERS
    order by 1,2,3;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to have multiple has_many :through relationships that pass through each other
In my understanding each applets are independent entities . Is it possible to have
Possible Duplicate: I have an array of integers, how do I use each one
Possible Duplicate: Linq - Top value form each group I have data in the
Possible Duplicate: Retrieving the last record in each group I have two tables set
Possible Duplicate: jquery data selector I have several elements with class 'connection_name'. Each one
I have the following code which I use to match fancybox possible elements: $('a.grouped_elements').each(function(){
Let's say I have n marbles, and each can be one of 8 possible
Possible Duplicate: Why was the switch statement designed to need a break? I have
Is it possible have two projects with the same name in flex builder? Here

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.