Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.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 2008中第一个_值()的替代项_Sql_Sql Server_Sql Server 2008 - Fatal编程技术网

sql server 2008中第一个_值()的替代项

sql server 2008中第一个_值()的替代项,sql,sql-server,sql-server-2008,Sql,Sql Server,Sql Server 2008,我有一个在Oracle和DB2中运行良好的查询。但它在SQLServer2008中不起作用,因为第一个值函数不适用于相同的。2008年有工作吗 select NameGuid, Name, AncestorGuid, ProductGuid, PathLength from ( select NameGuid, Name, AncestorGuid, ProductGuid, PathLength, -- take every

我有一个在Oracle和DB2中运行良好的查询。但它在SQLServer2008中不起作用,因为第一个值函数不适用于相同的。2008年有工作吗

select NameGuid, Name, AncestorGuid, ProductGuid, PathLength
from (
    select 
    NameGuid, 
    Name, 
    AncestorGuid, 
    ProductGuid, 
    PathLength, 
    -- take every row from original query with the same Name as this,
    -- order those rows by PathLength (and NameGuid to disambiguate)
    -- and return the NameGuid of the first row in that "partition"
    first_value(NameGuid) over (partition by Name order by PathLength asc, NameGuid asc) MinNameGuid
    from ( 
        ... your original query ...
    )
)
where 
-- return rows whose NameGuid is the same as the NameGuid calculated by first_value(...)
NameGuid = MinNameGuid

注意:该查询是对我以前的

的回答。您可以在SQL Server 2008中尝试此操作,以复制
第一个值
函数

;with CTE(NameGuid, Name, AncestorGuid, ProductGuid, PathLength) AS
(
  select 
  NameGuid, 
  Name, 
  AncestorGuid, 
  ProductGuid, 
  PathLength, 
-- take every row from original query with the same Name as this,
-- order those rows by PathLength (and NameGuid to disambiguate)
-- and return the NameGuid of the first row in that "partition"
-- first_value(NameGuid) over (partition by Name order by PathLength asc, NameGuid asc) MinNameGuid
 ROW_NUMBER() over (partition by Name order by PathLength asc, NameGuid asc) MinNameGuid
from ( 
    ... your original query ...
)a
)
Select c.NameGuid, c.Name, c.AncestorGuid, c.ProductGuid,
c.PathLength,c1.NameGUID
from CTE c
LEFT JOIN 
(SELECT NAMEGUID,Name from CTE where MinNameGuid = 1) C1
on 
c.Name = C1.Name
where c1.NAMEGUID is not NULL

您可以对子查询和
行编号()执行类似的操作。
我尝试用行编号()替换第一个值(),但命令行出现
错误:1列:0错误报告:SQL错误:ORDER BY子句在视图、内联函数、派生表、子查询和公共表表达式中无效,除非还指定了TOP或FOR XML。
这并不是那么简单-您需要计算子查询中的行数并加入到该子查询中。我没有一个完整的答案,但我想这可能会让你开始。