@@ -348,13 +348,13 @@ get_SDA_property <-
348348 }
349349
350350 # define several helper methods
351- .property_dominant_condition_category <- function (property , miscellaneous_areas = FALSE ) {
352- sprintf(" (SELECT TOP 1 %s FROM mapunit AS mu
351+ .property_dominant_condition_category <- function (property , miscellaneous_areas = FALSE , sqlite_dialect = FALSE ) {
352+ sprintf(" (SELECT %s %s FROM mapunit AS mu
353353 INNER JOIN component ON component.mukey = mapunit.mukey AND mapunit.mukey = mu.mukey %s
354- GROUP BY %s, comppct_r ORDER BY SUM(comppct_r) over(partition by %s) DESC) AS %s" ,
355- property ,
354+ GROUP BY %s, comppct_r ORDER BY SUM(comppct_r) over(partition by %s) DESC%s ) AS %s" ,
355+ ifelse(isTRUE( sqlite_dialect ), " " , " TOP 1 " ), property ,
356356 ifelse(miscellaneous_areas , " " ," AND component.compkind != 'Miscellaneous area'" ),
357- property , property , property )
357+ property , property , ifelse(isTRUE( sqlite_dialect ), " LIMIT 1 " , " " ), property )
358358 }
359359
360360 .property_min_max_CTE <- function (property , top_depth , bottom_depth , WHERE , FUN , include_minors = TRUE , miscellaneous_areas = FALSE , sqlite_dialect = FALSE ) {
@@ -371,15 +371,16 @@ get_SDA_property <-
371371 ifelse(miscellaneous_areas , " " ," AND component.compkind != 'Miscellaneous area'" ),
372372 paste(paste0(FUN , " (" , property , " ) AS " , property ), collapse = " ," ))
373373 }
374- .property_min_max <- function (property , top_depth , bottom_depth , WHERE , FUN , include_minors = TRUE , miscellaneous_areas = FALSE ) {
374+
375+ .property_min_max <- function (property , top_depth , bottom_depth , WHERE , FUN , include_minors = TRUE , miscellaneous_areas = FALSE , sqlite_dialect ) {
375376 sprintf(" SELECT mapunit.mukey, areasymbol, musym, muname, %s
376377 INTO #funagg
377378 FROM legend
378379 INNER JOIN mapunit ON mapunit.lkey = legend.lkey AND %s
379380 LEFT JOIN component ON component.mukey = mapunit.mukey %s %s
380381 SELECT mukey, areasymbol, musym, muname, %s FROM #funagg
381382 GROUP BY mukey, areasymbol, musym, muname" ,
382- paste(sapply(property , function (x ) .property_min_max_subquery(x , top_depth , bottom_depth , FUN = FUN , miscellaneous_areas = miscellaneous_areas )), collapse = " , " ),
383+ paste(sapply(property , function (x ) .property_min_max_subquery(x , top_depth , bottom_depth , FUN = FUN , miscellaneous_areas = miscellaneous_areas , sqlite_dialect = sqlite_dialect )), collapse = " , " ),
383384 WHERE ,
384385 ifelse(include_minors , " " ," AND component.majcompflag = 'Yes'" ),
385386 ifelse(miscellaneous_areas , " " ," AND component.compkind != 'Miscellaneous area'" ),
@@ -422,10 +423,10 @@ get_SDA_property <-
422423 MISCAREAS <- ifelse(miscellaneous_areas , " " , " AND component.compkind != 'Miscellaneous area'" )
423424
424425 DOMINANT <- ifelse(dominant , paste0(
425- " AND component.cokey = (SELECT " , ifelse(! sqlite_dialect , " TOP 1 " , " " ), " c2.cokey FROM component AS c2
426+ " AND component.cokey = (SELECT " , ifelse(isTRUE( sqlite_dialect ) , " " , " TOP 1 " ), " c2.cokey FROM component AS c2
426427 INNER JOIN mapunit AS mm1 ON c2.mukey = mm1.mukey AND c2.mukey = mapunit.mukey " ,
427428 gsub(" component" , " c2" , MISCAREAS ),
428- " ORDER BY c2.comppct_r DESC, c2.cokey " , ifelse(sqlite_dialect , " LIMIT 1" , " " ), " )" ), " " )
429+ " ORDER BY c2.comppct_r DESC, c2.cokey" , ifelse(isTRUE( sqlite_dialect ) , " LIMIT 1" , " " ), " )" ), " " )
429430
430431 PROPERTY <- paste(property , collapse = " , " )
431432
@@ -599,7 +600,8 @@ get_SDA_property <-
599600 WHERE ,
600601 dominant = FALSE ,
601602 include_minors = FALSE ,
602- miscellaneous_areas = FALSE ) {
603+ miscellaneous_areas = FALSE ,
604+ sqlite_dialect ) {
603605
604606 n <- seq_along(property )
605607 stopifnot(n > 0 )
@@ -635,7 +637,7 @@ get_SDA_property <-
635637 WHERE ,
636638 ifelse(include_minors , " " , " AND component.majcompflag = 'Yes'" ),
637639 ifelse(miscellaneous_areas , " " ," AND component.compkind != 'Miscellaneous area'" ),
638- ifelse(dominant , paste0(" AND component.cokey = (SELECT TOP 1 c2.cokey FROM component AS c2
640+ ifelse(dominant , paste0(" AND component.cokey = (SELECT " , ifelse(isTRUE( sqlite_dialect ), " " , " TOP 1 " ), " c2.cokey FROM component AS c2
639641 INNER JOIN mapunit AS mm1 ON c2.mukey = mm1.mukey AND c2.mukey = mapunit.mukey " ,
640642 ifelse(miscellaneous_areas , " " ," AND c2.compkind != 'Miscellaneous area'" ),"
641643 ORDER BY c2.comppct_r DESC, c2.cokey)" ), " " ),
@@ -705,9 +707,9 @@ get_SDA_property <-
705707 paste(sprintf(" #last_step2.%s" , property ), collapse = " , " )))
706708 }
707709
708- .property_dominant_component_numeric <- function (property , top_depth , bottom_depth , WHERE , FUN , miscellaneous_areas = FALSE ) {
710+ .property_dominant_component_numeric <- function (property , top_depth , bottom_depth , WHERE , FUN , miscellaneous_areas = FALSE , sqlite_dialect = FALSE ) {
709711 # dominant component numeric is a more specific case of weighted average
710- .property_weighted_average (property , top_depth , bottom_depth , WHERE , dominant = TRUE , include_minors = TRUE , miscellaneous_areas = miscellaneous_areas )
712+ .property_weighted_average_CTE (property , top_depth , bottom_depth , WHERE , dominant = TRUE , include_minors = TRUE , miscellaneous_areas = miscellaneous_areas , sqlite_dialect = sqlite_dialect )
711713 }
712714
713715 # create query based on method
@@ -718,14 +720,15 @@ get_SDA_property <-
718720 FROM legend
719721 INNER JOIN mapunit ON mapunit.lkey = legend.lkey AND %s
720722 INNER JOIN component ON component.mukey = mapunit.mukey AND
721- component.cokey = (SELECT TOP 1 c1 .cokey FROM component AS c1
723+ component.cokey = (SELECT %sc1 .cokey FROM component AS c1
722724 INNER JOIN mapunit AS mu ON component.mukey = mu.mukey AND
723725 c1.mukey = mapunit.mukey %s
724- ORDER BY c1.comppct_r DESC, c1.cokey)" ,
726+ ORDER BY c1.comppct_r DESC, c1.cokey %s )" ,
725727
726728 paste(sapply(agg_property , function (x ) sprintf(" %s AS %s" , x , x )), collapse = " , " ),
727- WHERE ,
728- ifelse(miscellaneous_areas , " " ," AND c1.compkind != 'Miscellaneous area'" )),
729+ WHERE , ifelse(isTRUE(sqlite_dialect ), " " , " TOP 1 " ),
730+ ifelse(miscellaneous_areas , " " ," AND c1.compkind != 'Miscellaneous area'" ),
731+ ifelse(isTRUE(sqlite_dialect ), " LIMIT 1" , " " )),
729732
730733 # weighted average (weighted_average handles vector agg_property)
731734 " WEIGHTED AVERAGE" = .property_weighted_average_CTE(agg_property , top_depth , bottom_depth , WHERE , include_minors = include_minors , miscellaneous_areas = miscellaneous_areas , dominant = FALSE , sqlite_dialect = sqlite_dialect ),
@@ -734,23 +737,24 @@ get_SDA_property <-
734737 " MIN/MAX" = .property_min_max_CTE(agg_property , top_depth , bottom_depth , WHERE , FUN , include_minors = include_minors , miscellaneous_areas = miscellaneous_areas , sqlite_dialect = sqlite_dialect ),
735738
736739 # dominant component (numeric) (.dominant_component_numeric handles vector agg_property)
737- " DOMINANT COMPONENT (NUMERIC)" = .property_dominant_component_numeric(agg_property , top_depth , bottom_depth , WHERE , miscellaneous_areas ),
740+ " DOMINANT COMPONENT (NUMERIC)" = .property_dominant_component_numeric(agg_property , top_depth , bottom_depth , WHERE , miscellaneous_areas , sqlite_dialect = sqlite_dialect ),
738741
739742 # dominant condition
740743 " DOMINANT CONDITION" =
741744 sprintf(" SELECT mapunit.mukey, areasymbol, musym, muname, %s
742745 FROM legend
743746 INNER JOIN mapunit ON mapunit.lkey = legend.lkey AND %s
744747 INNER JOIN component ON component.mukey = mapunit.mukey AND
745- component.cokey = (SELECT TOP 1 c1 .cokey FROM component AS c1
748+ component.cokey = (SELECT %sc1 .cokey FROM component AS c1
746749 INNER JOIN mapunit AS mu ON component.mukey = mu.mukey AND
747750 c1.mukey = mapunit.mukey %s
748- ORDER BY c1.comppct_r DESC, c1.cokey)
751+ ORDER BY c1.comppct_r DESC, c1.cokey%s )
749752 GROUP BY areasymbol, musym, muname, mapunit.mukey, component.cokey, compname, comppct_r
750753 ORDER BY mapunit.mukey, areasymbol, musym, muname, comppct_r DESC, component.cokey" ,
751- paste(sapply(agg_property , .property_dominant_condition_category ), collapse = " , " ),
752- WHERE ,
753- ifelse(miscellaneous_areas , " " ," AND c1.compkind != 'Miscellaneous area'" )),
754+ paste(sapply(agg_property , .property_dominant_condition_category , sqlite_dialect = sqlite_dialect ), collapse = " , " ),
755+ WHERE , ifelse(isTRUE(sqlite_dialect ), " " , " TOP 1 " ),
756+ ifelse(miscellaneous_areas , " " ," AND c1.compkind != 'Miscellaneous area'" ),
757+ ifelse(isTRUE(sqlite_dialect ), " LIMIT 1" , " " )),
754758
755759 # NO AGGREGATION
756760 " NONE" = sprintf(" SELECT mapunit.mukey, component.cokey, areasymbol, musym, muname,
0 commit comments