SQL Query
SQL Query using Function
Summary: Runs the lookup process for pairs of age and sex values by running a SQL query on a JMP table, selecting specific columns and filtering by age and sex.
Code:
Names Default To Here (1);
myquery = Function( {vals, agevar, sexvar},
// Section below would normally be your SQL query against your database.
// this was just copy/pasted from doing it manually
New SQL Query(
Version( 130 ),
Connection( "JMP" ),
JMP Tables(
["data_table" => "$SAMPLE_DATA\data_table.jmp"]
),
QueryName( "mylookup" ),
Select(
Column( "height", "t1", Numeric Format( "Fixed Dec", "0", "NO", "" ) ),
Column( "weight", "t1", Numeric Format( "Fixed Dec", "0", "NO", "" ) )
),
From( Table( "data_table", Alias( "t1" ) ), Sample( First N( vals ) ) ), //'vals' variable added here
Where(
In List(
Column(
"age",
"t1",
Analysis Type( "Ordinal" ),
Numeric Format( "Fixed Dec", "0", "NO", "" )
),
{agevar}, //'age' variable added here
UI( SelectListFilter( ListBox, Base( "Categorical" ) ) )
) & In List(
Column( "sex", "t1" ),
{sexvar}, //'sex' variable added here
UI( SelectListFilter( ListBox, Base( "Categorical" ) ) )
)
)
) << Run
);
// test to make sure function works
dt = myquery (2, 12, "F");
close (dt, no save);
//make table with pairs to lookup
dtlookup = New Table( "combos",
Add Rows( 4 ),
New Column(
"age",
Numeric,
"Ordinal",
Format( "Fixed Dec", 5, 0 ),
Set Values( [12, 12, 13, 13] ),
Set Display Width( 53 )
),
New Column( "sex", Character, "Nominal", Set Values( {"F", "M", "F", "M"} ) )
);
//now run the lookup for each pair
for each row (dtlookup,
myquery (2, :age, :sex )
);
Code Explanation:
- Define
myqueryfunction. - Create new SQL query.
- Set connection to JMP.
- Specify JMP tables.
- Name the query "mylookup".
- Select columns "height" and "weight".
- From "data_table" table, sample first N rows.
- Filter by age and sex.
- Run the query.
- Test
myqueryfunction. - Close test table without saving.
- Create "combos" table.
- Add rows to "combos".
- Add "age" column with values.
- Add "sex" column with values.
- For each row in "combos", run
myquery.