Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.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 2005 - Fatal编程技术网

Sql server 从上一行收集数据的查询

Sql server 从上一行收集数据的查询,sql-server,sql-server-2005,Sql Server,Sql Server 2005,我有一个记录表,例如a公司下面的示例数据。Nr是TH-123、TH-456等等。。。我需要收集数据 Nr. CO.Nr Employee Resp Description Date 1 TH-123 ABC NULL HELLO 10.05.2010 2 TH-123 NULL

我有一个记录表,例如a公司下面的示例数据。Nr是TH-123、TH-456等等。。。我需要收集数据

Nr.       CO.Nr           Employee       Resp            Description       Date

1         TH-123          ABC            NULL              HELLO           10.05.2010
2         TH-123          NULL           S14               NULL            18.05.2010
3         TH-123          DEF                              NULL            13.05.2010
4         TH-456          XYZ            NULL              NULL             1.07.2010
5         TH-456          NULL           S19               SOME             NULL
6         TH-456                                           TEXT            08.05.2010
7         TH-456                        NULL                               28.05.2010
对于TH-123, 如果Nr.为最大值,则这是我需要从group by CO.Nr开始的记录,因此这是Nr为3的记录, 如果其他列中的值为NULL或空格,则转到Nr为2的记录上方的记录,即使该记录的值为NULL,在本例中,也转到Nr为1的记录上方的记录。 在3条记录中,我需要取最大日期。 对于上述数据,我需要输出为

      CO.Nr           Employee       Resp            Description       Date

      TH-123          DEF            S14               HELLO            18.05.2010
      TH-456          XYZ            S19               TEXT             01.07.2010

提前谢谢

您可以使用子选择来选择所需的记录,然后加入该记录。对于employees one,类似于以下内容(我将剩下的专栏作为练习):


你可以用很多方法

select [co.nr],
(select top(1) employee from mytable b where b.[co.nr]=a.[co.nr]  and 
                        employee is not null order by nr desc) as employee,
(select top(1) resp from mytable b where b.[co.nr]=a.[co.nr]  and 
                        resp is not null order by nr desc) as resp,
(select top(1) description from mytable b where b.[co.nr]=a.[co.nr]  and 
                        description is not null order by nr desc) as description,
(select max([date]) from mytable b  where b.[co.nr]=a.[co.nr]) as Date
from (
select distinct [co.nr] 
 from mytable ) as a

我想您的代码中需要一个存储过程或一些逻辑。一个简单的查询可能无法执行您想要的操作。感谢您的响应,当我执行此查询时,遇到的错误是“'FIRST'未被识别为内置函数的名称。”然后我将第一个替换为TOP 1,然后错误是“Employee”列在选择列表中无效,因为它不在聚合函数中,也不在GROUP BY子句中。@幸运的是,我已经编辑了我的答案,也许您可以使用第二个查询?谢谢..工作得很好。在一个select中,对于每个字段,我都需要编写一个选择top 1…嵌套选择是否有任何限制或性能问题?@lucky查看您的
EXPLAIN
结果,您可以告诉我。确保它正在使用索引,而不是首先尝试进行完全交叉连接。也就是说,如果您在
CO.Nr
Nr.
上有索引,您应该可以。(如果你要选择最长的日期,在[Date]上)谢谢。工作得很好。在一次选择中,对于每个字段,我都需要写一个select top 1…嵌套选择是否有任何限制或性能问题?是的,根据记录的数量,这不会很好地执行-因为我们必须根据需要进行4次额外选择。但是如果您在[co.nr]上有索引,nr应该会修复它。这可以通过单次扫描表格来实现,但必须使用更复杂的方法。
SELECT t.MyTable.[CO.Nr], 
       (SELECT TOP(1) x.Employee
        FROM MyTable x
        WHERE x.[CO.Nr] = t.[CO.Nr]
        AND x.Employee IS NOT NULL AND x.Employee <> ''
        ORDER BY [Nr.] DESC) as Employee
FROM MyTable t
GROUP BY t.[CO.Nr]
select [co.nr],
(select top(1) employee from mytable b where b.[co.nr]=a.[co.nr]  and 
                        employee is not null order by nr desc) as employee,
(select top(1) resp from mytable b where b.[co.nr]=a.[co.nr]  and 
                        resp is not null order by nr desc) as resp,
(select top(1) description from mytable b where b.[co.nr]=a.[co.nr]  and 
                        description is not null order by nr desc) as description,
(select max([date]) from mytable b  where b.[co.nr]=a.[co.nr]) as Date
from (
select distinct [co.nr] 
 from mytable ) as a