0

I'm having difficulty querying the top 25 values. My table contains transaction dates, locations, UPC number, and sales quantity.

This is what I have so far -

SELECT date_id as "Date", store_id as "Store", sum(Sales_Unit) as "Total Sales" FROM fact_pos_sales GROUP BY date_id, store_id ORDER BY "Total Sales" DESC ;

This is what I'm hoping to get as my result, except for just the top 25 values. I've tried adding "Top 25" next to Select, or adding a "Limit 25" at the end of my sql, except none of these two methods work. I'm using Oracle SQL Developer, for what it's worth.

Current output, without the limit in place

Would someone be able to advise? I've been trying to search for the answers on Stockoverflow and elsewhere for the past two hours, with no results. Thank you!

3 Answers3

0

Not all database systems support the SELECT TOP clause (that's for SQL Server). MySQL supports the LIMIT clause to select a limited number of records, while Oracle uses ROWNUM.

Therefore something like:

SELECT * From 
 (
     SELECT date_id as "Date", store_id as "Store", sum(Sales_Unit) as "Total Sales" 
     FROM fact_pos_sales 
     GROUP BY date_id, store_id 
     ORDER BY "Total Sales" DESC
 ) 
WHERE ROWNUM <= 25;
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
0
Select * From
(
   SELECT date_id as "Date", 
          store_id as "Store", 
          sum(Sales_Unit) as "Total Sales" 
     FROM fact_pos_sales 
    GROUP BY date_id, store_id 
    ORDER BY "Total Sales" DESC ;
) 
Where RowNum <=25  
0

In Oracle 12c:

SELECT date_id as "Date", 
      store_id as "Store", 
      sum(Sales_Unit) as "Total Sales" 
FROM fact_pos_sales 
GROUP BY date_id, store_id 
ORDER BY "Total Sales" DESC
FETCH FIRST 25 ROWS ONLY;
Bobby Durrett
  • 1,223
  • 12
  • 19