Snowflake 2 decimal places - snowflake-cloud-data-platform

I have a data column that is a number and it has nulls, dashes, and commas. I want to remove all and replace with 0.00. Also I have some numbers that are 11.4 instead of 11.40. How do I fix this?
also something either nvl or round adds the comma back in.
I tried round, I tried doing ;;decimal(20,2), I tried a replace to 0.00. Nothing seems to work.
round(nvl(replace(replace(aum_total,'-',0),',',''),0)) as aum_total

so you are converting dash to zero makes low sense, from a guessing intent.
and the coma point I assume to to remove thousands, so to add that back in needs to be done via TO_CHAR via formatting.
And the trailing zeros also is a formatting thing..
select
column1 as aum_total
,translate(aum_total, '-', '0') as t1
,replace(t1, ',', '') as r1
,zeroifnull(r1) as z1
,to_number(z1, 20, 2) as n1
,to_char(n1, '9,999,999.00') as result
from values
(null),
('1-7'),
('1,8'),
('11.4'),
('11.40'),
('11.400'),
('11.1234'),
('1,234.567');
gives:
AUM_TOTAL
T1
R1
Z1
N1
RESULT
null
null
null
0
0
0.00
1-7
107
107
107
107
107.00
1,8
1,8
18
18
18
18.00
11.4
11.4
11.4
11.4
11.4
11.40
11.4
11.4
11.4
11.4
11.4
11.40
11.4
11.4
11.4
11.4
11.4
11.40
11.1234
11.1234
11.1234
11.1234
11.12
11.12
1,234.567
1,234.567
1234.567
1,234.567
1,234.57
1,234.57
Thinking about this more, I now assume you mean you want to replace a string that is only a dash, with zero, as that is standard accounting, and not all dashes, thus not mess-up negative numbers, so swapping to regexp_replace, and the number cast can be skip it seems also!
select
column1 as aum_total
,trim(aum_total) as t1
,regexp_replace(t1, '^-$', '0') as r1
,replace(r1, ',', '') as r2
,zeroifnull(r2) as z1
,to_char(z1, '9,999,999.00') as result
from values
(null),
(' - '),
(' -10.123 '),
('-'),
('-10.123'),
('1,8'),
('11.4'),
('11.40'),
('11.400'),
('11.1234'),
('1,234.567');

Related

How to extract a handicap value from column based on 2 criteria

Good morning,
I have an issue with extracting the correct handicap value within the following table:
K L M
Handicap York Hereford
0 1287 1280
1 1285 1275
2 1280 1271
3 1275 1268
4 1270 1265
5 1268 1260
6 1265 1258
7 1260 1254
8 1255 1250
9 1253 1246
I also have these 2 lines of sample score/round data:
G H I
Round Score Handicap
York 1269 5
York 1270 4
Hereford 1269 XXX
Hereford 1270 XXX
If for instance someone on a York, gets a score of 1269, they should get a handicap of 5, which this formula achieves:
INDEX($K$7:$K$16,MATCH($H7,$L$7:$L$16,-1))+1
However this formula only works on the one column $L$7:$L$16
Similarly, the 2ns score is calculated with the following formula:
=INDEX($K$8:$K$17,MATCH($H8,$L$8:$L$17,-1))
What I'd like to do is, build that out so if I changed the round to a Hereford, with the exact same score, the cell would automatically calculate that the handicap should be 3.
Is this possible, maybe with an array?
Regards,
Andrew.
With ms365, try:
Formula in I2:
=XLOOKUP(H2,FILTER(L$2:M$11,L$1:M$1=G2),K$2:K$11,"NB",-1,-1)
I would avoid using OFFSET because it is a volatile function.
To select the appropriate column, you can use another MATCH:
MATCH($G7,$L$6:$M$6,0)
will return the column number. This makes it simple if you more than just York and Hereford columns.
Then, to return the matching line:
=MATCH($H7,INDEX($L$7:$M$16,0,MATCH($G7,$L$6:$M$6,0)),-1)
Note the use of 0 for the Row argument in the INDEX function which will return the entire column (all the rows).
Since your handicaps are sequential, as written this formula returns the same values as does yours. But I don't think it is correct since both formulas return 1 for a 1287 York.
You probably need to subtract one from the result of the formula.
=MATCH($H7,INDEX($L$7:$M$16,0,MATCH($G7,$L$6:$M$6,0)),-1)-1
Reference your lookup range with an OFFSET() function, and for the third parameter (which is column offset), use a MATCH() on the headers.
The formula on your first row would be:
=INDEX($K$7:$K$16,MATCH(H7,OFFSET($L$7:$M$16,0,MATCH(G7,$L$6:$M$6,0)-1,ROWS($L$7:$M$16),1),-1))+1

