473,385 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Default value in a query

Hi,

I'm working with 3 tables to do a calculation and it's giving me trouble.
Example of the data:
Table A
Machine1
Machine2
Machine3

Table B
Machine1, 2 (RHours)
Machine3, 4 (RHours)

Table C
Machine2, 1 (PHours)
Machine3, 6 (PHours)

Query:
SELECT A.Machine, [b].[RHours], [C].[PHours], [b].[RHours]/([C].[PHours]+[b].[RHours]) AS [Ratio]
FROM (A LEFT JOIN B ON A.Machine = B.Machine) LEFT JOIN C ON A.Machine = C.Machine;

When I run the query I get:

Machine1, 2 (RHours), blank (PHours), blank (Ratio)
Machine2, blank (RHours), 1 (PHours), blank (Ratio)
Machine3, 4 (RHours), 6 (PHours), 0.4 (Ratio)


The whole thing would work if I could default a 0 for RHours and PHours when there was no corresponding record in the initial data table. Any suggestions on how to do that without modifying the original data?

Thanks,
Feb 28 '07 #1
3 3404
MMcCarthy
14,534 Expert Mod 8TB
Try this...

Expand|Select|Wrap|Line Numbers
  1. SELECT A.Machine, nz(B.[RHours],0), nz(C.[PHours],0), B.[RHours] / IIf((nz(C.[PHours],0) + nz(B.[RHours],0))=0,1,(nz(C.[PHours],0) + nz(B.[RHours],0))) AS Ratio
  2. FROM (A LEFT JOIN B ON A.Machine = B.Machine) LEFT JOIN C ON A.Machine = C.Machine;
  3.  
Feb 28 '07 #2
Try this...

Expand|Select|Wrap|Line Numbers
  1. SELECT A.Machine, nz(B.[RHours],0), nz(C.[PHours],0), B.[RHours] / IIf((nz(C.[PHours],0) + nz(B.[RHours],0))=0,1,(nz(C.[PHours],0) + nz(B.[RHours],0))) AS Ratio
  2. FROM (A LEFT JOIN B ON A.Machine = B.Machine) LEFT JOIN C ON A.Machine = C.Machine;
  3.  
All I can say is WOW! I had given up on being able to do this.....and I'm still not sure how it worked but it did.

Thanks!
Shawn
Feb 28 '07 #3
MMcCarthy
14,534 Expert Mod 8TB
All I can say is WOW! I had given up on being able to do this.....and I'm still not sure how it worked but it did.

Thanks!
Shawn
No problem Shawn.

The nz() function will return 0 if the value is null and since you can't divide a number by 0 the IIf statement checks for 0 and replaces it with 1.

Mary
Mar 1 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Q. John Chen | last post by:
Using MySQL control center, I created a MySQL database table with a column of Date type. It always give the a Default value (0000-00-00) even null is allowed. I tried remove the default value and...
6
by: Jason | last post by:
I have a function which performs a query and returns a table. The one parameter that can get passed in is a date which defaults to NULL. There is an IF statement in the function that will set the...
3
by: Ian D | last post by:
Firstly apologies for the convoluted question. I found this problem whilst building a larger database. I've distilled it down to as small as possible and can send a 200k example to anyone who has...
2
by: The Plankmeister | last post by:
Hi... I have a query which I'm accessing through PHP as a stored procedure. However, I need to be able not to pass a couple of parameters in certain situations. When I do this, I get an error: ...
2
by: Viorel | last post by:
Adding new row with default values. In order to insert programmatically a new row into a database table, without direct "INSERT INTO" SQL statement, I use the well-known DataTable.NewRow,...
9
by: Rizwan Karedoa | last post by:
Hi experts, I am developing an application, I am using vb 2005 and access. I have many date fields, When I am saving through Query for default date I save 1/1/1500 so when i find that date agian I...
3
by: Jason Mobarak | last post by:
Hello -- I'm attempting to get a handle on how to do xpath queries with System.Xml -- so far the biggest hurdle has been how to deal with a default namespace. If I use the test xml: <?xml...
4
by: Torilyn73 | last post by:
I have a combo box set up off a query. I want the default value to be the column heading so I don't have to put a label over it. So far I haven't been able to figure this out. Can anyone tell me if...
10
by: Brad Baker | last post by:
I have an asp.net/csharp application that requires a particular variable to work properly. This variable is usually passed via a query string in the URL when the application is first run but under...
3
by: MLH | last post by:
I have a table named tblDoItems. It has a text field named . There is no default value property setting at the table level. I have a query named qryAdminDoList based solely on the table that looks...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.