[英]SELECT MAX(col1) FROM <table_name> sub-query, where <table_name> is based on a value from the parent-query
So lets say we have a one-dimensional table of tables named 'TABLES':假设我们有一个名为“TABLES”的一维表:
| TABLE_NAMES |
| 'table1' |
| 'table2' |
| 'table3' |
...
Presume each of these tables has an attribute 'quantity' of type INT.假设这些表中的每一个都有一个 INT 类型的属性“数量”。
Does there exist a single query that can give me each of these table names, along with a corresponding MAX(quantity) value for that table.是否存在一个查询可以为我提供这些表名中的每一个,以及该表的相应 MAX(数量)值。
Currently this is my only solution:目前这是我唯一的解决方案:
SELECT 'table1', MAX(quantity) FROM table1 UNION SELECT 'table2', MAX(quantity) FROM table2...
However, this will not suffice as I do not know what these tables are ahead of time, and will therefore need them inferred from the 'TABLES' table itself.但是,这还不够,因为我事先不知道这些表是什么,因此需要从“TABLES”表本身推断它们。
The below query does not work, however I would imaging the query I am after looks something like this:下面的查询不起作用,但是我会想象我所追求的查询看起来像这样:
SELECT T.TABLE_NAME, (SELECT MAX(quantity) FROM T.TABLE_NAME) FROM TABLES AS T
Appreciate the help,感谢帮助,
Thanks谢谢
A single query probably not, but a stored procedure probably can provide the output you want.单个查询可能不会,但存储过程可能可以提供您想要的 output。
For example:例如:
create or replace table TABLES_X(tables_names varchar(100));
insert into tables_x values ('table2');
insert into tables_x values ('table3');
insert into tables_x values ('table4');
insert into tables_x values ('table5');
insert into tables_x values ('table2');
insert into tables_x values ('table3');
insert into tables_x values ('table4');
insert into tables_x values ('table5');
insert into tables_x values ('table2');
insert into tables_x values ('table3');
insert into tables_x values ('table4');
insert into tables_x values ('table5');
create or replace table table2 (quantity int);
create or replace table table3 (quantity int);
create or replace table table4 (quantity int);
create or replace table table5 (quantity int);
insert into table2 values (100);
insert into table2 values (200);
insert into table2 values (300);
insert into table3 values (1000);
insert into table3 values (2000);
insert into table3 values (3000);
insert into table4 values (10000);
insert into table4 values (20000);
insert into table4 values (30000);
insert into table5 values (100000);
insert into table5 values (200000);
insert into table5 values (300000);
Then the stored procedure can look like this:那么存储过程可以如下所示:
create or replace procedure read_result_set()
returns array not null
language javascript
as
$$
var array_of_rows = [];
var my_sql_command = "SELECT DISTINCT(tables_names) FROM TABLES_X";
var statement1 = snowflake.createStatement( {sqlText: my_sql_command} );
var result_set1 = statement1.execute();
while (result_set1.next()) {
var col = result_set1.getColumnValue(1);
array_of_rows.push(col);
var sql_cmd = "SELECT MAX(quantity) FROM " + col;
var statement2 = snowflake.createStatement( {sqlText: sql_cmd} );
var result_set2 = statement2.execute();
while (result_set2.next()) {
var col2 = result_set2.getColumnValue(1);
array_of_rows.push(col2);
}
}
return array_of_rows
$$
;
Call the procedure:调用程序:
call read_result_set();
and we get an array that looks like this:我们得到一个如下所示的数组:
[
"table2",
300,
"table3",
3000,
"table4",
300000,
"table5",
300000
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.