简体   繁体   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

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.

相关问题 MySql SELECT * FROM table_name WHERE col_name IN(任何值) - MySql SELECT * FROM table_name WHERE col_name IN (any value) 分区对以下查询“从表名中选择*”有什么影响? - What is the effect of partition on below query “Select * from Table_name” 当您运行查询“ SELECT * FROM table_name WHERE column_name;”时,会发生什么情况? - What is happening when you run the query “SELECT * FROM table_name WHERE column_name;”? sp_whoisactive导致未知查询:“Insert Into <table_name> 选择*,%% bmk %%来自 <table_name> 选项(maxdop 1)“ - sp_whoisactive resulting an unknown query: “Insert Into <table_name> select *,%%bmk%% from <table_name> option (maxdop 1)” SQL:从@variable 中选择表的表值,例如'Select * from @Table_name' where @table_name=[DB_Name].[Schema_name].[table_name] - SQL : selecting the table values of table from @variable like 'Select * from @Table_name' where @table_name=[DB_Name].[Schema_name].[table_name] &#39;select * from [table_name]&#39;是一个游标吗? - 'select * from [table_name]' is secretly a cursor? SQL 服务器如何 Select * from table_name where in (@parameter) - SQL Server How to Select * from table_name where in (@parameter ) select * from table_name where column like&#39;&#39; - select * from table_name where column like '&nbsp' 如何从一个表中选择一个 table_name 值并根据条件从 table_name 表中删除记录? - How to pick a table_name value from one table and delete records from the table_name table based on a condition? SQL:如何使用信息架构中的Table_Name和Pivoted Column_Name构建选择查询 - SQL: How to build a select query with Table_Name and Pivoted Column_Name from Information Schema
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM