Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.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
C# 表合并到自身_C#_Sql_Tsql - Fatal编程技术网

C# 表合并到自身

C# 表合并到自身,c#,sql,tsql,C#,Sql,Tsql,我创建了一个表“Test” 下表数据如下 insert into test(correlation,data) values(1,'x0') insert into test(correlation,data) values(1,'x1') insert into test(correlation,data) values(2,'z1') insert into test(correlation,data) values(2,'z2') insert into test(correlation,

我创建了一个表“Test”

下表数据如下

insert into test(correlation,data) values(1,'x0')
insert into test(correlation,data) values(1,'x1')
insert into test(correlation,data) values(2,'z1')
insert into test(correlation,data) values(2,'z2')
insert into test(correlation,data) values(3,'a')
insert into test(correlation,data) values(4,'b')
insert into test(correlation,data) values(5,'c')
我需要在网页上显示数据,并将表本身连接到 关联和分页

例如,如果我有两个具有相同相关性的记录(1),我需要 将两行显示为一行,其中数据为当前数据和以前的数据。 在下面的示例中,当前数据为x1,之前的数据为x0

以前

 Correlation  Data
    1         x0    
    1         x1    
之后

若相关性只有一行,则结果中的上一个相关性将为空

目前我在Linq做了分页,它正在工作,但我担心将来它会花费很多钱 性能问题

sameone可以帮助我学习SQL吗

这个问题还有其他好的解决办法吗

;with C as
(
  select correlation,
         data,
         row_number() over(partition by correlation order by id desc) as rn
  from @test
  where SomeColumn > 10 -- Where clause goes here (if possible)
)  
select C1.correlation,
       C2.data as [Previous Data],
       C1.data as [Current Data]
from C as C1
  left outer join C as C2 
    on C1.correlation = C2.correlation and
       C2.rn = 2
where C1.rn = 1
结果:

correlation Previous Data Current Data
----------- ------------- ------------
1           x0            x1
2           z1            z2
3           NULL          a
4           NULL          b
5           NULL          c

因此,每个
相关性
只有两个
数据
条目,第一个条目是
以前的
,第二个条目是
当前的
??那么只有一个
数据
条目的相关性又如何呢?如果相关性只有一个数据条目,那么当前值和先前值将为空,那么您希望在相关性数据上透视数据。(PIvot是一个函数,您可以通过搜索找到几个示例)例如:@xQbert,但PIvot它是关于将一列的唯一值转换为新列的标题,但在这种情况下,这将如何应用???我考虑了PIvot表。但这不会发生here@GregoryNozik-我发帖有点快。更新答案。我的测试表是由许多表组成的。你认为它会运行得快吗?@GregoryNozik-查询的速度取决于很多内容、索引以及你拥有的数据量。如果有任何东西可以过滤所需的数据,最好将where子句放在CTE部分,以避免在实际不需要的行上排序。
C
是CTE。我将在其中添加一个where子句,它将发挥最大的作用:)您能帮我优化这个查询吗
;with C as
(
  select correlation,
         data,
         row_number() over(partition by correlation order by id desc) as rn
  from @test
  where SomeColumn > 10 -- Where clause goes here (if possible)
)  
select C1.correlation,
       C2.data as [Previous Data],
       C1.data as [Current Data]
from C as C1
  left outer join C as C2 
    on C1.correlation = C2.correlation and
       C2.rn = 2
where C1.rn = 1
correlation Previous Data Current Data
----------- ------------- ------------
1           x0            x1
2           z1            z2
3           NULL          a
4           NULL          b
5           NULL          c