繁体   English   中英

SELECT MAX(col1) FROM<table_name> 子查询,在哪里<table_name>基于来自父查询的值</table_name></table_name>

[英]SELECT MAX(col1) FROM <table_name> sub-query, where <table_name> is based on a value from the parent-query

假设我们有一个名为“TABLES”的一维表:

| TABLE_NAMES |
|   'table1'  |
|   'table2'  |
|   'table3'  |
...

假设这些表中的每一个都有一个 INT 类型的属性“数量”。

是否存在一个查询可以为我提供这些表名中的每一个,以及该表的相应 MAX(数量)值。

目前这是我唯一的解决方案:

SELECT 'table1', MAX(quantity) FROM table1 UNION SELECT 'table2', MAX(quantity) FROM table2...

但是,这还不够,因为我事先不知道这些表是什么,因此需要从“TABLES”表本身推断它们。

下面的查询不起作用,但是我会想象我所追求的查询看起来像这样:

SELECT T.TABLE_NAME, (SELECT MAX(quantity) FROM T.TABLE_NAME) FROM TABLES AS T

感谢帮助,

谢谢

单个查询可能不会,但存储过程可能可以提供您想要的 output。

例如:

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); 

那么存储过程可以如下所示:

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 read_result_set();

我们得到一个如下所示的数组:

[
          "table2",
          300,
          "table3",
          3000,
          "table4",
          300000,
          "table5",
          300000
]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM