adding 2 fields including null entries

G

Guest

I'm working on a database which has 2 fields which each hold a name from a
list of names in a table. The 2 fields represent the same thing in the big
picture of the database, but for accesibility reasons, i need two have 2
fields. I need a query which can add the number of times each name appears in
both fields and then combine them. I have 2 crosstab queries which count the
number of times each name appears in each separate column. Some of the names
in one field are not neccesarily in the other so when i try to make a query
that adds the values of the 2 crosstab queries, it only gives the names and
combined values of the names in both fields (the intersection of both
columns). I'm aware that when adding two fields and if one is null the answer
is null, but there has to be some way to get this to work. Thanks you. Help
would be much appreciated!
 
I

Ilan

Hello,
I had a similar problem with the addition. The way to
solve your math issues is with an Nz field. In a query,
create a new calculated field and write an expression
like this:

NameofCalculatedField: Nz([FieldwithNullValue], 0)

Basically, create a new field and name it anything you
want. The query will read this line of code and if there
is a null value in the field (that i named for example
purposes) "FieldwithNullValue" then it will replace the
null with a zero. You can change the zero to whatever
value you want, but it sounds like you just want to add
it and count it as a zero.

Hope this was Helpful,
Ilan
 
G

Guest

Jesse said:
I'm working on a database which has 2 fields which each hold a name from a
list of names in a table. The 2 fields represent the same thing in the big
picture of the database, but for accesibility reasons, i need two have 2
fields. I need a query which can add the number of times each name appears in
both fields and then combine them. I have 2 crosstab queries which count the
number of times each name appears in each separate column. Some of the names
in one field are not neccesarily in the other so when i try to make a query
that adds the values of the 2 crosstab queries, it only gives the names and
combined values of the names in both fields (the intersection of both
columns). I'm aware that when adding two fields and if one is null the answer
is null, but there has to be some way to get this to work. Thanks you. Help
would be much appreciated!

It is still giving me the intersection of both columns, with the names that
are only in both fields. I need all the names in the first column with their
sum, all the names in the second with their their sum, and all the names in
both with the sum added from both columns.
 
I

Ilan

Check your join properties. Go to design view of the
query and right click on the relationship. Click join
properties. I think you should have a Left Join which is
the second option down. Right now, from what i can tell,
you sound like you have a plain inner join. Let me know.

Ilan
 
G

Guest

Ok, i changed to the second option, it didnt work, and i tried the third
option which half way worked. I have a table with the names and 2 queries
with the two columns' totals. All 3 are in this query. Any other ideas?
 
G

Guest

Hi Jesse,

If I understand your post correctly, I believe that your solution will
likely have to involve a union query somewhere because a left or right join
between your crosstab queries will potentially omit names that exist only in
the other column.

You may want to consider a different approach. The following query will
calculate the name counts using a union query as the source. The union query
counts the names in each column, then the query further groups the names and
adds the results. The query assumes a table name of tblNames and field names
of Name1 and Name2. If you replace those with the actual table/field names
and paste the following into sql view of a new query, I believe you will get
what you are looking for. Post back if you have any difficulty. Here is the
sql:

SELECT AllNames.NameGrp,
Sum(AllNames.CountOfName1) AS CountOfName1,
Sum(AllNames.CountOfName2) AS CountOfName2,
Sum(AllNames.CountOfName1) + Sum(AllNames.CountOfName2) AS TotalCountOfNames
FROM (SELECT tblNames.Name1 AS NameGrp,
Count(tblNames.Name1) AS CountOfName1, 0 AS CountOfName2
FROM tblNames
GROUP BY tblNames.Name1
HAVING tblNames.Name1 Is Not Null
UNION SELECT tblNames.Name2, 0, Count(tblNames.Name2)
FROM tblNames
GROUP BY tblNames.Name2
HAVING tblNames.Name2 Is Not Null) AS AllNames
GROUP BY AllNames.NameGrp
ORDER BY AllNames.NameGrp;

HTH, Ted Allen
 
G

Guest

I'm having difficulting following the code. I have 2 queries, Xtab1 and
Xtab2, then a table, Personnel. The names lie in Personnel but im not sure
that what i need even has to use those names since the ones im adding are
already in Xtab1 and Xtab2. Each querie has a list of names with their
totals. I do in fact want the Union of both queries, in the case if the name
is in both queries i'd like their totals combined. Your code may be right,
but i was just having trouble following it. Do you think there's anyway you
can take this information and enhance it a little? Thanks a bunch.
 
