Report and Graphic in access 2007 - calculating values on queries - ms-access

Picture a table with fields (Id, Valid, Value)
Valid = boolean
Value = number from 0 to 100
What I want is a report that counts the number of records where (valid = 0), and then gives me the total number of cases where (value < 70) and the number of cases where (value >= 70).
The problem is that the "value" field could be empty on some of the records and I only want the records where the value field is not empty.
I know that the second value (value>=70) is going to be calculated, but the problem is that I can't simply do (total number of records - number of records where value < 70), because there's the problem with the records where "value" is null...
And then I want to create graphic with these values, to see the percentage of records below and above 70.

"The problem is that the "value" field could be empty on some of the records and I only want the records where the value field is not empty."
Use a WHERE clause to exclude rows whose "value" field is Null.
Here is sample data for tblMetraton.
Id Valid Valu
1 -1 2
2 0 4
3 -1 6
4 0
5 0 90
I used Valu for the field name because Value is a reserved word.
You can use the Count() function in a query and take advantage of the fact it only counts non-Null values. So use Count() with an IIf() expression which returns a non-Null value (I used 1) for the condition you want to match and Null otherwise.
SELECT
Count(IIf(Valid=0,1,Null)) AS valid_false,
Count(IIf(Valu<70,1,Null)) AS below_70,
Count(IIf([Valu]>=70,1,Null)) AS at_least70
FROM tblMetraton AS m
WHERE (((m.[Valu]) Is Not Null));
Based on my sample data in tblMetraton, that query gives me this result set.
valid_false below_70 at_least70
2 3 1
If my sample data does not address the variability you're dealing with, and/or if my query results don't match your requirements, please show us you own sample data and the results you want based on that sample.

Related

SSRS Conditional Max

I would like my expression to return the max value of a column where another column equals "0".
example :
0 12
0 11
0 7
1 3
1 40
1 1
This should return 12.
I tried several things but can't make it work.
Any ideas?
Try something like.
=MAX(IIF(Fields!ColumnA.Value = 0, Fields!ColumnB.Value, -99999))
Column A and B refer to your unnamed columns in your sample data. The -99999 should be a value lower than the lowest Columb B value you will ever get. If Column B is always positive then any negative value or even 0 will suffice here.
The expression reads:
"for each row, look in Column A. If Column A is zero then return Column B's value, if Column A is not zero, return -99999. Now get the MAX value from these values"

How to not COUNT a value in SSRS matrix when value is NULL

I cannot make the my expression NOT count a NULL value in my SSRS matrix.
In my SSRS matrix, I have 2 columns one for AppraisalCompany and a count under the SubmittedDate column. In my report this what is happening:
Per Derrick's suggestion here is the change I made in the ColumnGroup properties for the SubmittedDate:
Here is my expression change in the ColumnGroup properties:
Unfortunately I got this error:
I'm suspicious of your Dataset, I'm not entirely sure how you're getting a null value to return 1 in the COUNT. I have been unable to reproduce your results.
Dataset Query
SELECT 'Drive In' AS AppraisalCompany, NULL AS SubmittedDate
UNION
SELECT 'Photo App - English', 'Dec-18'
Next I created a Row Group on AppraisalCompany and a Column Group on SubmittedDate.
I filtered the column group to remove the null grouping, using the expression =IsNothing(Fields!SubmittedDate.Value), operator <>, and Value true.
In the textbox in the matrix I used [Count(SubmittedDate)].
OUTUT
Appraisal Company | Dec-18
-------------------------------
Drive In | 0
Photo App - English | 1
By a Decimal (Number) datatype Nothing and 0 are the same. You can test this.
Put a tablix into your report with year from 2017 to 2019. Then put the year in a column of the tablix as a number format, then write the following expression in the detail textbox:
=CDec(IIF(CDec(Fields!Year.Value) = 2017, 0, Nothing))
After executing your report you will notice that every value in the year column is 0.
The same goes for the check. Both of these expressions will always return Yes. I basically check for 0 and the second one for for Nothing:
=IIF(CDec(IIF(CDec(Fields!Jahr.Value) = 2017, 0, Nothing)) = 0, "Yes", "No")
=IIF(CDec(IIF(CDec(Fields!Jahr.Value) = 2017, 0, Nothing)) = Nothing, "Yes", "No")
But remember your textbox/column has the be a number format.
So if you want to return Nothing and you display it in a number format textbox, it will show you a 0.
With this in mind it will make sense that a Count() returns the value 1 for 0 AND Nothing. So basically this will do the trick:
'Cont
=Sum(IIF(Fields!YourValue.Value = Nothing, 0, 1))

SSRS 2012 using switch statement with LookupSet count in group footer

