1
我想分頁SQL Server行。我在網上找到了一些教程,但我無法掌握每一個細節,這導致我在將their examples改編爲我現有的查詢時失敗了。如何爲使用GROUP BY和聚合時分頁SQL Server行
本教程的說明:
USE AdventureWorks2008R2
GO
SELECT
m.RowNr,
m.SalesOrderID,
m.SalesOrderDetailID,
m.OrderQty,
m.ProductID,
m.UnitPrice,
m.LineTotal,
m.rowguid,
m.ModifiedDate
FROM
(
SELECT
ROW_NUMBER() OVER(ORDER BY SalesOrderDetailID DESC) AS RowNr,
SalesOrderID,
SalesOrderDetailID,
OrderQty,
ProductID,
UnitPrice,
LineTotal,
rowguid,
ModifiedDate
FROM Sales.SalesOrderDetail AS tbl
)m
WHERE RowNr BETWEEN 1 AND 10;
我現有的(非分頁)查詢:
SELECT DISTINCT
MAX(i.ID) AS ID,
i.ItemLookupCode,
MAX(CAST(i.Notes AS varchar(max))) AS Notes,
MAX(CONVERT(varchar(30), i.Price, 1)) AS Price,
MAX(i.PictureName) AS PictureName
FROM Item AS i
LEFT JOIN nitroasl_pamtable AS n
ON i.ID = n.ItemID
WHERE (i.ID LIKE '%hdmi%' OR i.ItemLookupCode LIKE '%hdmi%' OR i.Notes LIKE '%hdmi%' OR i.Description LIKE '%hdmi%' OR i.ExtendedDescription LIKE '%hdmi%' OR n.ManufacturerPartNumber LIKE '%hdmi%' OR n.PAM_Keywords LIKE '%hdmi%')
AND (i.WebItem = 0 AND i.Price > 0)
GROUP BY i.ItemLookupCode
ORDER BY i.ItemLookupCode ASC;
我嘗試
/* Return Page */
SELECT
i.RowID,
MAX(i.ID) AS ID,
i.ItemLookupCode,
MAX(CAST(i.Notes AS varchar(max))) AS Notes,
MAX(CONVERT(varchar(30), i.Price, 1)) AS Price,
MAX(i.PictureName) AS PictureName
FROM (
SELECT
/* The following row is the line that is being blamed by the error */
ROW_NUMBER() OVER(ORDER BY ItemLookupCode) AS RowID,
MAX(ID) AS ID,
ItemLookupCode,
MAX(CAST(Notes AS varchar(max))) AS Notes,
MAX(CONVERT(varchar(30), Price, 1)) AS Price,
MAX(PictureName) AS PictureName
FROM Item AS tbl
) AS i
WHERE RowID BETWEEN 1 AND 15;
上面的查詢將返回以下錯誤:
Msg 8120, Level 16, State 1, Line 12 Column 'Item.ItemLookupCode' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
誰能誰是與SQL Server有更多的瞭解(2008 R2)幫我在這裏填寫的空白?
謝謝你。我不會想到這一點。以這種方式查看格式化查詢也有助於我瞭解究竟發生了什麼。謝謝! – derekmx271
因此,請注意,PHP的sqlsrv驅動程序不喜歡查詢。它成功了,但是沒有返回行嗎?......有沒有可能用不同的方法來格式化這個查詢,這個驅動程序可能會更好一點? – derekmx271
夥計,這個作品真棒!非常非常感謝你! – derekmx271