Taking Average of Money in SQL/SQLPro - ios

I am working on a dataset, and I am having trouble trying to calculate the average of a column with dollar currencies in it. All the values, for example, are listed in the format of $12.00, $5.43, $1,234.00.....
Whenever I use the code below, it returns a value of 0 as the avg. In the same vein, it does the same thing for the SUM command as well.
SELECT AVG(bill_amount) FROM Trips WHERE trip_date >= '08/13/13';
Is there something wrong with the code I am using, because I think it's right.

You would appear to be storing values in the bill_amount column as text rather than as a number. You can convert them to a number and then use average. Something like this:
select avg(cast(case when substr(bill_amount, 1, 1) between '0' and '9' then bill_amount
else substr(bill_amount, 2)
end) as decimal(18, 4))

Related

How do I display the value of a cell in one column based on the value of a cell in a different column?

I have a simple table.
Column A are dates (rows 2 to 100) that reflect approximately the next 3 months.
Column C are percentages that range from 0 to 1 (i.e. 0% to 100%). The %s are derived from a =FORECAST function.
I want to query a date in column A based on a value of 100% in column C. However, the QUERY function does not work because it's reading the =FORECAST formula in column C rather than the text or value of "100%" itself. (In other words, if I remove the =FORECAST formula and type in the string "100%", the query works.
How can I pull the date value in Column A and keep the formulas used in column C?
This sounds like a lookup to find the value in column A on the first row where column C contains the value 100%. Try this:
=vlookup(100%, { C2:C, A2:A }, 2, false)
In the event multiple rows in column C may contain 100%, and you want to get all such dates, try this:
=filter(A2:A, C2:C = 100%)
In the event your forecast() numbers do not produce exact percentages but figures like 100.04%, use this:
=filter(Forecast!A2:A, round(Forecast!C2:C, 2) = 100%)
Thanks to everyone who contributed. My workaround was replacing the = sign with a > sign, as shown:
=QUERY(StageHist!A:H, "SELECT A WHERE C>.99")
since I couldn't figure out how to make the 100% figure an exact 100%. Apparently the =FORECAST formula is designed to give a result with 8 or 9 digits to the right of the decimal. (Although I'm sure that I could nest a =ROUNDing function inside of FORECAST and get an exact 1.0 value. (A project for another day!)

Google Sheets ARRAYFORUMLA that can show 0 if the value is less than or equal to zero. Otherwise show the value of that set of data

I have set of columns that I am attempting to calculate the combined total of those columns then subtract from that total 8, if the difference after 8 is equal to or less than 0 I want to only show zero in the column I am doing this formula in. For those who might ask, I am using the ARRAYFORUMLA cause I want this calculation to repeat as I add new rows, keeping the totals I am seeking on the same row as the calculation is being done onto. So far I have most of this working. Well up to the IF ELSE THEN type of portion. My attempt is/was
=if(LTE((B3:B)+(C3:C)-8,0),ARRAYFORMULA((B3:B)+(C3:C)), 0)
As long as I'm understanding you correctly, I believe this will work:
=if(lte(sum(B2:C)-8,0),0,sum(B2:C))
It's at the top of the row and sums columns B and C. You can add more columns easily this way by either changing B/C to something else or passing more columns in.
Sum-8 is greater than 0
Sum-8 is less than 0
If what you want to do is to add each row to the total but only if its sum is 8 or greater, then your original formula was nearly OK but the two parts of the IF statement should have been reversed
=sum(ArrayFormula(if(LTE((B3:B)+(C3:C)-8,0),0,(B3:B)+(C3:C))))
You could also take most of the brackets out
=sum(ArrayFormula(if(LTE(B3:B+C3:C-8,0),0,B3:B+C3:C)))
and this would also work
=sum(ArrayFormula(if(LTE(B3:B+C3:C,8),0,B3:B+C3:C)))
Short answer
ARRAYFORMULA that can show 0 if the value is less than or equal to zero. Otherwise show the value of that set of data:
=ArrayFormula(IF(LTE(A1:A3,0),0,A1:A3))
Explanation
The basic IF syntax requires the use of scalar values, to use it with arrays, ARRAYFORMULA is required as an outer function:
ARRAYFORMULA(IF(array_logical_expression,array_if_true,array_if_false))
For the specific case described on the body of the question, the corresponding formula is:
=ARRAYFORMULA(IF(LTE(B3:B+C3:C-8,0),0,B3:B+C3:C))
Reference
Google Docs editors Help
IF
ARRAYFORMULA
Using arrays in Google Sheets

