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 8782541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:35:21+00:00 2026-06-13T20:35:21+00:00

I have a table with status column. I want an Oracle sql query which

  • 0

I have a table with status column. I want an Oracle sql query which will list me count of rows in each status in only one row.
for eg if my table is

Table A
Id       Status  Fkey
1         20      500
2         20      500  
3         30      501
4         40      501
5         30      502

Output should be

Fkey     Count_status20     Count_status30    Count_status40
500        2                      0                 0
501        0                      1                 1

A slight twist here

Table B 
FKey TKey 
500   1001 
501   1001
502   1002 

Now Output should be

TKey Count_status20     Count_status30    Count_status40 
1001     2                     1                    1 
1002     0                     1                    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-13T20:35:22+00:00Added an answer on June 13, 2026 at 8:35 pm

    If you are using Oracle 11g, then you can use the PIVOT function:

    select *
    from
    (
      select tkey, status, 
        status as col
      from tableB b
      left join tableA a
        on a.fkey = b.fkey
    ) src
    pivot
    (
      count(status)
      for col in ('20' as Count_Status20, 
                  '30' as Count_Status30,
                  '40' as Count_Status40)
    ) piv;
    

    See SQL Fiddle with Demo

    If you are not using Oracle11g, then you can use an aggregate function with a CASE statement:

    select tkey, 
      count(case when status = 20 then 1 else null end) as Count_Status20,
      count(case when status = 30 then 1 else null end) as Count_Status30,
      count(case when status = 40 then 1 else null end) as Count_Status40
    from tableB b
    left join tableA a
      on b.fkey = a.fkey
    group by tkey
    

    See SQL Fiddle with Demo

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose we have a table Users that has only one column UserId which is
i have a table column called status - data type is boolean. i want
I want which order have different order status, so I have a table called
I have a MySQL InnoDB table with a status column. The status can be
i have 2 tables, users and follows. table follows has a column named status.
I have a table column where each row has one of three states, and
I have two table emptable1(empid,status) emptable2(empid,week) i want to select all the empid whose
I have a photos table in the database that uses a status column to
I'm trying to query a table which has a column that contains straight xml
I have a table with 3 columns, first column contains service name which is

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.