我似乎无法按多个数据字段进行分组,并对特定的分组列进行求和。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