Custom Search
 




Inside This Article
1IIF function basics - the CASE statement of MS Access
2Use MS Access IIF function in GROUP BY clause
3Use MS Access IIF function in ORDER BY clause
   
4. A complex example of MS Access IIF function used within GROUP BY clause
5Use MS Access IIF function to conditionally calculate field values
6Use MS Access IIF function with NZ function to convert NULL values to numerics

A complex example of MS Access IIF function used within GROUP BY clause



This tutorial demonstrates a complex example of using IIF function with GROUP BY clause.

What do we want

In Suppliers table, we want to group suppliers into three continents: Asia-Pacific, America, and Europe.
Then, for each product category, we calculate the number of units in stock for each continent.

  • Asia-Pacific: Singapore, Japan, Australia
  • America: USA, Canada, Brazil
  • Europe: UK, Spain, Sweden, Germany, Norway, Denmark, Netherlands, Finland, Italy, France
Query Name: FuncTest_Grouping_Complex1
Code Logic: The first IIF checks if the country is a European country. The second IIF checks if the country is an American country. Everything else will automatically become Asia-Pacific countries (No IIF is needed for it).

In order to group the result by continents, we need to select GROUP BY in the Design View. If you prefer to deal with SQL directly, switch to SQL View and copy the entire IIF statement and paste into the GROUP BY clause but leave out the field alias - AS [Supplier Continent].

To calculate total units in stock, use SUM function.
SQL View:
 
SELECT Categories.CategoryName AS [Product Category],
       IIf([suppliers].[Country]='UK' 
           Or [suppliers].[Country]='Spain' 
           Or [suppliers].[Country]='Sweden' 
           Or [suppliers].[Country]='Germany' 
           Or [suppliers].[Country]='Norway' 
           Or [suppliers].[Country]='Denmark' 
           Or [suppliers].[Country]='Netherlands' 
           Or [suppliers].[Country]='Finland' 
           Or [suppliers].[Country]='Italy' 
           Or [suppliers].[Country]='France', 'Europe',
       IIf([suppliers].[country]='USA' 
           Or [suppliers].[Country]='Canada' 
           Or [suppliers].[Country]='Brazil', 'America', 'Asia-Pacific')) 
           AS [Supplier Continent], 
       Sum(Products.UnitsInStock) AS UnitsInStock
FROM Suppliers INNER JOIN 
     (Categories INNER JOIN Products 
     ON Categories.CategoryID = Products.CategoryID) 
     ON Suppliers.SupplierID = Products.SupplierID
GROUP BY Categories.CategoryName, 
       IIf([suppliers].[Country]='UK' 
           Or [suppliers].[Country]='Spain' 
           Or [suppliers].[Country]='Sweden' 
           Or [suppliers].[Country]='Germany' 
           Or [suppliers].[Country]='Norway' 
           Or [suppliers].[Country]='Denmark' 
           Or [suppliers].[Country]='Netherlands' 
           Or [suppliers].[Country]='Finland' 
           Or [suppliers].[Country]='Italy' 
           Or [suppliers].[Country]='France', 'Europe',
       IIf([suppliers].[country]='USA' 
           Or [suppliers].[Country]='Canada' 
           Or [suppliers].[Country]='Brazil', 'America', 'Asia-Pacific'));
Datasheet View: 21 records returned:

Simplify the SQL

When you look at the SQL presented above, you may realize that you can simplify it to achieve the same result. This can be done by using IN operator.

Query name: Func_Grouping_Complex2

SELECT Categories.CategoryName AS [Product Category], 
       IIf([suppliers].[Country] IN (
           'UK', 
           'Spain', 
           'Sweden', 
           'Germany', 
           'Norway', 
           'Denmark', 
           'Netherlands', 
           'Finland', 
           'Italy', 
           'France'), 'Europe',
       IIf([suppliers].[country] IN (
           'USA', 
           'Canada', 
           'Brazil'), 'America', 'Asia-Pacific')) 
           AS [Supplier Continent], 
       Sum(Products.UnitsInStock) AS UnitsInStock
FROM Suppliers INNER JOIN 
     (Categories INNER JOIN Products 
     ON Categories.CategoryID = Products.CategoryID) 
     ON Suppliers.SupplierID = Products.SupplierID
