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

  • SEARCH
  • Home
  • 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 6910881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:52:20+00:00 2026-05-27T08:52:20+00:00

I have the following table named foo: ID | KEY | VAL —————- 1

  • 0

I have the following table named foo:

ID | KEY  | VAL
----------------
 1 | 47   | 97
 2 | 47   | 98
 3 | 47   | 99
 4 | 48   | 100
 5 | 48   | 101
 6 | 49   | 102

I want to run a select query and have the results show like this

UNIQUE_ID        | KEY  | ID1 | VAL1 | ID2 | VAL2 | ID3 | VAL3
--------------------------------------------------------------
47_1:97_2:98_3:99|  47  | 1   |  97  | 2   |  98  |  3  | 99
48_4:100_5:101   |  48  | 4   |  100 | 5   |  101 |     |  
49_6:102         |  49  | 6   |  102 |     |      |     | 

So, basically all rows with the same KEY get collapsed into 1 row. There can be anywhere from 1-3 rows per KEY value

Is there a way to do this in a sql query (without writing a stored procedure or scripts)?

If not, I could also work with the less desirable choice of

UNIQUE_ID        | KEY  | IDS   | VALS 
--------------------------------------------------------------
47_1:97_2:98_3:99|  47  | 1,2,3 | 97,98,99
48_4:100_5:101   |  48  | 4,5   | 100, 101 
49_6:102         |  49  | 6     | 102 

Thanks!

UPDATE:

Unfortunately my real-world problem seems to be much more difficult than this example, and I’m having trouble getting either example to work 🙁 My query is over 120 lines so it’s not very easy to post. It kind of looks like:

with v_table as (select ...), 
  v_table2 as (select foo from v_table where...), 
  v_table3 as (select foo from v_table where ...),
  ...
  v_table23 as (select foo from v_table where ...)
  select distinct (...) as "UniqueID", myKey, myVal, otherCol1, ..., otherCol18 
  from tbl1 inner join tbl2 on...
  ... 
  inner join tbl15 on ...

If I try any of the methods below it seems that I cannot do group-bys correctly because of all the other data being returned.
Ex:

with v_table as (select ...), 
  v_table2 as (select foo from v_table where...), 
  v_table3 as (select foo from v_table where ...),
  ...
  v_table23 as (select foo from v_table where ...)
  select "Unique ID",
   myKey, max(decode(id_col,1,id_col)) as id_1, max(decode(id_col,1,myVal)) as val_1,
   max(decode(id_col,2,id_col)) as id_2,max(decode(id_col,2,myVal)) as val_2,
   max(decode(id_col,3,id_col)) as id_3,max(decode(id_col,3,myVal)) as val_3
  from (
    select distinct (...) as "UniqueID", myKey, row_number() over (partition by myKey order by id) as id_col, id, myVal, otherCol1, ..., otherCol18 
    from tbl1 inner join tbl2 on...
    ... 
    inner join tbl15 on ...
  ) group by myKey;

Gives me the error: ORA-00979: not a GROUP BY expression

This is because I am selecting the UniqueID from the inner select. I will need to do this as well as select other columns from the inner table.

Any help would be appreciated!

  • 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-05-27T08:52:21+00:00Added an answer on May 27, 2026 at 8:52 am

    You may try this

    select key,
        max(decode(id_col,1,id_col)) as id_1,max(decode(id_col,1,val)) as val_1,
        max(decode(id_col,2,id_col)) as id_2,max(decode(id_col,2,val)) as val_2,
        max(decode(id_col,3,id_col)) as id_3,max(decode(id_col,3,val)) as val_3
    from (
            select key, row_number() over (partition by key order by id) as id_col,id,val
            from your_table
        )
    group by key
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following query: select column_name, count(column_name) from table group by column_name having
I have the following query: SELECT * from foo where timestamp = (select max(timestamp)
I have a table named with Sales having the following columns: Sales_ID|Product_Code|Zone|District|State|Distributor|Total_Sales Now i
I have a table with the following fields: id VARCHAR(32) PRIMARY KEY, parent VARCHAR(32),
I have a table foo which contains the following columns create table foo (
I have a table with 2 columns, both having names I want a query
I have a comma separated file named foo.csv containing the following data: scale, serial,
Assume I have the following tables: TABLE: foo - foo_id (PK) TABLE: tag -
I have following query: SELECT * FROM ( SELECT catalog.*, images.image FROM `catalog` as
I have the following table in SQL Server 2000: TABLE_NAME | COLUMN_NAME | TYPE_NAME

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.