3
select col1,col2,col3 from tab1 union all
select col1,col2,col3 from tab2

o/p-

COL1       COL2       COL3
6518    10060152650 534010002   
6518    10060152651 534010002   
6518    10060152652 534020003   
6526    10060153296 534004002   
6526    10060153310 534004542   
6526    10060153365 533011103   
6526    10060153328 534010002   
6526    10060153348 534010002   

I want it like this

COL1  COL2         COL3
6518  10060152650  534010002    
      10060152651  534010002    
      10060152652  534020003    
6526  10060153296  534004002    
      10060153310  534004542    
      10060153365  533011103    
      10060153328  534010002    
      10060153348  534010002

can any one help me out..!!

1
  • A little effort in formatting goes a long way in getting quick responses. Commented Dec 10, 2013 at 8:04

3 Answers 3

5

The SQL developer shows you the rows you select. As your rows contain a value in Col1 it will be shown. So your only solution is not to select it, or in your example: not to select it when the line before contains the same value already. Don't forget to sort.

select 
  case when col1 = lag(col1) over (order by col1, col2, col3) then
    null 
  else 
    col1
  end as colx
, col2
, col3
from
(
  select col1,col2,col3 from tab1 union all
  select col1,col2,col3 from tab2
)
order by col1, col2, col3
Sign up to request clarification or add additional context in comments.

4 Comments

Ah, thanks for pointing that out. It's because I used col1 as an alias and the order by worked on that aslias rather than on the original field. (I think this was different in Ora 10g.) I'm correcting it. Here is the altered SQL fiddle: sqlfiddle.com/#!4/02c4d/11
Works perfectly this way :)
@ajmalmhd04: Don't remove the ORDER BY. Without it, the result is not guaranteed to be oredered.
Just checked it; I was wrong assuming that this was different in Ora 10g. It was exactly the same.
2

This will work for you:

SELECT CASE 
          WHEN RANK() OVER (PARTITION BY col1 ORDER BY col2) <> 1
            THEN ''
          ELSE TO_CHAR(col1)
        END AS col1,
    col2,
    col3
FROM (
    SELECT col1, col2, col3
    FROM tab1

    UNION ALL

    SELECT col1, col2, col3
    FROM tab2
    )

Here is an SQLFiddle with how the query works.

Comments

0

sql*plus has report layout commands. SQLDeveloper displays a data grid.

SQL>break on col1 nodup
SQL>select col1,col2,col3 from tab1 union all select col1,col2,col3 from tab2;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.