我在這個主題上看到了很多問題和答案,但我很難理解我在做什麼錯誤,下面的查詢獲取我需要的所有信息,但是如果它有多行,當r_num是相同的,並且每個r_num的r_order是相同的,那麼對於每個r_num我只有一個結果,現在雖然這個調用給了我錯誤,有人請向我解釋Distinct
是如何工作的,我該如何使用它在此查詢實現我的目標是什麼?SQL Server不同,選擇所有行
SELECT DISTINCT po_num, r_num, r_order, vendor, order_date, received_by, received_date FROM(
SELECT p.id as po_num, r.id as r_num, r.rec_order as r_order, v.name as vendor, p.order_date, r.received_by, r.received_date
FROM Parts.dbo.po as p INNER JOIN
Parts.dbo.vendor as v ON v.id = p.vendor__id INNER JOIN
Parts.dbo.receipts as r ON r.id = 'R-'+CAST(p.id AS varchar)
GROUP BY p.id, r.id, r.rec_order, v.name, p.order_date, r.received_by, r.received_date) as tbl
GROUP BY r_num, r_order
以下是錯誤我得到
Msg 8120, Level 16, State 1, Line 1 Column 'tbl.po_num' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
你得到了什麼錯誤? – ekad 2014-11-06 16:11:37
編輯顯示錯誤 – 2014-11-06 16:13:00
錯誤說明了這一切......並且'DISTINCT'不是這裏的問題,它是你的分組 – Tanner 2014-11-06 16:14:35