111+ Power Access Query/SQL Examples
[Go To Part 2]
Part 1 | Database A (PowerQueries.mdb) contains 91 power queries
1. Have you ever imagined using a parameter containing multiple default values in a where clause or a having clause or even an order by clause? This special type of parameter can handle multiple parameter values at a time. You can use the default values as provided or enter your own values. You also can change the default values to meet your changing needs. You will find our five query examples demonstrating this technique fascinating.


This example demonstrates how you can use this one of a kind parameter in the HAVING clause of a GROUP BY query. We provide three other examples showing how you can use this parameter in a WHERE or an ORDER BY clause.
_______________
2. Create a calendar using an Access query?
This may sound like a tall order to fill. However, with several built-in Access functions and a Union query structure, you can pull off what seems like an impossible solution. This parameterized Union query takes two parameters: a month number and a year. If you provide these two parameters, you can create any monthly calendar. Discover our fascinating
tricks and techniques.



_______________
3. You may find it hard to believe that you can create a calendar for a whole year with an Access query. Even with VBA, this could be a tall order. However, we pulled off what seems to be an impossible task without using a single line of VBA or Macros by working a Union query to the maximum. With this query you can create a calendar for an entire year and display it in a single datasheet view.
Parameter: year(yyyy)
.GIF)
.GIF)
_______________
4. Make a calendar for multiple years
You even can create a calendar for whatever years you want in one shot.
Just enter those years in the table provided, which serves as a multiple value parameter.



_______________
5. Let's say you need to change all the manager titles in the contact title field of your Customers table in the Northwind database. There are three different titles containing "Manager": Accounting Manager, Marketing Manager, Sales Manager. You want to change "Manager" into "Executive" for all three titles. To accomplish this, normally you need to run an update query three times. However, you can combine all three queries into one parameterized action query and achieve your goal in one shot. An example query demonstrates how you can update partial field values in one step.



As you can see from this example, the three contact titles containing the word “Manager”
now have “Executive” in its place.
_______________
6. Do you need to remove all numeric or non-numeric characters from string values in a column? For example, you have a part number in your column: A1B2C345. You want to end up with ABC by removing all the numeric characters. Or you want 12345 by removing all the non-numeric characters. This can normally be done using a user-defined function (VBA code). Two example update queries demonstrate a technique to accomplish this task without using a single line of code.
_______________
7. You want to get a list of your products whose unit price is equal to or above the average price with the average price shown at the end of the list labeled "Average". You also want the list sorted in an ascending order by product name. This task is easier said than done. But if you use a little ingenuity, you can come up with an amazingly simple solution. An example query provided gets you exactly what you want
in a single step i.e., no nested query.

Notice the average price at the bottom of the list.
_______________
8. How do you rank items within each category? For example, you may want
to get top three most expensive products by category.

This query uses no nested queries. Look at the two categories: Produce, Seafood. Notice how the query ranks the same prices within the same category.
_______________
9. How do you create a spreadsheet type of list out of an Access table?

_______________
10. Do you want to make a list of order numbers by customer with each row showing all the order numbers for each customer? We used a derived table and the IIF function in building this unique query.
[click on image to enlarge]

______________
11. Do you know that you can put a certain group of records at the top or bottom of your query results with the rest of them sorted separately?
For example, you may want to show your fast selling products at the top of the list. This custom sort technique involves sorting on a single or multiple expressions. You even can sort your whole list in accordance with a custom list. A dozen query examples demonstrate this technique:
1) Show 1997 orders at top
2) Show average price at bottom
3) Show customers in London at top
4) Show customers who have same address and shipping address
5) Show managers first
6) Show marketing managers & owners at top
7) Show products with IDs 50-60 at top
8) Show selected companies at top
9) Sort on an expression based on the Switch function
10) Sort on custom list
11) Sort on multiple expressions
______________
12. Do you want to get a list of your products with category average price next to unit price for each product?

The database contains two examples taking two different approaches to solve this problem.
______________
13. How about getting running sum within each group?

_______________
14. Do you want to find out how many products are priced below or above the average price and what the respective average price for these two groups is?

_______________
15. Do you want to find out how often your customers order?

Notice that at the bottom of the list there is a row total across the columns.
_______________
16. You may wonder what products are priced so high that they are 2+ times as expensive as average products within each category.

_______________
17. You sometimes want to assign sequential numbers to the results of your query.

The database contains two examples taking two different approaches to solve this problem.
_______________
18. Do you want to find out what products are priced the same within each product category?

_______________
19. Do you need to count your products by groups of price range?

_______________
20. What customers are located in the same city?

_______________
21. Have you ever tried a SQL like this: SELECT COUNT (DISTINCT ContactTitle) FROM Customers? You are out of luck. MS Access does not support this type of query. However,
there is a workaround. With a little bit of ingenuity you can come up with a single query.