G

Guest

Hi Jesse,

You can union the results of the two crosstabs, but then you would still
have to group the results and sum them to get the totals. The query I had
posted would do everything directly, so that you wouldn't need the two
crosstabs at all. I would be happy to break it down for you, but, if you
don't mind, post the names of your two name fields and I will first revise
the sql so that it will work for you without modification. Then, I'll break
that one down into pieces to help explain what it is doing.

-Ted Allen
 
G

Guest

I'll just give you as much detail as i can.
Xtab1 has (Field Name-Total-Crosstab) PSS-Group By-Row Heading, Year-Group
By Column Heading, Total Of PSS-Count-Row Heading, and PSS-Count-Value. Xtab2
has PSS2-Group By-Row Heading, Year-Group By Column Heading, Total Of
PSS2-Count-Row Heading, and PSS2-Count-Value. Then I have a table, Personnel
with field Names, which provides the names for PSS and PSS2, and the whole
point of this is to find how many times those names appear, so im not sure if
you need this table. So i need a list of the names with their Total Of PSS &
Total of PSS2 combined. The Year Column heading is not neccessary but it
would be nice. I hope that you can understand all of this. Let me know if you
need any more info. Thank you
 
G

Guest

Hi Jesse,

First I'll go through the method that will not use the crosstabs. I'll use
your field names, and also group by year since you said that would be
preferred.

To start with, you need to define the individual queries that will select
the names from each individual field, so that they can be unioned in the next
step. We would like to know the name, the year, and how many times that name
occurs in that year for each field.

First, we'll define the select query for the PSS Names:

SELECT Personnel.PSS AS Person, Personnel.Year, Count(Personnel.PSS) AS
PSSCount, 0 AS PSS2Count
FROM Personnel
GROUP BY Personnel.PSS, Personnel.Year
HAVING Personnel.PSS Is Not Null;

This query will select all records from the Personnel table with non-null
entries in the PSS field. It will group the records by Name and Year, and
will provide the count of the number of records matching that name/year. It
will also enter a 0 in a column for PSS2Count (since those will be counted by
the next query).

Then, a similar query will count the PSS2 entries:

SELECT Personnel.PSS2, Personnel.Year, 0, Count(Personnel.PSS2)
FROM Personnel
GROUP BY Personnel.PSS2, Personnel.Year
HAVING Personnel.PSS2 Is Not Null;

The only differences being that I'm counting the PSS2 fields for those
records where PSS2 is not null, I didn't alias any of the fields (because the
union query in the next step will only recognize field names/aliases from the
first query anyway), and I entered the 0 in the PSSCount field and placed the
PSS2 count in the PSS2Count field.

Note that you could paste the sql from either of the queries above in the
sql view of a new query and see what they are doing. Note also that these
two queries are essentially doing the same thing as your existing crosstab
queries.

Next, we want to combine these results, so we use a union statement. To use
a union, you basically just type the word UNION between the sql of the two
queries, so our query becomes:

SELECT Personnel.PSS AS Person, Personnel.Year, Count(Personnel.PSS) AS
PSSCount, 0 AS PSS2Count
FROM Personnel
GROUP BY Personnel.PSS, Personnel.Year
HAVING Personnel.PSS Is Not Null
UNION
SELECT Personnel.PSS2, Personnel.Year, 0, Count(Personnel.PSS2)
FROM Personnel
GROUP BY Personnel.PSS2, Personnel.Year
HAVING Personnel.PSS2 Is Not Null;

This will give you the combination of the first two tables, which will be
similar to a combination of your existing crosstabs.

Next, we want to group the records in the union query by name and year, and
sum the counts. If the records from the union query above were in a table
called AllNames, the query would look like the following:

SELECT AllNames.Person, AllNames.Year, Sum(AllNames.PSSCount) AS PSSCount,
Sum(AllNames.PSS2Count) AS PSS2Count, Sum(AllNames.PSSCount) +
Sum(AllNames.PSS2Count) AS TotalNameCount
FROM AllNames
GROUP BY AllNames.Person, AllNames.Year
ORDER BY AllNames.Person, AllNames.Year;

But, instead of having the data in a table, we want to use our union query
as a source. We could save the union query, and reference it here in place
of AllNames. Or, we can just insert the sql for the query itself and alias
the resulting recordset as AllNames. When doing this, we enclose the source
sql in ()'s such as the following:

