Crosstab Text Null Values

steve21nj

Registered User.
Local time
Today, 15:38
Joined
Sep 11, 2012
Messages
260
I have been working on a crosstab query where my text value (not a number) will not return a 0 instead of a null value.

My expression that returns the crosstab value is:
Code:
Expr1: Nz(Count(IIf([PropertyType]=" ",0,[PropertyType])))

The PropertyType can be "Destroyed" or "Turn Over". Am I setting up the Expr1 wrong?

The SQL for my query is:
Code:
TRANSFORM Nz(Count(IIf([PropertyType]=" ",0,[PropertyType]))) AS Expr1
SELECT qsites.site
FROM (qryPropertyDetails RIGHT JOIN (qsites LEFT JOIN Detention ON qsites.key = Detention.AppSite) ON qryPropertyDetails.DetentionID = Detention.Key) LEFT JOIN qPropertyType ON qryPropertyDetails.PropertyTypeID = qPropertyType.PropertyTypeID
GROUP BY qsites.site
PIVOT qPropertyType.PropertyType;

I have attached two screen shots. In Jan7b I would like all the null values to show 0 instead of nothing. Is this possible?
 

Attachments

  • jan7a.PNG
    jan7a.PNG
    35.4 KB · Views: 108
  • jan7b.PNG
    jan7b.PNG
    8.6 KB · Views: 121
