Alternative for teradata functions in snowflake - snowflake-cloud-data-platform

Is there any alternative functions in snowflake for below teradata functions ?
DAYNUMBER_OF_CALENDAR(),
DayOccurrence_Of_Month(),
WeekNumber_Of_Calendar(),
MonthNumber_Of_Quarter(),
QuarterNumber_Of_Year(),

Can you pls try these functions ?
Treadata func - snowflake func
DAYNUMBER_OF_CALENDAR() - DAY( <date_or_timestamp_expr> )
DayOccurrence_Of_Month() - DAYOFMONTH( <date_or_timestamp_expr> )
QuarterNumber_Of_Year() - QUARTER( <date_or_timestamp_expr> )
WeekNumber_Of_Calendar() - WEEKOFYEAR( <date_or_timestamp_expr> )

This is one way for the DAYOCCURANCE_OF_MONTH() hoping there's a more elegant approach someone can come up with.
TRUNC(DATEDIFF(DAY,DATE_TRUNC('MONTH',CURRENT_DATE()),CURRENT_DATE())/7)

DayOccurrence_Of_Month() -> EXTRACT(DAY FROM <date>)
WeekNumber_Of_Calendar() -> DATE_PART(week, <date>)
MonthNumber_Of_Quarter() -> DATE_PART(month, <date>)
QuarterNumber_Of_Year() -> DATE_PART(quarter, <date>)

Related

Change format to hh:mm:sss

Can I adapt this formula to make the result into hh:mm:ss format instead of just a raw number?
Get hours from schedule
=ARRAYFORMULA(
SUM(
IFERROR(
IF(
--REGEXEXTRACT(A2:G2,"- (\d+:\d+)")<(--REGEXEXTRACT(A2:G2,"^(\d+:\d+)")),
1+REGEXEXTRACT(A2:G2,"- (\d+:\d+)")-
REGEXEXTRACT(A2:G2,"^(\d+:\d+)"),
REGEXEXTRACT(A2:G2,"- (\d+:\d+)")-
REGEXEXTRACT(A2:G2,"^(\d+:\d+)")))*
24))
try:
=ARRAYFORMULA(TEXT(SUM(IFERROR(IF(
--REGEXEXTRACT(A2:G2,"- (\d+:\d+)")<(
--REGEXEXTRACT(A2:G2,"^(\d+:\d+)")),
1+REGEXEXTRACT(A2:G2,"- (\d+:\d+)")-
REGEXEXTRACT(A2:G2,"^(\d+:\d+)"),
REGEXEXTRACT(A2:G2,"- (\d+:\d+)")-
REGEXEXTRACT(A2:G2,"^(\d+:\d+)")))*24), "[hh]:mm:ss"))
Suggestion
Based on your question & on your previous post, you want to sum up the time from range A2:G2 OR AR2:AX2 (as seen on your sample screenshot) in hh:mm:ss format. Perhaps you can try this tweaked function below:
=ARRAYFORMULA(TEXT(SUM(IFERROR(TIMEVALUE(TRANSPOSE(
{
ARRAYFORMULA(IFNA(REGEXEXTRACT(AR2:AX2,"- (\d+:\d+)"),0)),
ARRAYFORMULA(IFNA(REGEXEXTRACT(AR2:AX2,"^(\d+:\d+)"),0))
}
)))), "[hh]:mm:ss"))
This function was derived from an answer on this quite similar post:
Demonstration

Reading derived column expression

I am in need of assistance reading this confusing derived column expression which contains multiple Boolean expressions.
I've tried reading it multiple ways, but not sure which one is correct.
ISNULL(ContractNumber) ? (ISNULL(PaidLossAmount)
&& ISNULL(CaseReserveAmount)) ? NULL(DT_CY) :
(ISNULL(PaidLossAmount) ? 0 : PaidLossAmount) + (ISNULL(CaseReserveAmount)
? 0 : CaseReserveAmount) : PaidLossAmount
Could someone please advise on how this expression should be read? Thanks for your advice!
This is a nested if then else in the format of [Logical test] ? [Do this if true] : [Do this if false]
This is the formatted version.
ISNULL(ContractNumber)
?(ISNULL(PaidLossAmount) && ISNULL(CaseReserveAmount))
?NULL(DT_CY)
: (ISNULL(PaidLossAmount)
? 0
: PaidLossAmount)
+ (ISNULL(CaseReserveAmount)
? 0
: CaseReserveAmount)
: PaidLossAmount
Here's a decision tree:

