Deprecated: Creation of dynamic property CF\WordPress\DataStore::$logger is deprecated in /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-content/plugins/cloudflare/src/WordPress/DataStore.php on line 23

Deprecated: Creation of dynamic property CF\WordPress\Proxy::$pluginAPI is deprecated in /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-content/plugins/cloudflare/src/WordPress/Proxy.php on line 31

Deprecated: file_get_contents(): Passing null to parameter #2 ($use_include_path) of type bool is deprecated in /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-content/plugins/codelights-shortcodes-and-widgets/codelights.php on line 20

Warning: session_start(): Session cannot be started after headers have already been sent in /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-content/plugins/automatic-video-posts/vendor/ternstyle/wordpress/src/class/wordpress.php on line 53

Deprecated: Return type of Requests_Cookie_Jar::offsetExists($key) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-includes/Requests/Cookie/Jar.php on line 63

Deprecated: Return type of Requests_Cookie_Jar::offsetGet($key) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-includes/Requests/Cookie/Jar.php on line 73

Deprecated: Return type of Requests_Cookie_Jar::offsetSet($key, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-includes/Requests/Cookie/Jar.php on line 89

Deprecated: Return type of Requests_Cookie_Jar::offsetUnset($key) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-includes/Requests/Cookie/Jar.php on line 102

Deprecated: Return type of Requests_Cookie_Jar::getIterator() should either be compatible with IteratorAggregate::getIterator(): Traversable, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-includes/Requests/Cookie/Jar.php on line 111

Deprecated: Return type of Requests_Utility_CaseInsensitiveDictionary::offsetExists($key) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-includes/Requests/Utility/CaseInsensitiveDictionary.php on line 40

Deprecated: Return type of Requests_Utility_CaseInsensitiveDictionary::offsetGet($key) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-includes/Requests/Utility/CaseInsensitiveDictionary.php on line 51

Deprecated: Return type of Requests_Utility_CaseInsensitiveDictionary::offsetSet($key, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-includes/Requests/Utility/CaseInsensitiveDictionary.php on line 68

Deprecated: Return type of Requests_Utility_CaseInsensitiveDictionary::offsetUnset($key) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-includes/Requests/Utility/CaseInsensitiveDictionary.php on line 82

Deprecated: Return type of Requests_Utility_CaseInsensitiveDictionary::getIterator() should either be compatible with IteratorAggregate::getIterator(): Traversable, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-includes/Requests/Utility/CaseInsensitiveDictionary.php on line 91

Warning: Cannot modify header information - headers already sent by (output started at /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-content/plugins/cloudflare/src/WordPress/DataStore.php:23) in /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-includes/rest-api/class-wp-rest-server.php on line 1758

Warning: Cannot modify header information - headers already sent by (output started at /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-content/plugins/cloudflare/src/WordPress/DataStore.php:23) in /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-includes/rest-api/class-wp-rest-server.php on line 1758

Warning: Cannot modify header information - headers already sent by (output started at /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-content/plugins/cloudflare/src/WordPress/DataStore.php:23) in /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-includes/rest-api/class-wp-rest-server.php on line 1758

Warning: Cannot modify header information - headers already sent by (output started at /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-content/plugins/cloudflare/src/WordPress/DataStore.php:23) in /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-includes/rest-api/class-wp-rest-server.php on line 1758

Warning: Cannot modify header information - headers already sent by (output started at /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-content/plugins/cloudflare/src/WordPress/DataStore.php:23) in /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-includes/rest-api/class-wp-rest-server.php on line 1758

Warning: Cannot modify header information - headers already sent by (output started at /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-content/plugins/cloudflare/src/WordPress/DataStore.php:23) in /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-includes/rest-api/class-wp-rest-server.php on line 1758

Deprecated: Creation of dynamic property ternplugin\youtube_video::$post is deprecated in /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-content/plugins/automatic-video-posts/vendor/ternstyle/plugin/src/class/youtube_video.php on line 64

Warning: Cannot modify header information - headers already sent by (output started at /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-content/plugins/cloudflare/src/WordPress/DataStore.php:23) in /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-includes/rest-api/class-wp-rest-server.php on line 1758

Warning: Cannot modify header information - headers already sent by (output started at /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-content/plugins/cloudflare/src/WordPress/DataStore.php:23) in /home1/cassanoc/public_html/bonbonsguide.com/wp/wp-includes/rest-api/class-wp-rest-server.php on line 1758
{"id":2129,"date":"2020-08-26T12:34:56","date_gmt":"2020-08-26T19:34:56","guid":{"rendered":"http:\/\/bonbonsguide.com\/wp\/?p=2129"},"modified":"2020-08-26T15:19:51","modified_gmt":"2020-08-26T22:19:51","slug":"rank-in-access-query-sales-item-ranking","status":"publish","type":"post","link":"https:\/\/bonbonsguide.com\/wp\/blog\/2020\/08\/rank-in-access-query-sales-item-ranking\/","title":{"rendered":"Rank in Access Query (Sales Ranking)"},"content":{"rendered":"\n

A common request I get is for ranking of sales in Access by region, state and item combination for analysis and space management.  In my business, we have some overlap, where a state can be part of two separate areas or regions, for example Southern CA vs. Northern CA.  It’s helpful for us to show separate rankings for each region and state combo, as the results often tell a different story for each.<\/p>\n\n\n\n

The Pass-Through<\/strong> query below enables me to determine the ranking at each of these levels.  By entering as a Pass-Through query, you can utilize the Rank and Partition SQL commands.  After running this query, I bring the data into Excel for further analysis, but this query is a good starting point for any similar analyses.<\/p>\n\n\n\n

Here is the full Access pass-through query<\/strong>, color coded for the explanation below:<\/p>\n\n\n\n

Select Region, State, Product_ID, SumSaleQ, StoreCount,Rank<\/span>
FROM<\/span> (Select Region, State, Product_ID, SumSaleQ, StoreCount, Rank() over (Partition By state, Region Order by SumSaleQ DESC)as rank<\/span>
FROM<\/span> (Select s.Region, s.state, m.Product_ID, sum(m.sale_quantity) as SumSaleQ, count(distinct(m.site_num)) as StoreCount<\/span>
FROM Store_monthly_sales m, FR_Site_Master s<\/span>
WHERE m.Site_num = s.Site_num AND m.accrual_month in (‘202007′,’202006′,’202005’)<\/span>
GROUP BY s.Region,m.Product_ID,s.state)<\/span>
WHERE SumSaleQ >5)<\/span>
WHERE rank <205<\/span>
Order by State ASC, Region Asc, Rank Asc<\/span><\/p>\n\n\n\n

Explanation<\/strong><\/h3>\n\n\n\n

Query 1<\/span><\/p>\n\n\n\n

Diving right in, since we are measuring at multiple levels, we have some nested queries.  Working from the inside out, my first nested query is this one:<\/p>\n\n\n\n

(Select s.Region, s.state, m.Product_ID, sum(m.sale_quantity) as SumSaleQ, count(distinct(m.site_num)) as StoreCount<\/span>
FROM Franchise.Store_monthly_sales m,Franchise.FR_Site_Master s<\/span>
WHERE m.Site_num = s.Site_num AND accrual_month in (‘202007′,’202006′,’202005’)<\/span>
GROUP BY s.Region,m.Product_ID,s.state)<\/span><\/p>\n\n\n\n

Query 2<\/span><\/p>\n\n\n\n

We then nest that within this query (the WHERE clause is optional – I am dealing with sales in the thousands, and want to exclude items with a sales quantity less than 5.  Note the Rank() over and Partition By Commands here:<\/p>\n\n\n\n

(Select Region, State, Product_ID, SumSaleQ, StoreCount, Rank() over (Partition By state, Region Order by SumSaleQ DESC)as rank<\/span><\/p>\n\n\n\n

FROM <\/span>…<\/p>\n\n\n\n

WHERE SumSaleQ >5)<\/em><\/span><\/p>\n\n\n\n

Query 3<\/span><\/p>\n\n\n\n

The final nesting and main query is selecting the rank from the second query, along with the other pertinent attributes.  The WHERE clause is again optional, I am looking to limit my results to the top 200 results only for each region and state combination. I use 205 as the figure to allow for ties.  The Order by is also optional.  I like to order the results by state, region and then rank ascending.<\/p>\n\n\n\n

Select Region, State, Product_ID, SumSaleQ, StoreCount,Rank<\/span><\/p>\n\n\n\n

FROM …<\/span><\/p>\n\n\n\n

WHERE rank <205<\/span>
Order by State ASC, Region Asc, Rank Asc<\/span><\/p>\n\n\n\n

Full Query (no color coding)<\/span><\/h3>\n\n\n\n

Select Region, State, Product_ID, SumSaleQ, StoreCount,Rank<\/span>
FROM (Select Region, State, Product_ID, SumSaleQ, StoreCount, Rank() over (Partition By state, Region Order by SumSaleQ DESC)as rank<\/span>
FROM (Select s.Region, s.state, m.Product_ID, sum(m.sale_quantity) as SumSaleQ, count(distinct(m.site_num)) as StoreCount<\/span>
FROM Store_monthly_sales m, FR_Site_Master s<\/span>
WHERE m.Site_num = s.Site_num AND m.accrual_month in (‘202007′,’202006′,’202005’)<\/span>
GROUP BY s.Region,m.Product_ID,s.state)<\/span>
WHERE SumSaleQ >5)<\/span>
WHERE rank <205<\/span>
Order by State ASC, Region Asc, Rank Asc<\/span><\/p>\n\n\n\n

Conclusion<\/h3>\n\n\n\n

I hope this post will help you in your efforts to rank in Access<\/strong>.  It can be a bit of a challenge, but hopefully the breakdown of the steps here will help you achieve your ranking goals.  Please comment any questions, feedback, or what you would like to see next!  Share this post if you feel it would help someone else! \ud83d\ude42<\/p>\n\n\n\n

If I helped you, please consider buying me a coffee via PayPal! Thanks!!\n<\/b><\/p>\n\n\n\n

\n
\n\n\n\n\n\n\n\n
\"\"<\/figure>\n<\/div>\n<\/b><\/form>\n","protected":false},"excerpt":{"rendered":"

A common request I get is for ranking of sales in Access by region, state and item combination for analysis and space management.  In my business, we have some overlap, where a state can be part of two separate areas or regions, for example Southern CA vs. Northern CA.  It’s helpful for us to show […]<\/p>\n","protected":false},"author":1,"featured_media":917,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":""},"categories":[29,101,140,102],"tags":[196,195,194,193,192,191],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/bonbonsguide.com\/wp\/wp-content\/uploads\/2018\/11\/digital-marketing-1433427_1920.jpg?fit=1920%2C1281&ssl=1","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p7QB1z-yl","_links":{"self":[{"href":"https:\/\/bonbonsguide.com\/wp\/wp-json\/wp\/v2\/posts\/2129"}],"collection":[{"href":"https:\/\/bonbonsguide.com\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bonbonsguide.com\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bonbonsguide.com\/wp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bonbonsguide.com\/wp\/wp-json\/wp\/v2\/comments?post=2129"}],"version-history":[{"count":9,"href":"https:\/\/bonbonsguide.com\/wp\/wp-json\/wp\/v2\/posts\/2129\/revisions"}],"predecessor-version":[{"id":2164,"href":"https:\/\/bonbonsguide.com\/wp\/wp-json\/wp\/v2\/posts\/2129\/revisions\/2164"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/bonbonsguide.com\/wp\/wp-json\/wp\/v2\/media\/917"}],"wp:attachment":[{"href":"https:\/\/bonbonsguide.com\/wp\/wp-json\/wp\/v2\/media?parent=2129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bonbonsguide.com\/wp\/wp-json\/wp\/v2\/categories?post=2129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bonbonsguide.com\/wp\/wp-json\/wp\/v2\/tags?post=2129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}