Apologies for the long post.
I have an SSRS 2012 report looking at 2 datasets, Dataset1 and Dataset2.
Dataset2 may return 0, 1 or 2 records, so I'm using LookupSet to do this. From the records returned, I need to display several fields.
The report is grouped by Main_ID. In the footer of Main_ID, I am counting the number of returned records from Dataset2.
=LookupSet(Fields!MainID.Value, Fields!MainID.Value, Fields!Record_Name.Value, "Dataset2").Length
This works fine, I'm getting 0, 1 or 2 as appropriate.
In Dataset2 the field:
HasIP
will be used to determine which record in the array I display (as I need to test a field value in Dataset2).
I'm trying to use a switch statement to use the length of the LookupSet array to return the correct fields.
If there are no records (eg length of array = 0) then return the
number 2
If there is 1 record (eg length of array = 1), then return
Record_Name
If there are 2 records (eg length of array = 2), then
check the field HasIP.
If HasIP = YesYes, then return the Record_Name of that matching entry
If HasIP = YesNo, then return the Record_Name of that matching entry.
If all else fails, return the number 2
This is what I'm up to at the moment.
=Switch(
'if no returns returned, display the numeral 2
LookupSet(Fields!Main_ID.Value, Fields!Main_ID.Value, Fields!Record_Name.Value, "Dataset2").Length=0, 2,
'if one return returned, then display Record Name
LookupSet(Fields!Main_ID.Value, Fields!Main_ID.Value, Fields!Record_Name.Value, "Dataset2").Length=1,
LookUp(Fields!Main_ID.Value,Fields!Main_ID.Value,Fields!Record_Name.Value,"Dataset2"),
'if two records returned, check the HasIP field. If HasIP = YesYes, then use this combination first.
LookupSet(Fields!Main_ID.Value, Fields!Main_ID.Value, Fields!Record_Name.Value, "Dataset2").Length=2 AND
LookUpSet(Fields!Main_ID.Value,Fields!Main_ID.Value,Fields!HasIP.Value,"Dataset2")(0)="YesYes",
LookUpSet(Fields!Main_ID.Value,Fields!Main_ID.Value,Fields!Record_Name.Value,"Dataset2")(0),
LookupSet(Fields!Main_ID.Value, Fields!Main_ID.Value, Fields!Record_Name.Value, "Dataset2").Length=2 AND
LookUpSet(Fields!Main_ID.Value,Fields!Main_ID.Value,Fields!HasIP.Value,"Dataset2")(1)="YesYes",
LookUpSet(Fields!Main_ID.Value,Fields!Main_ID.Value,Fields!Record_Name.Value,"Dataset2")(1),
'if two records returned, check the HasIP field. If HasIP = YesNo, then use this combination second.
LookupSet(Fields!Main_ID.Value, Fields!Main_ID.Value, Fields!Record_Name.Value, "Dataset2").Length=2 AND
LookUpSet(Fields!Main_ID.Value,Fields!Main_ID.Value,Fields!HasIP.Value,"Dataset2")(0)="YesNo",
LookUpSet(Fields!Main_ID.Value,Fields!Main_ID.Value,Fields!Record_Name.Value,"Dataset2")(0),
LookupSet(Fields!Main_ID.Value, Fields!Main_ID.Value, Fields!Record_Name.Value, "Dataset2").Length=2 AND
LookUpSet(Fields!Main_ID.Value,Fields!Main_ID.Value,Fields!HasIP.Value,"Dataset2")(1)="YesNo",
LookUpSet(Fields!Main_ID.Value,Fields!Main_ID.Value,Fields!Record_Name.Value,"Dataset2")(1),
'If all else fails, display the number 2
True, 2)
I'm getting the correct one returned for 2 records, but getting #ERROR for zero and 1 records. If I break the formula down to their individual parts:
=SWITCH(
LookupSet(Fields!MainID.Value, Fields!MainID.Value, Fields!Record_Name.Value, "Dataset2").Length=0,
2)
This works. The number 2 is displayed in my report where the are no entries in Dataset2 (the field is blank for any other combination).
If I use:
=SWITCH(LookupSet(Fields!MainID.Value, Fields!MainID.Value, Fields!Record_Name.Value, "Dataset2").Length=1, LookUp(Fields!MainID.Value,Fields!MainID.Value,Fields!Record_Name.Value,"Dataset2"))
This works fine for when I have 1 record returned from Dataset2 (the field is blank for any other combination).
No matter what I tried with the third part (handling the 2 records), I'm getting issues. I can get the correct Record Name returned, but I'm getting #ERROR for 0 or 1 records.
I've tried nesting the third part in IIF statements, flipping it round to look at the length first, etc, but no success.
I cannot edit the underlying query of either dataset.
Any guidance much appreciated.
Solved
See:
SSRS 2012. Replicate grouping results in report in Query Designer
Essentially it was adding a numeric value to HasIP and also add in the Primary key of the second dataset.
Then sort the LookupSet array to get the value you want.
In the expression, you can use this to assign a default value, even when there are not results returned by the LookupSet:
=iif(
LookupSet(
Fields!Main_ID.Value,Fields!Main_ID.Value,Fields!Main_ID.Value,"Dataset2"
).Length>0,
Mid(
Split(
Join(
Code.JoinSorted(
LookupSet(
Fields!Main_ID.Value,Fields!Main_ID.Value,Fields!Q12b.Value,"Dataset2")
)
,";")
,";").GetValue(0)
, 'start point of mid
InStr(
Join(
Code.JoinSorted(
LookupSet(
Fields!Main_ID.Value,Fields!Main_ID.Value,Fields!Q12b.Value,"Dataset2"
)
)
,";")
,"$")
+1,
1),
"2"
)

