我似乎無法按多個數據字段進行分組,並對特定的分組列進行求和。SELECT datafields with multiple groups and sum
我想將人員分組到客戶,然後將客戶分組爲價格,然後合計價格。合計金額(價格)最高的人應按升序列出。
例子:
table customer
-----------
customer | common_id
green 2
blue 2
orange 1
table invoice
----------
person | price | common_id
bob 2330 1
greg 360 2
greg 170 2
SELECT DISTINCT
min(person) As person,min(customer) AS customer, sum(price) as price
FROM invoice a LEFT JOIN customer b ON a.common_id = b.common_id
GROUP BY customer,price
ORDER BY person
我想要的結果是:
**BOB:**
Orange, $2230
**GREG:**
green, $360
blue,$170
The colors are the customer, that GREG and Bob handle. Each color has a price.
爲什麼'green'應該與'360'匹配並且用'170'與'blue'匹配?而不是相反呢? – 2012-01-31 18:35:41
顏色是顧客,GREG和Bob處理。每種顏色都有一個價格。 – tdjfdjdj 2012-01-31 18:39:35
那麼,你是說每個顧客都有一個價格? – 2012-01-31 18:41:45