SQL query TOP syntax not working

Shweta

Well-known Member
Joined
Jun 5, 2011
Messages
514
Hi All,

Below is the query I used to combine all records from UK_Sale and US_Sale Tables.

Code:
select Emp_Name, sum(sales)as TotalSales
from uk_sale 
group by emp_name
union all
select Emp_Name, sum(sales)as TotalSales
from us_sale 
group by emp_name
order by TotalSales desc

I got below table as a result of the above query:

Emp_NameTotalSales
F 4000
H 3950
A 2500
G 2290
E 1950
B 500
C 250
D 200

<colgroup><col><col></colgroup><tbody>
</tbody>

Now from the above result I want to find out the fourth highest Sale along with employee name i.e. G with 2290. For this, I am thinking to use DISTINCT TOP 4 to get the top four records (as data is sorted) and then I will use MIN function to get the 4th record. but I am not sure how to apply this logic.

Kindly help.

Thanks in advance!

Regards,
Shweta Jain
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
I don't think we can do this in one Query.
Try this:

1. Save the Union Query with the Name: Query1.
2. Create another Query using Query1 as source, SQL is given below:
Code:
SELECT TOP 4 Query1.Emp_Name, Query1.TotalSales
FROM Query1;
3. Save the above Query with the Name: Query2
4. The next step, frankly I have never created one like this before but serves the purpose. SQL is given below:
Code:
SELECT DLast("Emp_Name","Query2","TotalSales=" & DMin("TotalSales","Query2")) AS EmpName, DMin("TotalSales","Query2") AS Total_Sales
FROM Query2
GROUP BY DLast("Emp_Name","Query2","TotalSales=" & DMin("TotalSales","Query2")), DMin("TotalSales","Query2");
 
Upvote 0
this seems to work

Code:
select 
  top 1
  Emp_Name, 
  TotalSales
FROM 
(
SELECT 
  top 4 
  Emp_Name, 
  TotalSales
FROM 
  your_query 
order by 
  TotalSales Desc
)
order by 
  TotalSales Asc
 
Upvote 0
TOP allows for ties. Is that a problem? I don't think you can guarantee results with TOP.
 
Upvote 0

Forum statistics

Threads
1,221,813
Messages
6,162,126
Members
451,743
Latest member
matt3388

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top