I am creating a report in Access 2007 and I want to do the average, maximum, and minimum of several lab results. When I enter the following in the control source box, I get an #Error message in the group footer when all records being subtotaled are null when displaying the report. How can I get rid of this #Error and have the results return no value at all.
=CDbl(Avg([Arsenic]))
Thus data might look like this in my report
Serum
_______________________
Arsenic Iron
30 10
30
15
____________________
Avg 25 10
Max 30 10
Min 15 10
__________________________
Liver
__________________________
Arsenic Iron
8
0
2
____________________
Avg #Error 5
Max #Error 8
Min #Error 0
Many thanks
If you use just =Avg([Arsenic]), it'll be blank. Whats causing the #Error is when Avg() returns a blank, CDbl has a null input. If you absolutely must have CDbl() for whatever reason, then
=IIf(Avg([Arsenic]) Is Null,"",CDbl(Avg([Arsenic])))
will work
Related
hope you are well.
Resource
Reason
Qty
Average
11
Broken
15
48%
11
Shifted
5
16%
11
Flash
10
32%
11
Bleed
1
3%
So as you can see in the table above, I have a resource that has scrap and we need to break each reason down by % of the total.
So as you can see there is a total 31 scraps and I want to break each scrap by % for a particular Resource. I am not sure how to break it down by %
How would I be able the % in this way using SQL?
If I understand correctly, you can use window functions. To get the ratios:
select t.*,
(t.qty * 1.0 / sum(t.qty) over (partition by resource)) as ratio
from t;
You can multiply by 100 if you want a percentage between 0 and 100 rather than a ratio.
I'm suppose to Find the decade D with the largest number of films and the total number of films in D. A decade is a sequence of 10 consecutive years. For example, say in your database you have movie information starting from 1965. Then the first decade is 1965, 1966, ..., 1974; the second one is 1967, 1968, ..., 1976 and so on.
I'm suppose to implement this in jupyter note book where I imorpted sqlite3
I wrote the following code for it.
Select count(*) as total_films,concat(decade,'-',decade+9)
FROM (Select floor(YEAR('year')/10)*10 as decade FROM movie) t
GROUP BY decade
Order BY total_films desc;
However, the notebook threw error like "no such function: floor" and "no such function: Year" and no such function: concat"
Therefore, after going through sqlite documentation I changed code to
Select count(*) as total_films,decade||'-'||decade+9
FROM (Select cast(strftime('%Y',year)/10 as int)*10 as decade FROM movie) t
GROUP BY decade
Order BY total_films desc;
However, I got an incorrect output :
count(*) decade||'-'||decade+9
0 117 NaN
1 3358 -461.0
Would appreciate insights on why this is happening.
Updating question after going through comments by c.Perkins
1) I began, checking the type of year column
using the query PRAGMA table_info(movie)
Got the following result
cid name type notnull dflt_value pk
0 0 index INTEGER 0 None 0
1 1 MID TEXT 0 None 0
2 2 title TEXT 0 None 0
3 3 year TEXT 0 None 0
4 4 rating REAL 0 None 0
5 5 num_votes INTEGER 0 None 0
Since the year column is of the type text I changed to int using the cast function and check for nulls or NaN SELECT CAST(year as int) as yr FROM MOVIE WHERE yr is null
I didn't get any results, therefore it appears there are no nulls. However, on using the query SELECT CAST(year as int) as yr FROM MOVIE order by yr asc I see a lot of zeros in the year column
yr
0 0
1 0
2 0
3 0
4 0
-
-
-
-
3445 2018
3446 2018
3447 2018
3448 2018
3449 2018
3450 2018
From the above we see that the year is given as it is and in another stamp, therefore using strftime('%Y', year) did not yield result as mentioned in the comment.
Therefore, keeping all the above in mind, I changed the inner query to
SELECT (CAST( (year/10) as int) *10) as decade FROM MOVIE WHERE decade!=0 order by decade asc
Output for the above query :
decade
0 1930
1 1930
2 1930
3 1930
4 1930
5 1930
6 1940
7 1940
8 1940
-
-
-
3353 2010
3354 2010
3355 2010
3356 2010
3357 2010
Finally, placing this inner query, in the first query I wrote above
Select count(*) as total_films,decade||'-'||decade+9 as period
FROM (SELECT (CAST( (year/10) as int) *10) as decade FROM MOVIE WHERE decade!=0 order by decade asc)
GROUP BY decade
Output :
total_films period
0 6 1939
1 12 1949
2 71 1959
3 145 1969
4 254 1979
5 342 1989
6 551 1999
7 959 2009
8 1018 2019
As far as I can see the only issue is with period column where instead of showing 1930-1939 it is only showing 1939 and so on, if is using || is not right, is there anythother function that could be used ? because concat is not working.
Thanks in advance.
Pending updates to the question as requested in comments, here are a few immediate points that might help solve the problem without having all details:
Does the movie.year column contain null values? Likewise non-numeric or non-date values? the NaN (Not A Number) result likely indicates a null/invalid data in the source. (Technically there is no such NaN value in SQLite, so I'm assuming that the question data is copied form some other data grid or processed output.)
What type of data is in the column movie.year? Does it contain full ISO-8601 date strings or a Julian-date numeric value? Or does it only contain the year (as the column name implies)? If it only contains the year (as a string or integer), then the function call like strftime('%Y', year) will NOT return what you expect and is unnecessary. Just refer to the column directly.
I suspect this is where the -461.0 is coming from.
The operator / is an "integer division" operator if both operands are integers. A valid isolated year value will be an integer and the literal 10 is of course an integer, so integer division will automatically drop any decimal part and returns only the integer part of the division without having to explicitly cast to integer.
According to sqlite docs, the concatenation operator || has highest precedence. That means in the expression decade||'-'||decade+9, the concatenation is applied first so that one possible intermediate would be '1930-1930'+9. (Technically I would consider this result undefined since the string value does not contain a basic data type. In practices on my system, the string is apparently interpreted as 1930 and the overall result is the integer value 1939. Either way you will get unexpected bogus results rather than the desired string.)
I have a powerpivot table that shows work_tickets and timestamps for each step taken towards resolution:
`Ticket | Step | Time | **TicketDuration**
--------------------------------------
1 1 5:30 15
1 2 5:33 15
1 3 5:45 15
2 1 6:00 10
2 2 6:05 10
2 3 6:10 10
[ticketDuration] is a calculated column I added on my own. Now I'm trying to create a measure for the [AverageTicketDuration] so that it returns 12.5 minutes for the table above{ (15+10)/2 }. I haven't got a clue how to use DAX to produce the results. Please help!
What you are looking for is the AVERAGEX function, which has the following definition AVERAGEX(<table>,<expression>)
The idea being that it will iterate though each row of a defined table applying your calculation, then average the results.
In the below example, I use Table1 as the table name.
To start with to iterate along tickets we would use the following VALUES( Table1[ticket]) which will return the unique values in the ticket column.
Then assuming that your ticket duration is always the same within a ticket ID, the aggregation method used in the expression would be Average(Table1[Ticket]). Since for example of ticket 1, (15 + 15 + 15)/3 = 15
Put together the measure would look like below:\
measure:=AVERAGEX( VALUES( Table1[ticket]), AVERAGE(Table1[Ticket Duration]))
The result when dropped into a pivot using your sample data.
We have a particular drug that comes in different strengths. In my crystal report I'd like to display the number of times a given strength occurs in a dataset (planned treatments for patients). For example:
Strength Occurances
500 2
600 5
700 0
800 7
How could I easily do this?
You would required a formula to count.
create a formula like below
if {mytable.field} = 'xxx' then
{mytable.field};
then
count({formula});
I,m trying to make a query for this table which it have the following columns.
from , to, Range with values like
1, 100, A:
101,200, B:
201,300, C:
The columns are integer.
a user is going to give a number and I have to get on which rate is. Let say, the user send 105, I know that with a query I can get that it is on range B. But the problem is that sometimes users do not know the complete number that is going to be sent. Let say they only know the first two digits of the number, something like 10. I have to return all the possibilities that could involve l0. Let say, 10-101-1001-10001. The problem is that If I use LIKE I will not receive all the values because I do not have them in a column.
Any ideas how i can do this?
Just for your case (give first two digits), you can use substr to pick the first two digits of FROM and corresponding head of TO (length(to||'')-length(from||'')+2). Then compare them with user input to find the range.
With this query, you can get all ranges which contain number like what user send.
Use user input of 12 as an example, result will be ranges contain number like '12%':
select from, to, range from
(select Integer(substr(from,1,2)) substr_from,
Integer(substr(to,1,length(to||'')-length(from||'')+2) substr_to,
from, to, range
from your_table)
where (substr_from<=12 and substr_to>=12)
or (from<=12 and to>=12)
Below table will show the values of from, to, substr_from, substr_to
1 100 1 100
101 200 10 20
201 300 20 30
...................
901 1000 90 100
1001 1100 10 11
1101 1200 11 12
...................
9901 10000 99 100
10001 10100 10 10
10101 10200 10 10
...................
Since input is 12, these ranges will be returned: 1-100,101-200,1101-1200,1201-1300...
Of course this won't work if given digits is in the middle.