filepatterns to exclude a match

Shake uses various forms of matching, filepath against pattern. Is there one which can be used to exclude a pattern?
Example:
I have three directories a, b, c (somewhere in directory x) and each needs html files. There is a rule
(dir <> "//*.html") %> \out -> do -- deal with a and b
which should only deal with the directories a and b and a similar rule to process the files in c.
Is there a way (perhaps using ?>) to separate the two rules for one
(\f -> "x" prefixOf f && not "x/c" prefixOf f) <> "//*.html")
(\f -> "x/x" prefixOf f ) <> "//*.html")
and the other similarly?
The recommended approach to use xx.a.html and xx.b.html is here not possible, because files are processed with other programs which expect the regular html extension.
Alternatively
The rule:
pattern %> action
Is equivalent to:
(pattern ?==) ?> action
So you can write:
(\x -> "//*.html" ?== x && not ("c//*.html" ?== x) ?> action
To match rules matching //*.html, but not c//*.html.
More generally, there is no requirement to use the FilePattern ?== operator at all, so arbitrary matching predicates can be used instead.
With the advice (especially the hint about using **/*.html) I have it working.
The path bakedD is a prefix of staticD and the first rule deals only with files in bakedD which are not in staticD, the other only with these:
(\x -> ((bakedD <> "**/*.html") ?== x)
&& not ((staticD <> "**/*.html") ?== x)) ?> \out -> do
let md = doughD </> (makeRelative bakedD $ out -<.> "md")
runErr2action $ bakeOneFileFPs md doughD templatesD out
(staticD <> "**/*.html" ) %> \out -> do
copyFileChanged (replaceDirectory out doughD) out
There is no overlap between the two rules.
I hope this is useful for others with the same type of issue.

eval(parse(text="f")) on shinypass.io

I want to write an app that lets the user enter a function using textInput, and then does something with the function. Here is a toy example:
shinyUI(fluidPage(
titlePanel("Test"),
sidebarLayout(
sidebarPanel(
textInput("fun","function:",value="x")
),
mainPanel(
uiOutput("text")
)
)
shinyServer(function(input, output) {
findMax <- reactive({
f <- function(x) eval(parse(text = input$fun), envir = list(x))
x < seq(0,1,length=100)
max(f(x))
})
output$text <- renderText( {
findMax()
})
})
))
and this works just fine when run on my computer locally. But when i submit it to shinyapps.io i get the error: object x not found. It seems there is a problem with the envir argument of eval, but i have not been able to find out what it is.
There is of course a lot of discussion on the eval(parse()) construct in general, so if anyone has a suggestion on how to do this (have the ability to type in an expression in a box and get it turned into a function) differently i would also be grateful.
Wolfgang
After trying a number of things i finally got this one to work:
instead of
f <- function(x) eval(parse(text = input$fun), envir = list(x))
use
eval(parse(text = paste("f <- function(x)",input$fun,sep="")))
i have no idea why both work on my computer locally but only the second works on shinyapps.io . Also, i would still be interested if anyone has a different way to do this.
Wolfgang

Why does ST_Union throw a "TopologyException: side location conflict"?

I'd like to understand the meaning of this error:
Error: ERROR: GEOSUnaryUnion: TopologyException: side location conflict at 509222.06405540626 166742.77073020922
SQLState: XX000
ErrorCode: 0
Given from the following query:
select ST_union(z.geom) from zaz z
Postgis version:
POSTGIS="2.1.0 r11822"
GEOS="3.3.6-CAPI-1.7.6"
PROJ="Rel. 4.8.0, 6 March 2012"
GDAL="GDAL 1.9.2, released 2012/10/08"
LIBXML="2.9.0" RASTER
The problem can be reproduced with this query:
select ST_AsText(ST_Union(z.geom)) from (
select ST_MPolyFromText('MULTIPOLYGON(((513571.981531803 166341.349774259,513116.037800698 166090.997380913,513119.013934713 166083.021341753,513122.34720481 166072.188213938,513124.013839858 166065.997855187,513126.037610988 166046.950597491,513126.037610988 166023.974842894,513117.942526467 165913.024566813,513116.990163582 165897.905806017,513104.966582162 165803.502835059,513104.60944608 165716.718767181,513124.013839858 164966.018723224,513054.967530709 164991.018248951,513040.682087437 164995.065791211,512771.639572476 165072.207184881,512658.070298462 165089.230671447,512620.332919151 165094.825803396,512222.007142577 165144.705809488,512088.319202621 165158.396025957,511825.467046411 165185.300277453,511437.260125488 165216.25207121,511039.767666438 165238.394508282,510637.275302243 165245.299139197,510278.115449307 165247.084819606,509844.671291355 165243.275368066,509480.630578634 165236.01360107,509195.516939992 165227.918516549,508870.642150909 167051.217259529,509104.804375213 166841.340288788,509222.064055406 166742.770730209,509415.631811745 166631.106181965,509727.887792604 166450.871506013,509849.6711965 166401.824817445,509922.407911828 166380.753788619,509986.811451913 166352.659083516,510105.975857876 166299.445807327,510225.259309199 166266.232151719,510464.302393288 166229.209044572,510544.777057055 166213.495056973,510668.227095999 166170.876817877,510762.034840154 166140.282160202,510828.938332812 166126.949079815,510890.365738883 166118.139723131,510959.05491195 166107.782776758,511018.33950153 166100.16387368,511066.076691131 166092.42592524,511097.742757051 166089.211700504,511281.786884542 166098.021057189,511590.471504583 166137.90125299,511730.825984733 166143.853521021,511766.896728995 166143.972566381,511806.419788715 166145.520156069,511905.108392654 166136.353663303,512001.535134742 166108.735139643,512202.95988488 166038.379331527,512434.145975169 165984.332737814,512507.120781218 165968.618750214,512581.643176955 165965.642616199,512654.379892283 166077.307164444,512784.496471421 166043.855418115,512787.948786879 166078.973799492,512787.948786879 166078.973799492,512767.830120937 166133.377529288,512774.020479688 166276.23196201,512799.020005415 166301.350533097,512836.400248644 166276.946234174,512874.018582594 166305.040939276,512838.066883692 166334.564188705,512845.090559968 166427.895751417,512806.996044575 166460.990361664,512846.757195016 166499.561058499,512858.899821797 166718.723567368,512820.329124962 166737.770825064,512833.900296071 166747.532544633,512821.043397126 166755.984765236,512877.708988772 166768.365482739,512893.899157814 166775.627249735,512930.68417424 166780.270018799,512977.469000957 166786.103241468,513181.393703668 166834.554703233,513269.011089071 166852.054371242,513314.605462182 166864.077952663,513568.886352428 166930.624309239,513908.760856946 167004.313387452,513964.593131069 167015.265560627,514336.967019032 167086.454686267,514356.252367449 167090.264137806,514374.10917154 167095.621179034,514433.274715759 166842.649787754,514423.036814747 166834.554703233,514403.037194166 166819.197851716,514302.443864457 166749.675361124,513950.664823878 166560.750373849,513571.981531803 166341.349774259)))') as geom
union
select ST_MPolyFromText('MULTIPOLYGON(((509658.841483455 166823.126348616,509700.031178223 166767.055983772,509737.292376092 166816.816944504,509780.982023433 166778.008156947,509821.338400677 166825.269165106,509862.051914003 166803.960045559,509727.887792604 166450.871506013,509415.631811745 166631.106181965,509480.987714716 166813.959855849,509442.417017881 166923.362542242,509390.751331379 167281.570032294,509425.988758117 167342.997438364,509448.012149829 167345.97357238,509478.963943585 167352.04488577,509503.963469312 167355.021019785,509530.986766168 167357.9971538,509555.986291895 167357.9971538,509583.961951636 167358.949516685,509588.009493897 167357.9971538,509603.009209333 167068.955018259,509651.579716458 167025.622507,509658.841483455 166823.126348616)),((509222.064055406 166742.770730209,509288.967548065 167023.955871951,509248.373080099 167078.002465664,509288.967548065 167023.955871951,509222.064055406 166742.770730209)))') as geom
) z
I'd also like to know if there is a way to "fix" one of the geometries to avoid
the error.
Strangely, if I create a table with a union of the two above selects, and then run an ST_Union, then it doesn't fail.
ST_MakeValid solves the problem.
The result can be passed to ST_Union:
ST_Union(st_makeValid(t.geom))
For the explanation, it could not be better than this one: PostGIS: Tips for power users.

Resources