2

我有2個表 - 1與父記錄,1與子記錄。對於每個父記錄,我試圖用MAX(SalesPriceEach)返回單個子記錄。從每個父行的子項的SQL返回最大值

此外,我想只有當有超過1個子記錄時返回一個值。

parent - SalesTransactions table: 
+-------------------+---------+ 
|SalesTransaction_ID| text | 
+-------------------+---------+ 
| 1     | Blah | 
| 2     | Blah2 | 
| 3     | Blah3 | 
+-------------------+---------+ 

child - SalesTransactionLines table 
+--+-------------------+---------+--------------+ 
|id|SalesTransaction_ID|StockCode|SalesPriceEach| 
+--+-------------------+---------+--------------+ 
| 1| 1    | 123 | 99   | 
| 2| 1    | 35 | 50   | 
| 3| 2    | 15 | 75   | 
+--+-------------------+---------+--------------+ 


desired results 
+-------------------+---------+--------------+ 
|SalesTransaction_ID|StockCode|SalesPriceEach| 
+-------------------+---------+--------------+ 
| 1    | 123 | 99   | 
| 2    | 15 | 75   | 
+-------------------+---------+--------------+ 

我發現了一個非常類似的問題here,並根據我對答案的查詢,但我沒有看到我期望的結果。

WITH max_feature AS (
    SELECT c.StockCode, 
      c.SalesTransaction_ID, 
      MAX(c.SalesPriceEach) as feature 
    FROM SalesTransactionLines c 
GROUP BY c.StockCode, c.SalesTransaction_ID) 
    SELECT p.SalesTransaction_ID, 
      mf.StockCode, 
      mf.feature 
    FROM SalesTransactions p 
LEFT JOIN max_feature mf ON mf.SalesTransaction_ID = p.SalesTransaction_ID 

該查詢的結果爲每個父項返回多行,甚至不是最先的值!

回答

2
select stl.SalesTransaction_ID, stl.StockCode, ss.MaxSalesPriceEach 
from SalesTransactionLines stl 
inner join 
(
    select stl2.SalesTransaction_ID, max(stl2.SalesPriceEach) MaxSalesPriceEach 
    from SalesTransactionLines stl2 
    group by stl2.SalesTransaction_ID 
    having count(*) > 1 
) ss on (ss.SalesTransaction_ID = stl.SalesTransaction_ID and 
     ss.MaxSalesPriceEach = stl.SalesPriceEach) 

,或者:

SELECT stl1.* 
FROM SalesTransactionLines AS stl1 
LEFT OUTER JOIN SalesTransactionLines AS stl2 
ON (stl1.SalesTransaction_ID = stl2.SalesTransaction_ID 
    AND stl1.SalesPriceEach < stl2.SalesPriceEach) 
WHERE stl2.SalesPriceEach IS NULL; 
0
SELECT SalesTransactions.SalesTransaction_ID, 
     SalesTransactionLines.StockCode, 
     MAX(SalesTransactionLines.SalesPriceEach) 
FROM SalesTransactions RIGHT JOIN SalesTransactionLines 
     ON SalesTransactions.SalesTransaction_ID = SalesTransactionLines.SalesTransaction_ID 
GROUP BY SalesTransactions.SalesTransaction_ID, alesTransactionLines.StockCode; 
0
select a.SalesTransaction_ID, a.StockCode, a.SalesPriceEach 
from SalesTransacions as a 
    inner join (select SalesTransaction_ID, MAX(SalesPriceEach) as SalesPriceEach 
       from SalesTransactionLines group by SalesTransaction_ID) as b 
    on a.SalesTransaction_ID = b.SalesTransaction_ID 
     and a.SalesPriceEach = b.SalesPriceEach 

子查詢返回反式ID及其最大值表,所以只是那些2倍的值

3

我與交易表本身加入吧知道我對這個派對遲了一年,但我總是更喜歡在這些情況下使用Row_Number。它解決了有兩行符合Max標準並確保只返回一行的問題:

with z as (
select 
    st.SalesTransaction_ID 
    ,row=ROW_NUMBER() OVER(PARTITION BY st.SalesTransaction_ID ORDER BY stl.SalesPriceEach DESC) 
    ,stl.StockCode 
    ,stl.SalesPriceEach 
from 
    SalesTransactions st 
    inner join SalesTransactionLines stl on stl.SalesTransaction_ID = st.SalesTransaction_ID 
) 
select * from z where row = 1 
+0

這是一種非常直接的方法來解決問題 – greg