Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.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 server SQL Server按案例排序_Sql Server_Database_Sql Server 2014 - Fatal编程技术网

Sql server SQL Server按案例排序

Sql server SQL Server按案例排序,sql-server,database,sql-server-2014,Sql Server,Database,Sql Server 2014,我想使用此查询创建第四列: 但这个错误出现了: Invalid column name 'column3'. 我想要这个结果:红色标记的列: 您需要使用子查询外部的order by,如下所示: Select *, row_number() over(partition by column3 order by [Id]) as column4 from (select ID, column0, column1, column2, column3 = Case when co

我想使用此查询创建第四列:

但这个错误出现了:

Invalid column name 'column3'.
我想要这个结果:红色标记的列:


您需要使用子查询外部的order by,如下所示:

Select *, row_number() over(partition by 
 column3 order by [Id]) as column4 
 from (select ID, column0, column1, column2, column3 = Case when 
      column2 = '' then column1 else column2 end 
        from myTable) ti 
对于图像中的第4列,您需要按列3按id进行分区。如果按列0进行分区,则您将获得所有1,这样可以工作:

select *, row_number() over(partition by 
column0 order by [column3]) as column4 from 
(select ID, column0, column1, column2, column3 = Case when 
column2 = '' then column1 else column2 end from myTable) ti 

不按[column3]排序,按case语句本身排序。这不起作用,当column2=然后column1 else column2 end时,从select ID、column0、column1、column2、column3=case选择*,第4列的所有行都是1。只有西斯才会在短日期内先做一天。多亏了这项工作,第4列的条件就像你说的那样。
select *, row_number() over(partition by 
column0 order by [column3]) as column4 from 
(select ID, column0, column1, column2, column3 = Case when 
column2 = '' then column1 else column2 end from myTable) ti