0
查詢我有一個用例類似選擇行如何在SQL
a 3
a 4
a 5
a 6
a 5
b 3
b 5
b 3
如何得到這樣
a 4
a 5
b 5
b 3
輸出選擇a和b的HIGHT數量,但只有2行
這就是我現在寫的查詢,似乎它不工作
SELECT id, barcode, actualsku, inventorycount
FROM ( SELECT pallet.id AS id,
pallet.barcode AS barcode,
inventoryunit.sku AS actualsku,
Count(inventoryunit.id) AS inventorycount
FROM (SELECT *
FROM mft.asset
WHERE container_type = 'PALLET'
AND location_type = 'PRIME' :: mft.location_type
AND is_deleted = FALSE
AND (attributes ->> 'sku' :: text) IS NOT NULL) pallet
LEFT OUTER JOIN (SELECT *
FROM mft.asset
WHERE asset_type = 'INVENTORYUNIT' :: mft.asset_type
AND is_deleted = FALSE) inventoryunit
ON pallet.id = inventoryunit.parent_id
GROUP BY inventoryunit.sku,
pallet.id,
pallet.barcode,
pallet.attributes) test
WHERE (SELECT COUNT(*) FROM test as t
WHERE t.actualsku = test.actualsku
AND t.inventorycount <= test.inventorycount
) <= 2
你如何選擇兩行? –
結果背後的邏輯是什麼? – maSTAShuFu
這是一個重複的http://stackoverflow.com/questions/15969614/in-sql-how-to-select-the-top-2-rows-for-each-group – jakewins