Add or subtract the first value of a column to the rest of the column (MATLAB)

I was wondering how to go about adding or subtracting the first value of my data to/from the rest of the column, so that the first row of data would be 0.
For instance, this:
A = [13.2 12.4 -11.7 6.3 -4.0
14.2 13.1 -9.2 8.2 -4.1
14.4 14.5 -7.6 10.0 -5.1];
Would change to:
0 0 0 0 0
1 0.7 2.5 1.9 0.1
1.2 2.1 4.1 3.7 1.1
I think I can check whether the first number is positive/negative by using sign() and choose whether to add or subtract this using an ifelse statement, but I am unsure how to apply this to each column individually (or if this is the best way!).
Many thanks in advance.
You actually need element-by-element operation, as the definition of bsxfun states. In your case it should be:
A = [13.2 12.4 -11.7 6.3 -4.0
14.2 13.1 -9.2 8.2 -4.1
14.4 14.5 -7.6 10.0 -5.1];
B=bsxfun(#minus,A,A(1,:))
B =
0 0 0 0 0
1.0000 0.7000 2.5000 1.9000 -0.1000
1.2000 2.1000 4.1000 3.7000 -1.1000
This is the result for your question description, but for the example that you add, I assume that you want the absolute values, so you need to add abs:
B=abs(bsxfun(#minus,A,A(1,:)))
B =
0 0 0 0 0
1.0000 0.7000 2.5000 1.9000 0.1000
1.2000 2.1000 4.1000 3.7000 1.1000
You can select the first row and subtract it from the matrix.
A = A - A(1, :)
Or for older versions of Matlab:
A = A - repmat(A(1, :), size(A, 1), 1)

Summing Previous N Rows using Where Statement

Having some problems finding an answer to what I think is a simple query but I'm very green with SQL:
YR MO ID FLAG RETURN
2001 01 1 1 3.00
2001 02 1 2 4.00
2001 03 1 3 -1.00
2001 04 1 4 1.00
2001 05 1 5 1.00
2001 06 1 6 1.00
2001 07 1 7 1.00
2001 08 1 8 1.00
2001 09 1 9 1.00
2001 10 1 10 1.00
2001 11 1 11 2.00
2001 12 1 12 1.00
2002 12 2 3 1.00
2002 04 2 0 0.05
I'd like a new column next to sum the previous 12 RETURN values WHERE FLAG = 12. Any help is greatly appreciated!
The data will be sorted by ID, then Year, and Month so it should be order sequentially.
The output would be (3+4+-1+1+1+1+1+1+1+1+2+1) = 16
I'd like the output (16) in the FLAG=12 row
Maybe a Windowed Function would fit the bill here:
SELECT *, CASE WHEN FLAG = 12 THEN SUM([RETURN]) OVER (PARTITION BY ID ORDER BY YR, MO ROWS BETWEEN 12 PRECEDING AND CURRENT ROW) ELSE NULL END
FROM SomeTable
ORDER BY ID, YR, MO
So, there are a couple issues with what you are attempting. First, you will need to either programmatically or administratively (through the UI) create the new column; the select call will not do this for you. Next, you need to be sure you want that data in your schema as it will be very 'odd' to have a column that sums flagged values. It seems as if you want to know that result but don't necessarily need to store it. If that is true (or can be made true), then I would suggest creating a select call that uses the 'sum', 'order by ... desc' (this means you need to know the ordering) and 'limit 12' functions. Given any row where the Flag is 12, you should be able get the result you want with a single call.
Just another note, since you've mentioned two different DBMSs, make sure you validate the SQL against both; I'm fairly certain you can find a generic request that will work in both systems. Good luck.

Finding minimum value in index(match) array [EXCEL]

This is my simple table
A B C
tasmania hobart 21
queensland brisbane 22
new south wales sydney 23
northern territory darwin 24
south australia adelaide 25
western australia perth 26
tasmania hobart 17
queensland brisbane 18
new south wales sydney 19
northern territory darwin 11
south australia adelaide 12
western australia perth 13
index match array formula:
=INDEX(A2:C9,MATCH(1,(H4=$A:$A)*(I4=$B:$B),0),3)
Basically A and B are my lookup criteria while C is the value I want to get. I want C to be the minimum value among the matched C value.
Ex.
If I have tasmania and hobart as my criteria, I would want to get 17 because it is the minimum value and not 21.
I tried nesting MIN inside the index match array (H4=$A:$A)*(I4=$B:$B)*(MIN($C:$C)) but it only results in errors
This is rather a MIN(IF... than a INDEX. Before SUMIF or COUNTIF was implemented in Excel even SUM(IF... or COUNT(IF... had to be used this way.
Since there is not a MINIFS until now, for this we must further use:
{=MIN(IF($A$1:$A$1000=H4,IF($B$1:$B$1000=I4,$C$1:$C$1000,NA())))}
This is an array formula. Input it into the cell without the curly brackets and press [Ctrl]+[Shift]+[Enter] to confirm. The curly brackets should then appear automatically.
With the AGGREGATE function as a standard formula,
=AGGREGATE(15, 6, C2:INDEX(C:C, MATCH(1E+99,C:C ))/((A2:INDEX(A:A, MATCH(1E+99,C:C ))=F2)*(B2:INDEX(B:B, MATCH(1E+99,C:C ))=G2)), 1)
    
As an older style standard formula, your original would look like,
=MIN(INDEX(C2:C13+((H4<>A2:A13)+(I4<>B2:B13))*1E+99, , ))

Working off of the results of 2 Conditional Formats

Another ? for you. How can I work off of the results of 2 Conditional Formats & have just the results of those conditions highlighted. The 2 Conditional Formats results are in (column C & G) & I need to have the results highlighted in (column A)... A's 3 arguments are as follows:
condition1 cell value equal to 0, No Format
condition2 formula is =$G27>=LARGE($G$27:$G$150,10), Bold Format
condition3 formula is =$C27>=LARGE($C$27:$C$150,10), Colored Red
Another quandry...
This is just like the fizzbuzz programmer test.
So, without writing the code for you: I'll recommend a loop through a 'listobject(table)' followed by setting the range values using '.interior.colorindex' or 'font.colorindex' properties.
OK, without using code...
- Format your table as a table using "Format as Table" function
- In the "Table" menu, select the "Total Row" checkbox
- Set your Total formula for Cols C and G as =LARGE([ColC],10) and =LARGE([ColG],10), respectively.
- In Conditional Formatting, set up two rules as follows:
- =$B28>=$D$[TotalRowlNumber]
- =$B28>=$C$[TotalRowNumber]
- You shouldn't need a condition for =0 since you are not changing any formats.
How does that work?
Here's a sample table:
ID Col A Col C Col G
____________________________
1 50.66 51.33 97.17
2 16.09 83.39 97.37
3 b71.94 69.77 28.06
4 21.60 20.59 21.14
5 33.62 65.58 39.21
6 21.96 34.59 17.99
7 br80.94 93.02 96.84
8 b70.53 37.53 29.60
9 32.06 37.38 0.15
10 br89.81 67.02 6.85
11 br89.76 64.65 74.00
12 47.94 46.06 1.71
13 b61.19 34.19 90.13
14 br79.11 35.77 86.97
15 39.89 79.15 77.88
16 br93.20 8.01 13.99
17 31.84 18.12 95.61
18 br99.78 19.99 3.89
19 38.94 32.12 18.56
20 13.17 22.23 61.82
21 br75.75 51.42 28.32
22 b55.89 49.93 76.30
23 br72.78 82.46 27.07
24 b57.20 31.26 76.90
25 6.46 6.85 2.78
Total 51.33 74.00
Use the LARGE() formula to get the 10th largest value in the TOTAL row.
Then reference this cell in the condition.
So, any number larger than 51.33 will be bold and any number larger than 74.00 will be bold red. (Note, I used random number generator, so the numbers may be good or bad.)
Also, I added a 'b' tag and an 'r' note where cells will be formatted bold and red, respectively.

Resources