I have the following SQL which is written in Access 2003. Now, I have to develop the prj with SQL Server2008 and occur error when running this SQL at SQL Server2008 because First() function of Access cannot recogize in SQL Server 2008. At SQL Server 2008, I tried with TOP() function but I still cannot slove it. Please help me.
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
I think you'll need to use a Windowing Function
You replace your tables with versions that have Row Number on them
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.