Sumif only from positive number

currently I were already able to create a working forumlar with the SUMIF
=SUMIF(Units!K2:K183; "<=31.12.2015"; Units!X2:X183)
This is working fine and showing me the total sum of the the entries in row X2 to X183 if the date in row K2 to K183 is before 31.12.2015.
But now I want to add some extra:
I want to sum only the positive values in row X2 to X183. So in row X are just Euro amounts listed like e.g. 30,00 €. And a few lines have negative amounts in it like -20,00 €. Those negative values I want to exclude from this SUM.
Is this possible?
Perhaps with some kind of filter function?
Sorry for asking so fast. I found the solution myself:
=SUMIFS(Units!X2:X183; Units!K2:K183; "<=31.12.2015"; Units!X2:X183; ">0")
Just use the SUMIFS function and all works fine :-)

Ignoring blanks in averageifs when data has negative numbers and zero

I have a formula that will take the data from the past 30 days (column A are dates, column F has the data which is always either blank or above 0).
=AVERAGEIFS(F3:F, A3:A,">"&TODAY()-30, F3:F, ">0")
I need another, similar formula to apply to column H, however H will have negative numbers and 0. I would have thought this would work but it is simply not ignoring the blanks and the average does not match when I do a regular average and manually select the non blanks.
=AVERAGEIFS(H3:H, A3:A,">"&TODAY()-30, H3:H, "<>''")
What am I missing?
Apparently, it's just:
=AVERAGEIFS(H3:H, A3:A,">"&TODAY()-30, H3:H, "<>")
That unfortunately wasn't very clear in the documentation.

How to sum largest $n$ values in a range in Google Spreadsheet?

I have a list of values and I need to sum the largest 10 values (in a row). I found this but I can't figure it out/get it to work:
https://productforums.google.com/forum/#!topic/docs/A5jiMqkRLYE
let's say you want to sum the 10 highest values of the range E2:EP
then try:
=sumif(E2:P2, ">="&large(E2:P2,10))
and see if that works ?
EDIT: Maybe this is a better option ? This will only sum the 10 outputted by the array_constrain. Will only work in the new google sheets, though..
=sum(array_constrain(sort(transpose($A3:$O3), 1, 0), 10 ,1))
Can you see if this works ?
This works in old google sheets too:
sum(query(sort(transpose($A3:$O3), 1, false), "select * limit 10"))
Transpose puts the data in a column, sort sorts the data in a descending order and then query selects first 10 numbers.
Unfortunately, replacing sort with "order by" in a query statement does not work, because you can not reference a column in a range returned by transpose.
The sortn function seems to be just what you need.
From the documentation linked above, it "[r]eturns the first n items in a data set after performing a sort." The data set does not have to be sorted. It takes a bunch of optional parameters as it can sort on multiple columns.
SORTN(range, [n], [display_ties_mode], [sort_column1, is_ascending1], ...)
The interesting ones for your case are n, sort_column1, and is_ascending1. Specifically, your required formula would be
sum(sortn(transpose(A3:O3), 10, 0, 1, false)))
Some notes:
This assumes your data in A3:O3. You can replace it with your range.
transpose converts the data row to a data column as required by sortn.
10 is n, indicating the number of values that you require.
0 is the value for display_ties_mode. We are ignoring this value.
1 is the value of sort_column1, telling that we want to sort the first column (after transpose).
false tells sortn to sort descending and thus pick the largest values. The default is to pick the smallest.

Resources