E
E
eliasum2020-04-11 10:06:39
Oracle
eliasum, 2020-04-11 10:06:39

How to convert query from t-sql to oracle sql?

Hello!
There are two tables:
5e916bf73d386788563464.jpeg

You need to display a list of sellers and the number of their orders that have the maximum number of goods sold.

Query implemented in t-sql:

select top(1) with ties s.Name
     , sum(isnull(o.Amount, 0)) as Quantity
  from Sellers as s
 inner join Orders as o
    on s.id = o.Salesperson_id
 where o.Order_date is not null and o.Order_date > '20091231'
 group by s.id, s.Name
having count(o.Salesperson_id) > 1
 order by sum(isnull(o.Amount, 0)) desc
        , row_number() over(partition by s.id order by (select null))


Can you please tell me how to implement the same query in Oracle SQL Developer?

5e916da8a8bf6610134740.jpeg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2020-04-15
@eliasum

And what's the problem?

select s.Name
     , sum(nvl(o.Amount, 0)) as Quantity
  from Sellers as s,
          Orders as o
 where s.id = o.Salesperson_id 
   and o.Order_date is not null 
   and o.Order_date > to_date('20091231','YYYYMMDD') /*если это date*/
 group by s.id, s.Name
having count(o.Salesperson_id) > 1
 order by sum(nvl(o.Amount, 0)) desc
offset 0 rows fetch next 1 rows only

removed inner join since restrictions are imposed on Orders as o in where
, and this should work
. You can add this
too, row_number() over(partition by s.id order by null) but this does not guarantee the sort order, so you can omit it

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question