0

我有以下用 Access 2003 编写的 SQL。现在,我必须使用 SQL Server2008 开发 prj,并在 SQL Server2008 上运行此 SQL 时出现错误,因为 Access 的 First() 函数无法在 SQL Server 2008 中识别。在 SQL Server 2008 年,我尝试了 TOP() 函数,但我仍然无法解决它。请帮我。

  SELECT DISTINCTROW TableA.TCode,                  
  TableA.DCode,                 
  TableA.DepCode,                   
  TableA.ShouhinCode,                   
  First(TableB.TName) AS TNameFirstRecord,                  
  First(TableC.LDepName) AS LDepNameFirstRecord,                    
  First(TableD.ThingType) AS ThingTypeFirstRecord,                  
  First(TableA.GoodsName) AS GoodsNameFirstRecord,                  
  Sum(TableA.Amount) AS AmountData,                 
  Sum(TableA.MoneyAmount) AS MoneyAmountData,                   
  Sum(TableA.DetailedMoneyAmount) AS DetailedMoneyAmounData,                    
  Sum(TableA.SummaryMoneyAmount) AS SummaryMoneyAmountData,                 
  TableA.POSNo                  
 FROM (                 
 (TableA INNER JOIN TableC ON TableA.DCode = TableC.DCode)                  
 INNER JOIN TableB ON TableA.TCode = TableB.TCode                   
 )                  
  INNER JOIN TableD ON TableA.DepCode = TableD.DepCode                  
 GROUP BY TableA.TCode,                 
  TableA.DCode,                 
  TableA.DepCode,                   
  TableA.ShouhinCode,                   
  TableA.POSNo                  
 ORDER BY TableA.TCode,                 
  TableA.DCode,                 
  TableA.DepCode,                   
  TableA.ShouhinCode                    
4

1 回答 1

0

我认为您需要使用窗口功能

您将表替换为具有行号的版本

SELECT TableA.TCode,                  
    A.DCode,                 
    A.DepCode,                   
    A.ShouhinCode,                   
    B.TName AS TNameFirstRecord,                  
    C.LDepNamee AS LDepNameFirstRecord,                    
    D.ThingType AS ThingTypeFirstRecord,                  
    A.GoodsNameAS GoodsNameFirstRecord,                  
    Sum(A.Amount) AS AmountData,                 
    Sum(A.MoneyAmount) AS MoneyAmountData,                   
    Sum(A.DetailedMoneyAmount) AS DetailedMoneyAmounData,                    
    Sum(A.SummaryMoneyAmount) AS SummaryMoneyAmountData,                 
    A.POSNo                  
FROM 
    INNER JOIN (select row_number() over (partition by object_id ORDER BY Name DESC) as RowNumber, * from TableA) A                 
    INNER JOIN (select row_number() over (partition by object_id ORDER BY Name DESC) as RowNumber, * from TableC) C ON A.DCode = C.DCode                
    INNER JOIN (select row_number() over (partition by object_id ORDER BY Name DESC) as RowNumber, * from TableB) B ON A.TCode = B.TCode                   
    INNER JOIN (select row_number() over (partition by object_id ORDER BY Name DESC) as RowNumber, * from TableD) D ON A.DepCode = D.DepCode    

WHERE 
  A.RowNumber = 1 AND B.RowNumber = 1 AND C.RowNumber = 1 AND D.RowNumber = 1

GROUP BY A.TCode,                 
    A.DCode,                 
    A.DepCode,                   
    A.ShouhinCode,                   
    A.POSNo                  
ORDER BY A.TCode,                 
    A.DCode,                 
    A.DepCode,                   
    A.ShouhinCode    
于 2012-05-27T02:45:34.713 回答