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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:11:40+00:00 2026-06-14T00:11:40+00:00

Assume the following table: TableA: ID GroupName SomeValue 1 C 1 2 C 1

  • 0

Assume the following table:

TableA:

ID  GroupName SomeValue
1   C         1
2   C         1
2   B         1
2   A         1

I need to construct a query that selects the following result:

ID  GroupName SomeValue
1   C         1
1   B         0
1   A         0
2   C         1
2   B         1
2   A         1

The GroupName is actually derived from TableA column’s CASE expression and can take only 3 values: A, B, C.

Are the analytic functions the way to go?

EDIT

Sorry, for not mentioning it, but the ID could consist of multiple columns. Consider this example:

ID1 ID2 GroupName SomeValue
1   1   C         1

1   2   C         1

2   2   C         1
2   2   B         1
2   2   A         1

I need to pad SomeValue with 0 for each unique combination ID1+ID2. So the result should be like this:

ID1 ID2 GroupName SomeValue

1   1   C         1
1   1   B         0
1   1   A         0

1   2   C         1
1   2   B         0
1   2   A         0

2   2   C         1
2   2   B         1
2   2   A         1  

EDIT2
Seems like solution, proposed by @Laurence should work even for multiple-column ‘ID’. I couldn’t rewrite the query proposed by @Nicholas Krasnov to conform to this requirement. But could somebody compare these solutions performance-wise? Will the analytic function work faster than ‘cross join + left outer join’?

  • 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-14T00:11:40+00:00Added an answer on June 14, 2026 at 12:11 am

    To fill in gaps, you could write a similar query using partition by clause of outer join:

    SQL> with t1(ID,GroupName,SomeValue) as
      2  (
      3    select 1,   'C',   1  from dual union all
      4    select 2,   'C',   1  from dual union all
      5    select 2,   'B',   1  from dual union all
      6    select 2,   'A',   1  from dual
      7  ),
      8  groups(group_name) as(
      9    select 'A' from dual union all
     10    select 'B' from dual union all
     11    select 'C' from dual
     12  )
     13  select t1.ID
     14       , g.group_name
     15       , nvl(SomeValue, 0) SomeValue
     16    from t1
     17    partition by (t1.Id)
     18    right outer join groups g
     19       on (t1.GroupName = g.group_name)
     20    order by t1.ID asc, g.group_name desc
     21  ;
    
            ID GROUP_NAME  SOMEVALUE
    ---------- ---------- ----------
             1 C                   1
             1 B                   0
             1 A                   0
             2 C                   1
             2 B                   1
             2 A                   1
    
    6 rows selected
    

    UPDATE: Response to the comment.

    Specify ID2 column in the partition by clause as well:

    SQL> with t1(ID1, ID2, GroupName,SomeValue) as
      2  (
      3    select 1, 1, 'C', 1 from dual union all
      4    select 1, 2, 'C', 1 from dual union all
      5    select 2, 2, 'C', 1  from dual union all
      6    select 2, 2, 'B', 1  from dual union all
      7    select 2, 2, 'A', 1  from dual
      8  ),
      9  groups(group_name) as(
     10    select 'A' from dual union all
     11    select 'B' from dual union all
     12    select 'C' from dual
     13  )
     14  select t1.ID1
     15       , t1.ID2
     16       , g.group_name
     17       , nvl(SomeValue, 0) SomeValue
     18    from t1
     19    partition by (t1.Id1, t1.Id2)
     20    right outer join groups g
     21    on (t1.GroupName = g.group_name)
     22  order by t1.ID1, t1.ID2  asc , g.group_name desc
     23  ;
    
           ID1        ID2 GROUP_NAME  SOMEVALUE
    ---------- ---------- ---------- ----------
             1          1 C                   1
             1          1 B                   0
             1          1 A                   0
             1          2 C                   1
             1          2 B                   0
             1          2 A                   0
             2          2 C                   1
             2          2 B                   1
             2          2 A                   1
    
    9 rows selected
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's assume that I have the following table: CREATE TABLE [dbo].[PricesHist] ( [Product] [varchar](6)
Assume I have the following style table, col1 col2 and col3 have same value
Assume the following function prototype: void *function(int arg) . Is that a pointer to
Assume that the following Perl code is given: my $user_supplied_string = &retrieved_from_untrusted_user(); $user_supplied_string =~
Consider the following table: People FirstName nvarchar(50) LastName nvarchar(50) Let's assume for the moment
I have a mysql table, For simplicity's sake assume the following table was used
Assume the following mapping entries: < class name=Customer table=customer> <id name=InternalId column=uintCustomerId type=long> <generator
Let's assume the following: Table A id | value ---------- 1 | red 2
Assume we have the following table: id name member 1 jacky a;b;c 2 jason
Assume we have the following table: Id A B 1 10 ABC 2 10

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.