[FONT=&quot]Expr1: Nz(Count(IIf([PropertyType]=" ",0,[PropertyType])))[/FONT]
I think it should be so, (I haven't tried it), then if count return null, then set it to 0.
[FONT=&quot]Expr1: Nz(Count([PropertyType]),0)[/FONT]
 
That worked great! Guess I was over thinking a simple thing.

With the crosstab, I was attempting to calculate the totals. I am able to do this fine in the report footer for each text box of the details section.

Is it possible to also sum each individual row? When I attempt to add together, it takes the correct values but doesn't add, simply meshes.

For example.
Code:
[B][B]Site     Personal     Medication   Money    Seized Property    Total[/B][/B]
[B]South       2              0          0            1            3[/B]
 
[B]But my display is showing like my attachment:[/B]
 
[B][B]Site     Personal      Medication  Money    Seized Property     Total[/B][/B]
[B]South       2             0         0            1               2001[/B]

It takes the values of 2, 0, 0, and 1 and places them on the line.

Code:
=([Personal]+[Medication]+[Money]+[Seized Property])

I tried it with the Nz function, changed the format to currency on both the report and query with no results.
 

Attachments

  • jan-8.PNG
    jan-8.PNG
    18.3 KB · Views: 112
The reason is because Nz(), based on my experience returns a String.. So when you use Field1+Field2+Friled3+Field4 it will give a concatenation of the String values returned.. So what I do is normally use a wrapper function.. that will convert the string to Integer.. something like..
Code:
CInt(Nz(FieldName, 0))
See if it works..
 
Works great....

the final result for the detail section:

Code:
=CInt(Nz([Personal],0)+CInt(Nz([Medication],0)+CInt(Nz([Money],0)+CInt(Nz([Seized Property],0)))))

final result for the footer:

Code:
=Sum([Personal])+Sum([Medication])+Sum([Money])+Sum([Seized Property])

thank you very much!
 
Glad to help Steve, I sometime find puzzled by how the data is displayed.. so tinkering around them lead me to this..
 
Is it possible to also sum each individual row?
Can't you do it in the crosstabel query at once like below?
TRANSFORM .... AS Expr1
SELECT qsites.site, Count([PropertyType]) AS [TotalPropertyType]
FROM ....
 
That also works great. I was attempting to further narrow down the results by status, making only "Processed" or "UnProcessed" for the criteria.

When I add this, it only shows the two sites where the property is marked either "Processed" or "UnProcessed", removing the rest of the data. Any thoughts as to why?

With Criteria:

Code:
TRANSFORM Nz(Count([PropertyType]),0) AS Expr1
SELECT qsites.site, Count(qPropertyType.[PropertyType]) AS TotalPropertyType
FROM tblStatus RIGHT JOIN ((qryPropertyDetails RIGHT JOIN (qsites LEFT JOIN Detention ON qsites.key = Detention.AppSite) ON qryPropertyDetails.DetentionID = Detention.Key) LEFT JOIN qPropertyType ON qryPropertyDetails.PropertyTypeID = qPropertyType.PropertyTypeID) ON tblStatus.StatusID = qryPropertyDetails.PropertyStatus
WHERE (((tblStatus.Status)="Processed")) OR (((tblStatus.Status)="UnProcessed"))
GROUP BY qsites.site
PIVOT qPropertyType.PropertyType;

Without Criteria:
Code:
TRANSFORM Nz(Count([PropertyType]),0) AS Expr1
SELECT qsites.site, Count(qPropertyType.[PropertyType]) AS TotalPropertyType
FROM tblStatus RIGHT JOIN ((qryPropertyDetails RIGHT JOIN (qsites LEFT JOIN Detention ON qsites.key = Detention.AppSite) ON qryPropertyDetails.DetentionID = Detention.Key) LEFT JOIN qPropertyType ON qryPropertyDetails.PropertyTypeID = qPropertyType.PropertyTypeID) ON tblStatus.StatusID = qryPropertyDetails.PropertyStatus
GROUP BY qsites.site
PIVOT qPropertyType.PropertyType;

Not sure if I need to redo my queries or not.
 

Attachments

  • criteriaSQL.PNG
    criteriaSQL.PNG
    44.4 KB · Views: 106
  • criteriaResult.PNG
    criteriaResult.PNG
    5.1 KB · Views: 109
  • WithoutCriteriaSQL.PNG
    WithoutCriteriaSQL.PNG
    44.5 KB · Views: 101
  • WithoutCriteriaResults.PNG
    WithoutCriteriaResults.PNG
    12.5 KB · Views: 110
Maybe the following can do it, (haven't tried it), and then remove the criteria.

TRANSFORM Nz(iif(tblStatus.Status="Processed" OR tblStatus.Status="UnProcessed", Count([PropertyType]),0),0) AS Expr1
..
..
..
 
I get an error saying you tried to execute a query that does not include the specified expression as part of an aggregate function.

SQL:
Code:
TRANSFORM Nz(IIf(tblStatus.Status="Processed" Or tblStatus.Status="UnProcessed",Count([PropertyType]),0),0) AS Expr1
SELECT qsites.site, Count(qPropertyType.PropertyType) AS TotalPropertyType
FROM tblStatus RIGHT JOIN ((qryPropertyDetails RIGHT JOIN (qsites LEFT JOIN Detention ON qsites.key = Detention.AppSite) ON qryPropertyDetails.DetentionID = Detention.Key) LEFT JOIN qPropertyType ON qryPropertyDetails.PropertyTypeID = qPropertyType.PropertyTypeID) ON tblStatus.StatusID = qryPropertyDetails.PropertyStatus
GROUP BY qsites.site
PIVOT qPropertyType.PropertyType;
 

Attachments

  • Capture.PNG
    Capture.PNG
    67.4 KB · Views: 113
I added:
Status as a field,
total: Group by

Kinda does what i want, but it duplicates the values on the results.

Code:
TRANSFORM Nz(IIf([tblStatus].[Status]="Processed" Or [tblStatus].[Status]="UnProcessed",Count([PropertyType]),0),0) AS Expr1
SELECT qsites.site, Count(qPropertyType.PropertyType) AS TotalPropertyType
FROM tblStatus RIGHT JOIN ((qryPropertyDetails RIGHT JOIN (qsites LEFT JOIN Detention ON qsites.key = Detention.AppSite) ON qryPropertyDetails.DetentionID = Detention.Key) LEFT JOIN qPropertyType ON qryPropertyDetails.PropertyTypeID = qPropertyType.PropertyTypeID) ON tblStatus.StatusID = qryPropertyDetails.PropertyStatus
GROUP BY qsites.site, tblStatus.Status
PIVOT qPropertyType.PropertyType;
 

Attachments

  • Capture2.PNG
    Capture2.PNG
    42.7 KB · Views: 110
Last edited:
Did you try Expr1 as a Group By instead of expression?? I am not sure what I am talking, as I have not dealt with a lot of Cross Tab queries..

EDIT : Never mind.. I just saw your answer..
 
Last edited:
My results are still off by duplicating whenever a site has a property
 
It is because of the grouping on the tblStatus.Status
Then it will made a row for each type/value of status.
Do you have some sample data? If, then I can look at it, but I've only MS-Access 2000.
 
Sure. The query that I have been testing is: [qhelp3]

The Query should return this instead of the previous:

Code:
Site    Personal   Medication   Money  Seized Property  Total
""       0              0        0              0           0
""
""
North     1             1         0              0          2
South     2             0         0              1          3
""
""
 
Last edited:
You've to do it in 2 steps.
First create the below query, call it "qryProperty1Part":
SELECT qsites.site, qPropertyType.PropertyType, IIf([tblStatus].[Status]="Unprocessed" Or [tblStatus].[Status]="Processed",[tblStatus].[Status]) AS Status
FROM tblStatus RIGHT JOIN ((qryPropertyDetails RIGHT JOIN (qsites LEFT JOIN Detention ON qsites.key = Detention.AppSite) ON qryPropertyDetails.DetentionID = Detention.Key) LEFT JOIN qPropertyType ON qryPropertyDetails.PropertyTypeID = qPropertyType.PropertyTypeID) ON tblStatus.StatusID = qryPropertyDetails.PropertyStatus;
Then the second query:
TRANSFORM nz(Count([Status]),0) AS Udtryk1
SELECT qryProperty1Part.site, Count(qryProperty1Part.Status) AS TotalPropertyType
FROM qryProperty1Part
GROUP BY qryProperty1Part.site
PIVOT qryProperty1Part.PropertyType;
 
Thank you! I understand the reasoning behind the query and hope to apply this methodology in the future. The results are great. Thank you again!
 

Users who are viewing this thread

Back
Top Bottom