在SQL Server2008中运行Access2003的相同SQL时发生SQL错误

问题描述:

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

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      
+1

为什么你需要通过使用不同的,当你组在底部? –

我认为你需要使用一个Windowing Function

您与对他们的行号版本替换你的表

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  
+0

对不起,当我用你教我的答案跑步时,它会发生错误nera INNER JOIN语句。你可以再教我一遍。 – khinlai

+0

请告诉我们错误是什么 - 将whol错误复制到消息 –

+0

中,错误消息是:1.'Inner'关键字附近的语句不正确,'C'附近的语句错误,'B'附近的语句错误,'' D' – khinlai