Solved

Help in Getting highest 20 rows (SQL query)

  • 7 May 2023
  • 4 replies
  • 56 views

Userlevel 1
Badge +5

Trying to get highest 20 rows of the below query based on “Net Amount in USD” column which is a sum column. Any clues?

 

select 

 INVOICE.IDENTITY as "Identity", 
supplier.vendor_name,
to_char(sum(INVOICE.ACTUAL_NET_DOM_AMOUNT), '$999,999,999') as "Net Amount in USD"

 from &AO.INVOICE 

inner join

&AO.supplier

on &AO.Invoice.identity = &AO.supplier.vendor_no

where   INVOICE.OBJSTATE <> 'Cancelled' 

group by

 INVOICE.IDENTITY,vendor_name
 

icon

Best answer by Tomas Ruderfelt 8 May 2023, 07:26

View original

4 replies

Userlevel 7
Badge +18

Hi @Housseiny, 
You can try several options.
First Order by using the ‘Net Amount in USD’ value.
Then try as below.

ex: 
# Add below to the beginning of the select statement.SELECT TOP (20) PERCENT

Or
# Add below where condition to the SQL Query
WHERE ROWNUM <= 20

Or
#Add below condition to end of the Select Statement
FETCH FIRST 20 ROWS ONLY.

Best Regards
-Kelum
 

Userlevel 7
Badge +19

Like this, had to remove &AO when I tested but I guess you get the idea:

 

SELECT i.identity "Supplier ID", s.vendor_name "Supplier Name", to_char(sum(i.actual_net_dom_amount), '$999,999,999') as "Net Amount in USD"
FROM invoice i
INNER JOIN supplier s
ON i.identity = s.vendor_no
WHERE i.objstate <> 'Cancelled' 
AND i.party_type_db = 'SUPPLIER'
GROUP BY i.identity,s.vendor_name
ORDER BY sum(i.actual_net_dom_amount) DESC
FETCH FIRST 20 ROWS ONLY

Userlevel 1
Badge +5

Hi @Housseiny, 
You can try several options.
First Order by using the ‘Net Amount in USD’ value.
Then try as below.

ex: 
# Add below to the beginning of the select statement.SELECT TOP (20) PERCENT

Or
# Add below where condition to the SQL Query
WHERE ROWNUM <= 20

Or
#Add below condition to end of the Select Statement
FETCH FIRST 20 ROWS ONLY.

Best Regards
-Kelum
 

Many thanks Kelum for your prompt feedback

Userlevel 1
Badge +5

Like this, had to remove &AO when I tested but I guess you get the idea:

 

SELECT i.identity "Supplier ID", s.vendor_name "Supplier Name", to_char(sum(i.actual_net_dom_amount), '$999,999,999') as "Net Amount in USD"
FROM invoice i
INNER JOIN supplier s
ON i.identity = s.vendor_no
WHERE i.objstate <> 'Cancelled' 
AND i.party_type_db = 'SUPPLIER'
GROUP BY i.identity,s.vendor_name
ORDER BY sum(i.actual_net_dom_amount) DESC
FETCH FIRST 20 ROWS ONLY

Many thanks Tomas, worked like a charm

Reply