Solved

Forcing 0s in Access SQL

Posted on 2011-02-17
5
288 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 39

Expert Comment

by:als315
Comment Utility
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
Comment Utility
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 39

Expert Comment

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

Author Comment

by:bishopkd
Comment Utility
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 39

Accepted Solution

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

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
The new Microsoft OS looks great, is easier than ever to upgrade to, it is even free.  So what's the catch?  If you don't change the privacy settings, Microsoft will, in accordance with the (EULA) you clicked okay to without reading, collect all the…
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 viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now