Search This Blog

Monday, April 6, 2015

Query with order by and different types of it

Requirement to show elements 'Basic Salary', 'Basic Grand sum', 'Accomadation Allowances' at top of sql query of elements

select pet.element_name from pay_element_types_f pet

---OUTPUT

Acccomadation Allowance
Basic Salary
Basic Grand sum

--desired output.
Basic Salary
Basic Grand sum
Acccomadation Allowance




1.
order by specific data at top for single query then
order by                              
decode(column,'Basic Salary',1,
                                  'Basic Grand Sum',2,
                                   'Accommodation Allowance',3,4);
                                  
2.
order by specific data at top for multiple queries using union
select * from
( select 1.col1 A, 1.col2 from table1 1
union
select  2.col1 A, 2.col2 from table2 2
union
select  3.col1 A, 3.col2 from table3 3
)
order by
decode(A,'Basic Salary',1,--here A is the alias of column
                                  'Basic Grand Sum',2,
                                   'Accommodation Allowance',3,4);
                                  
3.
order by for mutliple queries
use UNION ALL, data from the first query will show first.


4.
can use decode or else the case statement also

order by
case when column = 'Basic Salary' then 1 else 2 end;                          
                                   

No comments:

Post a Comment