_______________
22. Create random multi-year dates (parameterized)
This select query comes in handy when you are in a hurry to create 100 test dates. This query accepts three parameters (years). You can add or delete your parameters as you wish. If you need more or less than 100 records, then you can modify the underlying single column table by adding or deleting records accordingly.

_______________
23. Create random single-year dates (parameterized)
This make-table query comes in handy when you are in a hurry to create 100 test dates. This query takes one parameter. If you need more or less than 100 records, then modify the underlying single column table by adding or deleting records accordingly.
_______________
24. Group customers by total sales and count them by group.
This query employs a derived table (based on Orders table and Order Details table) and the IIF function. This powerful combination enables you to pull off what would otherwise be impossible to accomplish in a single step.

_______________
25. When is the last time a customer bought and how much is it?
You may find this information useful if you want to analyze your recent marketing efforts.

_______________
26. Transpose columns to rows.
You may want to convert a table like this:
to a table like this:

_______________
27. Transpose rows to columns.


_______________
28. Average unit price excluding top three and bottom three products
This query uses a derived table, a left join, and Union All to get the average
price of the products from the Products table without the three lowest-priced and the three highest-priced products. You need to be very creative to construct a query like this in a single step!
_______________
29. Most expensive products by category

_______________
30. A list of products whose price is at least two times the average for the category.
This query uses a correlated subquery in the Having clause.

_______________
31. Double unit prices for top three selling products by category
This make table query is based on this select query:

_______________
32. Nth most expensive products by category
This query takes a parameter where you provide a number (Nth).
_______________
33. Customers who have never ordered

______________
34. Customers who ordered above average quantity by product
This single query uses a self-join.

______________
35. Latest order by customer

______________
36. Who ordered last in each month in 1997

______________
37. Which customers are from five countries with fewest orders?

______________
38. Count total records in multiple tables in a single query.
This query demonstrates a technique to count the numbers of rows
in multiple tables.
______________
39. Get maximum and average value across columns
This query shows you how to get the maximum and average across
the columns from a table.


______________
40. Create a test table using Union All in a single query
This query creates a table shown below.

______________
41. Top customers by country in 1998
If you compare the two queries, you will see that the first
query picked up only the top customer for each country.


______________
42. Last orders in each month

______________
43. Customers who ordered above average quantity by product

______________
44. Five Most Recent Shippments by Company

______________
45. Which customers are from five countries with fewest orders?

______________
46. Select every other rows.

______________
47. How many orders each employee took from each country?

______________
48. Largest order for each product in terms of quantity ordered

______________
49. Products purchased by customers

______________
50. Products excluding top three and bottom three
______________
51. Top salesperson for each country
This query finds who is the best salesperson for each country
based the number of orders taken for the country.

______________
52.Who is the best salesperson?
This query select the best salesperson among the top
salespersons shown above.

______________
53. Three top orders ever placed for each product

______________
54. Three top selling products by category in terms of quantity?

______________
55. What is the biggest order by product in terms of quantity?

______________
56. Who ordered the most products?

______________
57. Combine two tables to make a single table in a single step
This make-table query combines in a single step the CompanyName column from two tables: Customers and Suppliers.
______________
58. Who never ordered three products: 1,7,10 (product IDs)?

______________
59. Find average of distinct unit prices in a single step

______________
60. How many items of each product were sold?

______________
61. Which suppliers we have not done any business with?
Notice that we added a dummy record to the underlying tables to demonstrate our technique.
______________
62. Who ordered more than once on the same day?

______________
63. Get employee's age as of today
______________
64. Product variance report
This query shows in a single step how to get the difference between
the average price and the price of each product. This query employs
a derived table and No-Join.
.gif)
______________
65. Quarterly order count by customer for 1997 orders
These two queries show two different approaches to solving the same problem.


______________
66. Best customers by country in 1998
The second query lists all customers and their purchase amounts.
The first query picks the very top customer for each country.


______________
67. Select N records randomly
You can select five (or any number of) records from your table every time you open this query
or by holding down the SHIFT key and pressing the F9 key in the query’s DATASHEET view.

______________
68. Quarterly order amount by year


These two queries look alike. However the top query is a typical cross tab query and
the bottom one is a select query to demonstrate how to emulate a cross tab query
using Access’ built functions.
______________
69. A CrossTab query example

______________
70. Who never ordered any products?

We provide two examples to demonstrate two different approaches to
solving the same problem.
71. How do you assign sequential numbers to rows in a table without an identity column?

(includes both Part 1 and Part 2)
[Go To Part 2]
We offer an unconditional 30-day Money Back Gurantee and free e-mail support! Your order is secured via RegNow SSL Secure Server (see RegNow Security/Privacy statement). You will be able to download the product immediately after ordering!
|