簡體   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