SELECT AllNames.Person, AllNames.Year, Sum(AllNames.PSSCount) AS PSSCount,
Sum(AllNames.PSS2Count) AS PSS2Count, Sum(AllNames.PSSCount) +
Sum(AllNames.PSS2Count) AS TotalNameCount
FROM (Place Union Query SQL Here) AS AllNames
GROUP BY AllNames.Person, AllNames.Year
ORDER BY AllNames.Person, AllNames.Year;

Therefore, inserting the Union Query SQL inside the ()'s gives the final
query sql:

SELECT AllNames.Person, AllNames.Year, Sum(AllNames.PSSCount) AS PSSCount,
Sum(AllNames.PSS2Count) AS PSS2Count, Sum(AllNames.PSSCount) +
Sum(AllNames.PSS2Count) AS TotalNameCount
FROM (SELECT Personnel.PSS AS Person, Personnel.Year, Count(Personnel.PSS)
AS PSSCount, 0 AS PSS2Count
FROM Personnel
GROUP BY Personnel.PSS, Personnel.Year
HAVING Personnel.PSS Is Not Null
UNION SELECT Personnel.PSS2, Personnel.Year, 0, Count(Personnel.PSS2)
FROM Personnel
GROUP BY Personnel.PSS2, Personnel.Year
HAVING Personnel.PSS2 Is Not Null) AS AllNames
GROUP BY AllNames.Person, AllNames.Year
ORDER BY AllNames.Person, AllNames.Year;

If you copy this sql and paste it into the sql view of a new query, I
believe that it will give you what you are looking for.

If you want to check against the results of your crosstabs, you can union
them by typing the following in a union query:

SELECT * FROM Xtab1
UNION ALL
SELECT * FROM Xtab2

Note that in this case I used Union All, because your counts in the Xtab
queries will be in the same column, and if you had records in the two Xtabs
that had the same values for all fields, a normal union query would exclude
them.

Then, you could do a summary query on the result of that union to get your
total count.

By, the way, if you want a query to give you the counts by name, without
grouping by year, the sql would be:

SELECT AllNames.Person, Sum(AllNames.PSSCount) AS PSSCount,
Sum(AllNames.PSS2Count) AS PSS2Count, Sum(AllNames.PSSCount) +
Sum(AllNames.PSS2Count) AS TotalNameCount
FROM [SELECT Personnel.PSS AS Person, Count(Personnel.PSS) AS PSSCount, 0 AS
PSS2Count
FROM Personnel
GROUP BY Personnel.PSS
HAVING Personnel.PSS Is Not Null
UNION SELECT Personnel.PSS2, 0, Count(Personnel.PSS2)
FROM Personnel
GROUP BY Personnel.PSS2
HAVING Personnel.PSS2 Is Not Null]. AS AllNames
GROUP BY AllNames.Person
ORDER BY AllNames.Person;

Hopefully this will help, and hopefully I got your table and field names
right. Post back if it doesn't work or if you have other questions.

-Ted Allen
 
G

Guest

Ok, first i tried:

SELECT * FROM Xtab1
UNION SLECT * FROM Xtab2

and this worked except the names that were in both showed up separate. I
then went and did a summary query but it still gave me the same info. Could
an IIf statement in the sql solve this? If i can find an answer to this i
might as well just forget about the sql doing the crosstabs b/c i know that i
can get this to work.

When it comes to the sql which does the the crosstab work for me, my person
field (i.e. "AllNames.Person") and my Year field, come from different tables,
and my PSS field also comes from a different table. So, should i need
something different in the FROM statements and something different in the
____.Person, and ___.Year? Thank you very much for your help so far. I'm
making progress. My email is (e-mail address removed) if you want to reply there
b/c finding this entry is getting tedious. Thanks a bunch.
 
G

Guest

Hi Jesse,

Yes, the previous sql would have to be modified if the Year value is coming
from a different table. But, you didn't give any other details on the other
table so I can't really go any further with that.

The sql that I posted at the end of the previous message should still work,
it just won't have the year values.

In looking back at your previous post, I now realize that it looks like you
have set up the crosstabs to provide unique columns for each year. This will
create problems with the Union Query, because the Union will just line up the
colums by their order, not by name. So, if the first starts with Year 2000,
and the second with 2001, you will have mixed data, and the column will be
labeled according to the first query. So, before using the union query on
your crosstabs, you will need to assign fixed column headings to each to make
sure that they will always line up (you do this under the column headings
property of the query).

But, if you want the columns by year, and you want it to be more flexible,
it would be better to union the data first, then use that as the data source
for your crosstab.

