Question :- How to find Top N on different database engines ?
Solution :-
Teradata SQL :-
select top 10 COL1, SUM(MEASURE) from TABLE
group by 1
order by 2 desc
SQL Server :-
select top 10 COL1, SUM(MEASURE) from TABLE
group by COL1
order by 2 desc
As you can see in the above examples that SQLs for teradata and SQL Server are similar. However Oracle engine does not accept TOP function therefore we will be using ROWNUM function, instead. You can also use Rank function in Oracle but i like to use rownum function, which is easier than rank.
Oracle :-
SELECT JOB_TITLE, SAL FROM
(select JOB_Title, SUM(MAX_SALARY) SAL from JOBS
group by Job_title
order by 2 desc ) A
WHERE ROWNUM <=10;
No comments:
Post a Comment