Solved

Forcing 0s in Access SQL

Posted on 2011-02-17
5
292 Views
Last Modified: 2012-05-11
Hi.  I have a mini- app written in Access (VBA) in which I loop through about 15 queries and write the results to an Excel template.  The template reads the raw output on a back tab and displays it on visible tabs in a presentable type way through cell references to the data tab.

My issue is this:

In a query similar to this

SELECT
“1” AS Sort,
"HC " AS [Fmt],
Sum(Iif([Trim Code] IN ('A'),(Actl_MR_Bind+Actl_MR_Mfg_Print),0))  AS dPa,
Sum(Iif([Trim Code] IN ('B'),(Actl_MR_Bind+Actl_MR_Mfg_Print),0))  AS dPb,
Sum(Iif([Trim Code] NOT IN ('A','B'),(Actl_MR_Bind+Actl_MR_Mfg_Print),0))  AS dPo
FROM HDB
WHERE …

UNION SELECT
“2” AS Sort,
"TR " AS [Fmt],
Sum(Iif([Trim Code] IN ('A'),(Actl_Run_Bind+Actl_Run_Mfg_Print),0))  AS dPa,
Sum(Iif([Trim Code] IN ('B'),(Actl_Run_Bind+Actl_Run_Mfg_Print) ,0))  AS dPb,
Sum(Iif([Trim Code] NOT IN ('A','B'),(Actl_Run_Bind+Actl_Run_Mfg_Print),0))  AS dPo
FROM HDB
WHERE …

UNION SELECT
“3” AS Sort,
"MM " AS [Fmt],
Sum(Iif([Trim Code] IN ('A'),(Actl_CtnPal_Bind),0))  AS dPa,
Sum(Iif([Trim Code] IN ('B'),(Actl_CtnPal_Bind) ,0))  AS dPb,
Sum(Iif([Trim Code] NOT IN ('A','B'),(Actl_CtnPal_Bind),0))  AS dPo
FROM HDB
WHERE …

When run  it for a vendor who produces HC (1) and MM (3) but no TR (2) the results write out as:
1  HC    100   200  100
3  MM   200   100  100

And if a vendor has no data, the query results do not write out to the spreadsheet at all and writes the next query over its intended destination cells.

So, my question is:  Is there a way to instead output the query results like this:

1  HC    100   200  100
2  TR      0        0       0
3  MM   200   100  100

Or do I need to find a better way to do this all around?

Many thanks.
0
Comment
Question by:bishopkd
  • 3
  • 2
5 Comments
 
LVL 40

Expert Comment

by:als315
ID: 34916775
Yes!
Add query (or table) with all products you like to see (HC,TR,MM) and join it in query with your union query (All from created query/table and only existed from union query)
0
 

Author Comment

by:bishopkd
ID: 34917173
I love the simplicity of this.  I'm not getting it to work though.  What am I doing wrong?

SELECT
tblFMT.format,
"HC-disP" AS [Fmt],
Sum(Iif([Trim Code] IN ('A'),(Actl_Plt_Bind+Actl_Plt_Mfg_Print) * ([Enter HCPa]),0))  AS dPa,
Sum(Iif([Trim Code] IN ('B'),(Actl_Plt_Bind+Actl_Plt_Mfg_Print) * ([Enter HCPb]),0))  AS dPb,
Sum(Iif([Trim Code] NOT IN ('A','B'),(Actl_Plt_Bind+Actl_Plt_Mfg_Print) * ([Enter HCPo]),0))  AS dPo
FROM HDB RIGHT JOIN tblFMT
ON HDB.fmt = tblFMT.format
WHERE Year([ProdOrdr Del Date]) = [Enter Year] And
Month([ProdOrdr Del Date]) BETWEEN ([Enter SMonth]) and ([Enter EMonth]) And
HDB.Binder In ([Enter Binder]) AND
HDB.[Contract Narrow Web] In ('Y') AND
HDB.fmt in ('HC')
GROUP BY tblFMT.format,HDB.fmt
ORDER by [fmt]

UNION

SELECT
tblFMT.format,
"TR-disP" AS [Fmt],
Sum(Iif([Trim Code] IN ('A'),(Actl_Plt_Bind+Actl_Plt_Mfg_Print) * ([Enter TRPa]),0))  AS dPa,
Sum(Iif([Trim Code] IN ('B'),(Actl_Plt_Bind+Actl_Plt_Mfg_Print) * ([Enter TRPb]),0))  AS dPb,
Sum(Iif([Trim Code] NOT IN ('A','B'),(Actl_Plt_Bind+Actl_Plt_Mfg_Print) * ([Enter TRPo]),0))  AS dPo
FROM HDB RIGHT JOIN tblFMT
ON HDB.fmt = tblFMT.format
WHERE Year([ProdOrdr Del Date]) = [Enter Year] And
Month([ProdOrdr Del Date]) BETWEEN ([Enter SMonth]) and ([Enter EMonth]) And
HDB.Binder In ([Enter Binder]) AND
HDB.[Contract Narrow Web] In ('Y') AND
HDB.fmt in ('TR')
GROUP BY tblFMT.format,HDB.fmt
ORDER by [fmt]
0
 
LVL 40

Expert Comment

by:als315
ID: 34917395
Resulting query - qry1, table qry - your union query, products - list of all products
DB26828504.mdb
0
 

Author Comment

by:bishopkd
ID: 34918088
This works when I do not have a WHERE Statement.
If the Qry table had a Printer field and your SQL had to look like this:

SELECT
Qry.Product,
nz(sum([Qry]![F1]),0) AS F1,
nz(Sum([Qry]![F2]),0) AS F2,
nz(Sum([Qry]![F3]),0) AS F3
FROM Products LEFT JOIN Qry ON Products.Product = Qry.Product
WHERE Products.Printer IN ('OPM')
Group by Qry.Product;

It no longer pulls in the TR with 0s...
0
 
LVL 40

Accepted Solution

by:
als315 earned 125 total points
ID: 34918495
All where should be in separate query. Create additional query for Products
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

User Beware!  This is a rather permanent solution to removing your email from an exchange server.  The only way to truly go back is to have your exchange administrator restore your mailbox from backups.  This is usually the option of last resort.  A…
This collection of functions covers all the normal rounding methods of just about any numeric value.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …

838 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question