If you post back with the table detail, I can give you the sql for that.

Right now it looks like you have a table Personnel, with Fields PSS and
PSS2, and a separate table somehow providing the year. Post some info on the
other table name, and how it joins to the Personnel table.

Or, post the SQL for your two existing crosstab queries (switch to sql view
and just copy/paste), that will probably give me all I need (your earlier
post only gave the values from the design grid, but that doesn't say what the
source tables are, or their relationships).

By the way, I try to keep the posts in the group so that others may benefit
as well if they need to do something similar. But, it should be pretty easy
to locate the post if you just search for your name (which is why I always
end my posts with my full name).

HTH, Ted Allen
 
G

Guest

Alright, here are my two existing crosstab queries.

TRANSFORM Count([Start Up].[Pre-Ship Setup]) AS [CountOfPre-Ship Setup]
SELECT [Start Up].[Pre-Ship Setup], Count([Start Up].[Pre-Ship Setup]) AS
[Total Of Pre-Ship Setup]
FROM [Start Up]
GROUP BY [Start Up].[Pre-Ship Setup]
PIVOT [Start Up].Year;

and

TRANSFORM Count([Start Up].[Pre-Ship Setup 2]) AS [CountOfPre-Ship Setup 2]
SELECT [Start Up].[Pre-Ship Setup 2], Count([Start Up].[Pre-Ship Setup 2])
AS [Total Of Pre-Ship Setup 2]
FROM [Start Up]
GROUP BY [Start Up].[Pre-Ship Setup 2]
PIVOT [Start Up].Year;


Let me know if you need more information
 
G

Guest

Thanks Jesse.

I think that should be enough. I assume then that these are the actual
table/field names, and the others that you had posted were shortened
versions? Or, is [Start Up] a query that pulls together the personnel and
year info? It's a little confusing because you had mentioned before that the
year info was coming from a different table than the PSS fields, but these
all appear to be coming from [Start Up].

In any case, I should be able to post the sql shortly using [Start Up] as
the source, and using the field names referenced in the crosstab sql.

In the meantime, post back if any of my assumptions above are incorrect.
Also, if [Start Up] is a query, you could post the sql for that query if you
would like for me to post a query based directly on the source queries rather
than that query. Finally, let me know what your ideal result would be. Do
you want just the total count by year, would you also like subtotals of each
type? Etc.

-Ted Allen
 
G

Guest

OK, I think the following union query should give you a good basis to create
a pivot from (I added a field PSSType to identify which column the records
came from, in case you later decided to differentiate by those as well):

SELECT [Start Up].[Pre-Ship Setup] AS PSSName, [Start Up].Year, "PSS" AS
PSSType, Count([Start Up].[Pre-Ship Setup]) AS PSSCount
FROM [Start Up]
GROUP BY [Start Up].[Pre-Ship Setup], [Start Up].Year, "PSS"
HAVING [Start Up].[Pre-Ship Setup] Is Not Null
UNION SELECT [Start Up].[Pre-Ship Setup 2], [Start Up].Year, "PSS2" AS Type,
Count([Start Up].[Pre-Ship Setup 2])
FROM [Start Up]
GROUP BY [Start Up].[Pre-Ship Setup 2], [Start Up].Year, "PSS2"
HAVING [Start Up].[Pre-Ship Setup 2] Is Not Null;

If you save the union query as qUnionStartup, you could use the following
crosstab query:

TRANSFORM Sum(qUS.PSSCount) AS SumOfPSSCount
SELECT qUS.PSSName
FROM qUnionStartup AS qUS
GROUP BY qUS.PSSName
PIVOT qUS.Year;

Or, if you want to do it all in one shot, you can use the following:

TRANSFORM Sum(qUS.PSSCount) AS SumOfPSSCount
SELECT qUS.PSSName
FROM (SELECT [Start Up].[Pre-Ship Setup] AS PSSName, [Start Up].Year, "PSS"
AS PSSType, Count([Start Up].[Pre-Ship Setup]) AS PSSCount
FROM [Start Up]
GROUP BY [Start Up].[Pre-Ship Setup], [Start Up].Year, "PSS"
HAVING [Start Up].[Pre-Ship Setup] Is Not Null
UNION SELECT [Start Up].[Pre-Ship Setup 2], [Start Up].Year, "PSS2" AS Type,
Count([Start Up].[Pre-Ship Setup 2])
FROM [Start Up]
GROUP BY [Start Up].[Pre-Ship Setup 2], [Start Up].Year, "PSS2"
HAVING [Start Up].[Pre-Ship Setup 2] Is Not Null) AS qUS
GROUP BY qUS.PSSName
PIVOT qUS.Year;