SAS : Eliminate duplicates if a condition is satisfied

I want to eliminate duplicates from a database, based on an identifier, an order and a condition.
More precisely, I have data with several observations. I have sometimes a condition that makes me want to keep that observation anyway (let fix it condition=1), but then also keep the observation with the same identifier even if this condition does not hold (condition=0).
But if I have for one identifier several observations where condition=0 then I want to elminate duplicates, with criterion being having the greatest order.
Without the condition I can do that
proc sort data=have;
by identifier descending order;
run;
proc sort nudopkey data=have;
by identifier;
run;
But how to incorporate my condition in this ?
Edit 1 : add a database example :
data Test;
input identifier $ order condition;
datalines;
1023 1 0
1023 2 0
1064 2 0
1064 1 0
1098 1 0
1098 1 1
;
Then I want to keep
1023 2 0
1064 2 0
1098 1 0
1098 1 1
Edit 2 : tried to precise my conditions
I presume you want to eliminate duplicates only when the condition for all records for an identifier is set to 0. In that case you want to keep the record with the maximum order and eliminate all other records with the same identifier.
Proc sql;
create table want as
select *
from test
group by identifier
having max (condition) ne 0
or order eq max (order)
;
Quit;
This will keep all rows for an Identifier where the maximum condition = 1,
or in the case of those where maximum condition = 0, select the row with the maximum order.
Is that what you want?
Some of this depends on how you define 'condition'. Is your condition easily verifiable on every record for that identifier? Then you can do something like this.
Evaluate the condition.
For records where it is true (you want to remove the duplicate), set flag=0. For records where it is not true, increment the condition flag by one.
If the condition is true for all records in that ID, all will have the same value (flag=0) and nodupkey on by identifier flag; will remove extras. If the condition is false for all records, those will not be removed. If it's true for some and false for some, and you want to remove only some of the records with that identifier (only the duplicates where it is true), then you have to make sure that either it's sorted to have all of the condition=true records at top, or have a separate flag counter that determines what value the flag will be (since it sometimes will go to 0 in the middle, so 0 0 0 1 2 3 0 4 5 6 is what you want, not 0 0 0 1 2 3 0 0 1 2 ).
Perhaps easier to see is to do it within a datastep. After sorting by identifier descending order:
data want;
set have;
by identifier descending order;
if (condition=true) and not (first.identifier) then delete;
run;
This will, again, work if either condition=true is always at the top, or if it's always consistent within one ID group. If it's inconsistent and mixed, then you need to keep track of whether you've kept one where it was true (assuming you want to), or it might delete all records where it is true; use a separate variable to keep track of how many you've kept. first.identifier will be 1/TRUE for the first record for that identifier only, not taking into account the condition. You could also create the flag, then sort by identifier flag descending order; and guarantee the condition=true are at the top (either by making flag=0 for true, or sorting by descending flag.)

Preference for rows where field is different from "x"

I want to query my database so that I don't order by or order by desc but instead seek rows when a given field is different from 0 first and only after get others ordered by if there are no rows with field != "0". What is the best way to accomplish this or even is it possible to do so?
This is supposing the field can have values from -100 to 0 to 100
With example:
Considering LIMIT equals 3 and field in consideration is field1
field1
10
0
-20
-40
Another set:
field1
0
0
-100
50
In the first set, the results extracted will be 10, -20 and -40 (by any order), while in the second set they should be -100, 50 and 0 (any of the zeros, by any order).
First I'd like to check whether there exists != 0 in the database and if true extract those and only after 0's until I fill the LIMIT
something like this?
its not clear AT ALL what you want so I'm only guessing here.
I want to query my database so that I don't order by or order by desc but instead seek rows when a given field is different from 0
seek rows to me means find all rows that are not 0.
only after get others ordered by if there are no rows with field != "0".
to me that means order the rows in a sub query where the field is not 0.
with that in mind maybe this query?
SELECT * FROM table t
WHERE field IN (
SELECT field
from table t
WHERE field <> 0
ORDER BY field
)
without data It's not clear what you want. but i think something like this is what you were asking.. you could also try
SELECT * FROM table t
WHERE field <> 0
ORDER BY field