MS access sql rank query

CRX2C

New Member
Joined
Nov 20, 2015
Messages
8
Hi Everyone,

I'm trying to create a rank query from access to rank the lowest cost as shown below. Below are all the fields in my table and I am ranking them based on ORG STATE and DEST PROV. The example below is two transportation carriers going to two different origin/destination (CA to AB and CA to ON). I need to rank the lowest cost based on these pairings. I would appreciate if someone can provide me the sql query for this. Thanks in advance

Carrier Name ORG STATEDEST PROV# of shipmentsTotal CostRank
Carrier ACAAB293$50,0001
Carrier BCAAB293$75,0002
Carrier ACAON150$42,0002
Carrier BCAON150$40,0001

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

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Some sites give these answers:
https://bytes.com/topic/access/insights/954764-ranking-queries-ms-access-sql
https://usefulgyaan.wordpress.com/2013/04/23/ranking-in-ms-access/

I'm not sure how they deal with ties. The second URL doesn't explain, but it seems to ignore ties. You could first query for distinct values, then use the given approach, to treat ties equally. The first URL I didn't look at closely enough.

Edit: I guess it goes without saying you can also push/pull your data into an excel workbook and use Excel's rank function as well.
 
Last edited:
Upvote 0
Some sites give these answers:
https://bytes.com/topic/access/insights/954764-ranking-queries-ms-access-sql
https://usefulgyaan.wordpress.com/2013/04/23/ranking-in-ms-access/

I'm not sure how they deal with ties. The second URL doesn't explain, but it seems to ignore ties. You could first query for distinct values, then use the given approach, to treat ties equally. The first URL I didn't look at closely enough.

Edit: I guess it goes without saying you can also push/pull your data into an excel workbook and use Excel's rank function as well.

Thanks. I tried those examples but somehow I keep getting a syntax error.
 
Upvote 0
With your provided data, this was the adaptation of the second url's strategy:
Code:
SELECT T1.[ORG STATE], T1.[DEST PROV], T1.[Total Cost],  
       (SELECT COUNT(T1.[ORG STATE])
          FROM
                 [MyTable] T2
         WHERE 
			T2.[ORG STATE] = T1.[ORG STATE]
			AND T2.[DEST PROV] = T1.[DEST PROV]
			AND T2.[Total Cost] <= T1.[Total Cost]) AS Rank
FROM 
	[MyTable] T1
ORDER BY [ORG STATE], [DEST PROV], [Total Cost]

If we want to count ties equally, one way is to get rid of duplicates first, so:
Define Query1 first:
Code:
SELECT DISTINCT [ORG STATE], [DEST PROV], [Total Cost] FROM MyTable;
Then using Query1:
Code:
SELECT T1.[ORG STATE], T1.[DEST PROV], T1.[Total Cost],  
       (SELECT COUNT(T1.[ORG STATE])
          FROM
                 [Query1] T2
         WHERE 
			T2.[ORG STATE] = T1.[ORG STATE]
			AND T2.[DEST PROV] = T1.[DEST PROV]
			AND T2.[Total Cost] <= T1.[Total Cost]) AS Rank
FROM 
	[Query1] T1
ORDER BY [ORG STATE], [DEST PROV], [Total Cost]
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,990
Messages
6,122,625
Members
449,093
Latest member
catterz66

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