Post back if you have any problems, or if you would like any other summary
info.

-Ted Allen
 
G

Guest

Well, i think we're almost there. It seems to work. Start Up is actually a
table, and Year has its own table, but it is used as a lookup for one of the
fields in Start Up. Along with the totals for each year, at total of all the
years combined would be useful too. And, finally I have about 3 more sets of
fields i have to do this with in my Start Up table. If i just substitute the
field names that should be fine shouldnt it? Thank you very much.
Jesse
 
G

Guest

ok, ive been messing around with it and i copied and pasted what you gave me
and it worked. i tried another query and just substituted the fields and it
gave me an error when i tried to save:

Syntax error in query. Incomplete query clause.

I kept getting that error and then one time i copied the first sql which
worked and it gave me the same thing even though the first one worked. i dont
understand why it wont run the exaxt same query.
 
G

Guest

I'm not sure why the error. Try posting the sql of the revised query and
I'll see if I see anything unusual. Sometimes Access revises the SQL,
especially if you switch to design view. If that continues to be a problem,
it may be better to save the union query as one query, and base the pivot on
that (although I set up a test table on my end, and I haven't had any
problems - could be diff versions of Access).

Here is revised sql for a query that will give subtotals for each type, as
well as an overall total.

TRANSFORM Sum(qUS.PSSCount) AS SumOfPSSCount
SELECT qUS.PSSName, Sum(qUS.PSSCount) AS TotalPSS, Abs(Sum((PSSType =
"PSS")*PSSCount)) as SubTotalPSS1, Abs(Sum((PSSType = "PSS2")*PSSCount)) as
SubTotalPSS2
FROM (SELECT [Start Up].[Pre-Ship Setup] AS PSSName, [Start Up].Year, "PSS"
AS PSSType, Count([Start Up].[Pre-Ship Setup]) AS PSSCount
FROM [Start Up]
GROUP BY [Start Up].[Pre-Ship Setup], [Start Up].Year, "PSS"
HAVING [Start Up].[Pre-Ship Setup] Is Not Null
UNION SELECT [Start Up].[Pre-Ship Setup 2], [Start Up].Year, "PSS2" AS Type,
Count([Start Up].[Pre-Ship Setup 2])
FROM [Start Up]
GROUP BY [Start Up].[Pre-Ship Setup 2], [Start Up].Year, "PSS2"
HAVING [Start Up].[Pre-Ship Setup 2] Is Not Null) AS qUS
GROUP BY qUS.PSSName
PIVOT qUS.Year;

To answer your earlier question, you should be able to use the same syntax
to compare other pairs of fields. Just past the text in Word, find/replace
the field names, then copy/paste the sql to a new query.

Post back and let me know how it goes.

-Ted Allen
 
G

Guest

If i wanted just the combined total and Years columns without the 2 subtotals
what would i have to take out. And I'm still getting that error when i try to
save the same query with different field names. Here is what will not save:

TRANSFORM Sum(qUS.PSSCount) AS SumOfPSSCount
SELECT qUS.PSSName, Sum(qUS.PSSCount) AS TotalPSS, Abs(Sum((PSSType =
"PSS")*PSSCount)) AS SubTotalPSS1, Abs(Sum((PSSType = "PSS2")*PSSCount)) AS
SubTotalPSS2
FROM [SELECT [Start Up].[Plant Install] AS PSSName, [Start Up].Year, "PSS"
AS PSSType, Count([Start Up].[Plant Install]) AS PSSCount
FROM [Start Up]
GROUP BY [Start Up].[Plant Install], [Start Up].Year, "PSS"
HAVING [Start Up].[Plant Install] Is Not Null
UNION SELECT [Start Up].[Plant Install 2], [Start Up].Year, "PSS2" AS Type,
Count([Start Up].[Plant Install 2])
FROM [Start Up]
GROUP BY [Start Up].[Plant Install 2], [Start Up].Year, "PSS2"
HAVING [Start Up].[Plant Install 2] Is Not Null]. AS qUS
GROUP BY qUS.PSSName
PIVOT qUS.Year;

Its the same case except instead of Pre-Ship Setup its Plant Install. And it
still will not save the original sql when i try to make another copy of it.
 
G

Guest

when i get the error, it goes back to the sql and the cursor is in between
the s and the f in the word Transform.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top