Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 用max()选择入_Sql_Sql Server_Insert_Max - Fatal编程技术网

Sql 用max()选择入

Sql 用max()选择入,sql,sql-server,insert,max,Sql,Sql Server,Insert,Max,我有一个基本查询,用于确定表中某列的最大值: select A.revenue_code_id, max(A.revenue_code_version) from rev_code_lookup A group by A.revenue_code_id 这将导致约580行(整个表有2400多行) 这对于我的查询结果来说很好,但我不知道如何根据最大值将580行插入到新的able中。我意识到这不是正确的代码,但我想的是这样的: select * into new_table from rev_co

我有一个基本查询,用于确定表中某列的最大值:

select A.revenue_code_id, max(A.revenue_code_version) from rev_code_lookup A
group by A.revenue_code_id
这将导致约580行(整个表有2400多行)

这对于我的查询结果来说很好,但我不知道如何根据最大值将580行插入到新的able中。我意识到这不是正确的代码,但我想的是这样的:

select * into new_table from rev_code_lookup where max(revenue_code_version)

无论选择的复杂性如何,在另一个表中插入的方式始终相同:

insert into table
[unbeliavablycomplicatedselecthere]
因此,在你的情况下:

insert into new_table
select A.revenue_code_id, max(A.revenue_code_version) from rev_code_lookup A
group by A.revenue_code_id
同样,如果需要创建一个全新的表,请首先执行以下操作:

CREATE TABLE new_table
AS
select A.revenue_code_id, max(A.revenue_code_version) from rev_code_lookup A
group by A.revenue_code_id

这将创建相应的表架构,然后您可以执行上一个查询以插入数据。

在另一个表中的插入方式始终相同,无论您选择的复杂性如何:

insert into table
[unbeliavablycomplicatedselecthere]
因此,在你的情况下:

insert into new_table
select A.revenue_code_id, max(A.revenue_code_version) from rev_code_lookup A
group by A.revenue_code_id
同样,如果需要创建一个全新的表,请首先执行以下操作:

CREATE TABLE new_table
AS
select A.revenue_code_id, max(A.revenue_code_version) from rev_code_lookup A
group by A.revenue_code_id

这将创建相应的表架构,然后您可以执行上一个查询来插入数据。

您可以使用
row\u number()
函数来获取所需的数据。结合另一个答案,将结果插入到一个表中(作为示例,我已经编写了两个额外的列):


您可以使用
行编号()
函数获取所需数据。结合另一个答案,将结果插入到一个表中(作为示例,我已经编写了两个额外的列):


可能的重复项不适用于Oracle,下次我一定要指定SQL server以避免混淆。谢天谢地,可能的副本不适用于Oracle,下次我一定要指定SQL server以避免混淆。ThanksOK,如果我还有两个字段要添加到insert语句中呢?源表中总共有4个字段,一个是文本,另一个是日期。但是我需要根据revenue\u code\u version的最大值进行插入。正如我所说的,无论查询有多复杂,模式都是一样的。因此,只需更改您的查询,并将其添加到
create table as
以及
insert-in
的末尾。当我执行以下操作时,我将所有行从源插入到目标中,而不仅仅是最大行。我显然出了问题……'选择A.revenue\u code\u id,max(revenue\u code\u version)作为revenue\u code\u version,description,last\u updated to revenue\u code2,从revenue\u查找组中按A.revenue\u code\u id,description,last\u updated order by max(revenue\u code\u version)描述,我刚刚意识到,您的问题不在于INSERT INTO的语法,而在于max。在这种情况下,请参见@Laurence的答案。好的,如果我有两个其他字段要添加到INSERT语句中,该怎么办?源表中总共有4个字段,一个是文本,另一个是日期。但是我需要根据revenue\u code\u version的最大值进行插入。正如我所说的,无论查询有多复杂,模式都是一样的。因此,只需更改您的查询,并将其添加到
create table as
以及
insert-in
的末尾。当我执行以下操作时,我将所有行从源插入到目标中,而不仅仅是最大行。我显然出了问题……'选择A.revenue\u code\u id,max(revenue\u code\u version)作为revenue\u code\u version,description,last\u updated to revenue\u code2,从revenue\u查找组中按A.revenue\u code\u id,description,last\u updated order by max(revenue\u code\u version)描述,我刚刚意识到你的问题不在于插入到的语法,而在于max。在这种情况下,请参见@Laurence的答案。谢谢,这就是我所需要的。里面有一些东西我以前没见过/用过,所以我要去了解它们。谢谢,这就是我所需要的。这里有一些我以前没有见过/使用过的东西,所以我将学习它们。