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
   
4A complex example of MS Access IIF function used within GROUP BY clause
5. Use MS Access IIF function to conditionally calculate field values
6Use MS Access IIF function with NZ function to convert NULL values to numerics

Use MS Access IIF function to conditionally calculate field values



In Part 4, we looked at how to use IIF function with GROUP BY clause. The format of query result in Part 4 is not very eye friendly. The query only groups the UnitsInStock totals vertically by Continent. This results in more records, making comparisons of UnitInStock totals between different categories and continent more difficult.

What do we want

In Suppliers table, we want to calculate the number of units in stock for each product category and then list the result by continents horizontally. This is what Access crosstab query type is for.

Countries and continents

  • Asia-Pacific: Singapore, Japan, Australia
  • America: USA, Canada, Brazil
  • Europe: UK, Spain, Sweden, Germany, Norway, Denmark, Netherlands, Finland, Italy, France

The data result format showing here is PivotTable format. Access crosstab query type is used for the same purpose - making query result set easier to read when comparing data across columns.

Query Name: FuncTest_Grouping_Complex3
Code Logic: To get our desired result format, we wrap IIF function inside SUM function.

The first SUM will add up the UnitsInStock for each country that belongs to Europe by using IIF function inside the SUM function. The second SUM applies the same logic for America. The last SUM does it against Asia-Pacific countries.

In GROUP BY, we specify product category so that only the same category is grouped.

SQL View:
 
SELECT Categories.CategoryName, 
       Sum(IIf([suppliers].[Country] IN (
           'UK', 
           'Spain', 
           'Sweden', 
           'Germany', 
           'Norway', 
           'Denmark', 
           'Netherlands', 
           'Finland', 
           'Italy', 
           'France'), [products].[unitsinstock])) AS Europe, 
        Sum(IIf([suppliers].[country] IN (
           'USA', 
           'Canada', 
           'Brazil'), [products].[unitsinstock])) AS America, 
        Sum(IIf([suppliers].[country] IN (
           'Japan', 
           'Australia', 
           'Singapore'),[products].[unitsinstock])) AS [Asia-Pacific]
FROM Suppliers INNER JOIN 
     (Categories INNER JOIN Products 
     ON Categories.CategoryID = Products.CategoryID) 
     ON Suppliers.SupplierID = Products.SupplierID
GROUP BY Categories.CategoryName;
Datasheet View: 8 records returned:

In Part 6 of IIF tutorial we'll fine-tune the query by using NZ function to eliminate NULL values which show blanks in the result set above.


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
   
4A complex example of MS Access IIF function used within GROUP BY clause
5. Use 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