Home » SQL & PL/SQL » SQL & PL/SQL » 2 Rows Data into 1 Row
2 Rows Data into 1 Row [message #681130] Thu, 18 June 2020 06:34 Go to next message
Genesys
Messages: 45
Registered: August 2010
Member
Dear All,

Have data like below

code desc val1 val2

123 abc 0 34
123 abc 33

need output as below

code desc val1 val2
123 abc 33 34

any suggestion pls
Re: 2 Rows Data into 1 Row [message #681131 is a reply to message #681130] Thu, 18 June 2020 06:42 Go to previous messageGo to next message
John Watson
Messages: 8922
Registered: January 2010
Location: Global Village
Senior Member
Another college homework question? You need an aggregation: GROUP BY and MAX.

In future, please use [code] tags as described here You have been asked to do this before. More than once.
Re: 2 Rows Data into 1 Row [message #681133 is a reply to message #681130] Thu, 18 June 2020 09:38 Go to previous messageGo to next message
Michel Cadot
Messages: 68624
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Please read the OraFAQ Forum Guide and How to use [code] tags and make your code easier to read.
Indent the code, use code tags and align the columns in result.

Also always post your Oracle version, with 4 decimals (query v$version), as often solution depends on it.

With any SQL or PL/SQL question, please, Post a working Test case: create table (including all constraints) and insert statements along with the result you want with these data then we will be able work with your table and data. Explain with words and sentences the rules that lead to this result.

Re: 2 Rows Data into 1 Row [message #681137 is a reply to message #681130] Fri, 19 June 2020 06:00 Go to previous message
Solomon Yakobson
Messages: 3269
Registered: January 2010
Location: Connecticut, USA
Senior Member
SQL> with t as (
  2             select 123 code,'abc' descr,0 val1,34 val2 from dual union all
  3             select 123 code,'abc' descr,33 val1,null val2 from dual
  4            )
  5  select  code,
  6          descr,
  7          sum(val1) val1,
  8          sum(val2) val2
  9    from  t
 10    group by code,
 11             descr
 12  /

      CODE DES       VAL1       VAL2
---------- --- ---------- ----------
       123 abc         33         34

SQL>
SY.
Previous Topic: Query not returning correct values (merged)
Next Topic: Loading a CLOB with a KEY
Goto Forum:
  


Current Time: Thu Mar 28 10:34:42 CDT 2024