GROUP BY Categories.CategoryName, 
       IIf([suppliers].[Country] IN (
           'UK', 
           'Spain', 
           'Sweden', 
           'Germany', 
           'Norway', 
           'Denmark', 
           'Netherlands', 
           'Finland', 
           'Italy', 
           'France'), 'Europe',
       IIf([suppliers].[country] IN (
           'USA', 
           'Canada', 
           'Brazil'), 'America', 'Asia-Pacific'));

Food for thoughts

To build a true relational database, we may want to build a Continents table that contains data for all the continents. In Suppliers table, add a new column ContinentID which is the foreign key pointing back to the Continents table.

Building the Continents table serves many purposes. One of them is to define a data source that can be referred at any time without hard-coding the continent names in your source code such as SQL or VBA.

In our example above, we can join Continents with Suppliers table and write a SELECT statement to replace the code inside the IN operator. This is called sub-query.

It is also a good idea to separate out all countries into its own table. In systems analysis, this is a normalization process. Sometimes you may need to de-normalize tables to speed up query response time, but this is outside the scope of this tutorial.

What's next

The query result showing in Datasheet View has listed Supplier's Continent in the second, third, and fourth column. It's nice for presentation purpose that we can list each continent in its own column.

Category Name Europe America Asia-Pacific
Beverages 324 203 32
Condiments 45 372 90
Confections 340 17 29
Dairy Products 393 0 0
Grains/Cereals 244 0 64
Meat/Poultry 0 136 29
Produce 26 15 59
Seafood 396 208 97

This can be easily achieved if we copy the query result into Excel spreadsheet and create PivotTable for data analysis. But to work with Access, we need to tweak our query to achieve the desired result. This technique is demonstrated in next tutorial Part 5 of IIF function.


Copyright© GeeksEngine.com




Inside This Article
1IIF function basics - the CASE statement of MS Access
2Use MS Access IIF function in GROUP BY clause
3Use MS Access IIF function in ORDER BY clause
   
4. A complex example of MS Access IIF function used within GROUP BY clause
5Use MS Access IIF function to conditionally calculate field values
6Use MS Access IIF function with NZ function to convert NULL values to numerics
Related Articles:

1.IIF function basics - the CASE statement of MS Access


Other Recent Articles from the MS Access category:

1.Examples of MS Access DateDiff function used in query and VBA code
2.MS Access DateDiff function
3.How to find out your computer name and username by VBA
4.Examples of MS Access DatePart function
5.MS Access DatePart function
6.Examples of MS Access DateAdd function
7.MS Access DateAdd function
8.IIF function basics - the CASE statement of MS Access
9.MS Access Date Expression
10.Solved: MS Access error "The text is too long to be edited"
11.Create MS Access Combo Box essential properties by VBA code
12.Create MS Access Combo Box essential properties manually
13.How to do text search in MS Access programmatically
14.Solved - the size of the Access query result is larger than the maximum size of a database (2 GB)
15.How to easily get a list of field names in MS Access
16.How to count distinct records in MS Access
17.How to do transaction based processing in MS Access
18.How to open a document (local/network file or web page) from MS Access
19.How to use ADOX to create unique composite index - the VBA approach
20.How to do cross-table update queries in MS Access - the right way
21.Three efficient ways to get the number of records by using VBA
22.How to create a composite unique index (not as a primary key) in MS Access
23.Use VBA to get the correct number of records in a Recordset object
24.Disable Access Prompt when a record is changed, table deleted, or action queries run
25.How to hide and unhide a MS Access object
26.How to return multiple values from a VBA function (Part 3)
27.How to return multiple values from a VBA function (Part 2)
28.How to return multiple values from a VBA function (Part 1)
29.Three ways to programmatically duplicate a table in MS Access by VBA
30.Create a DLL by CSharp or VB.Net for VBA
31.How to correctly reference and call a DLL
32.How to register a C# or VB.Net DLL
33.Email address validation by Regular Expressions using VBA
34.Fix MS Access error: Query must have at least one destination field
35.How to unselect radio buttons in MS Access after it has been selected
36.How to Change Query Timeout Value for MS Access SQL Queries
37.What is Northwind Traders database

Copyright © 2024 GeeksEngine.com. All Rights Reserved.

This website is hosted by HostGator.

No portion may be reproduced without my written permission. Software and hardware names mentioned on this site are registered trademarks of their respective companies. Should any right be infringed, it is totally unintentional. Drop me an email and I will promptly and gladly rectify it.

 
Home | Feedback | Terms of Use | Privacy Policy