Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Microsoft Access FIRST () Equivalent in Oracle SQL

I have a simple query in MS Access database

SELECT
ID,
FIRST(LEVEL),
FIRST(NAME),
FIRST(DESIGNATION)
FROM TABLE 1
WHERE FIRST(LEVEL)<> '1';

What is the Oracle SQL equivalent of the FIRST() expression.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Something like this might be one option:

  • use a CTE to fetch all columns you need, along with row_number analytic function which partitions data on id and sorts them by … what, exactly? What is "first" in your case? I chose id; if it is not, use column you really want
  • then, in another CTE, use max aggregate function with a condition which says that only value – whose row number equals 1 – should be returned
  • finally, filter data

Note that you can’t (OK, you can, but probably shouldn’t) name column level in Oracle as it is reserved for pseudocolumn. That’s why I used c_level.

with 
temp as
  (select id, c_level, name, designation,
     row_number() over (partition by id order by id) rn
   from table_1
  ),
temp2 as
  (select id,
     max(case when rn = 1 then c_level end) c_level,
     max(case when rn = 1 then name end) name,
     max(case when rn = 1 then designation end) designation
   from temp
   group by id)
select id, c_level, name, designation
from temp
where c_level <> '1';
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading