logo
down
shadow

Passing column names dynamically for a record variable in PostgreSQL


Passing column names dynamically for a record variable in PostgreSQL

By : LeVernon Phipps
Date : November 24 2020, 05:47 AM
I hope this helps you . Using PostgreSQL, column values from a table for 1st record are stored in a record variable. for ex: let the variable be: recordvar , Working with this dummy table
code :


Share : facebook icon twitter icon
Dynamically sorting columns in dplyr via passing ordered vector with column names to select

Dynamically sorting columns in dplyr via passing ordered vector with column names to select


By : Mark
Date : March 29 2020, 07:55 AM
I wish did fix the issue. I'm using the code below to generate a simple summary table: , You're definitely on the right path.
code :
mt_sum <- mtcars %>%
  group_by(am) %>%
  summarise_each(funs(min, mean, median, max), mpg, cyl) %>%
  mutate(am = as.character(am)) %>%
  left_join(y = as.data.frame(table(mtcars$am),
                              stringsAsFactors = FALSE),
            by = c("am" = "Var1")) %>%
  .[, names(.)[order(names(.))]]
Dynamically Passing Table Names - using SQL Command from Variable

Dynamically Passing Table Names - using SQL Command from Variable


By : Buk Lau
Date : March 29 2020, 07:55 AM
I wish did fix the issue. Variable mappings at ForEach ADO Enumerator are mandatory, your mappings are ok. Please change datatype of User::IndivTable variable to string.
Change Variable 'User::CompanyInformation' expression to
code :
"select [name],[address],[address 2],[city],[phone no_],[fax no_],  
[VAT Registration no_],[registration no_],[business type] from " + [User::IndivTable]  
How to preserve column names when dynamically passing data frame columns to `aggregate`

How to preserve column names when dynamically passing data frame columns to `aggregate`


By : Peter Haßler
Date : March 29 2020, 07:55 AM
seems to work fine I will be assuming a general case, where you have multiple LHS (left hand sides) as well as multiple RHS (right hand sides).
code :
## S3 method for class 'data.frame'
aggregate(x, by, FUN, ..., simplify = TRUE, drop = TRUE)
## `LHS` and `RHS` are vectors of column names or numbers giving column positions
fun1 <- function (df, LHS, RHS){
  ## call `aggregate.data.frame`
  aggregate.data.frame(df[LHS], df[RHS], mean)
  }
## S3 method for class 'formula'
aggregate(formula, data, FUN, ...,
          subset, na.action = na.omit)
as.formula( paste(paste0("cbind(", toString(LHS), ")"),
                  paste(RHS, collapse = " + "), sep = " ~ ") )
LHS <- c("y1", "y2", "y3")
RHS <- c("x1", "x2")
as.formula( paste(paste0("cbind(", toString(LHS), ")"),
                  paste(RHS, collapse = " + "), sep = "~") )
# cbind(y1, y2, y3) ~ x1 + x2
fun2 <- function (df, LHS, RHS){
  ## ideally, `LHS` and `RHS` should readily be vector of column names
  ## but specifying vector of numeric positions are allowed
  if (is.numeric(LHS)) LHS <- names(df)[LHS]
  if (is.numeric(RHS)) RHS <- names(df)[RHS]
  ## make a formula 
  form <- as.formula( paste(paste0("cbind(", toString(LHS), ")"),
                      paste(RHS, collapse = " + "), sep = "~") )
  ## call `aggregate.formula`
  stats:::aggregate.formula(form, df, mean)
  }
set.seed(0)
dat <- data.frame(y1 = rnorm(10), y2 = rnorm(10),
                  x1 = gl(2,5, labels = letters[1:2]))

## "data.frame" method with `fun1`
fun1(dat, 1:2, 3)
#  x1          y1         y2
#1  a  0.79071819 -0.3543499
#2  b -0.07287026 -0.3706127

## "formula" method with `fun2`
fun2(dat, 1:2, 3)
#  x1          y1         y2
#1  a  0.79071819 -0.3543499
#2  b -0.07287026 -0.3706127

fun2(dat, c("y1", "y2"), "x1")
#  x1          y1         y2
#1  a  0.79071819 -0.3543499
#2  b -0.07287026 -0.3706127
Mysql select query for dynamically convert rows to column not working when passing variable while passing static value w

Mysql select query for dynamically convert rows to column not working when passing variable while passing static value w


By : Alan Mervin Guilas
Date : March 29 2020, 07:55 AM
hop of those help? I got the answer :D. I am posting answer if anyone found helpful.
In 3rd query value for prn was passing along with '' so removed quotes, it should like.
code :
where prn=$prn 
PostgreSQL plpgsql - variable column names

PostgreSQL plpgsql - variable column names


By : user7142869
Date : March 29 2020, 07:55 AM
this will help I found info here: Assign to NEW by key in a Postgres trigger
If we enable the module hstore by:
code :
CREATE EXTENSION hstore;
dynamic_column:='name';    
temp_sql_string:='"'||dynamic_column||'"=>"2222"';
NEW := NEW #= temp_sql_string::hstore;
shadow
Privacy Policy - Terms - Contact Us © ourworld-yourmove.org