sql查询获取楼层列表

时间:2015-01-08 23:37:51

标签: mysql sql

我有一张表

customer_id     floors
1234               12
1234                7
1234               -
1234               12
3456               1
3456               1
3476               2
3476               9

...
...

我希望得到一份显示类似内容的报告(按客户计算楼层数。请注意,如果该条目被客户复制到楼层,那么它应该只计算一次)

customers          no_of_floors
1234                   3
3456                   1
3476                   2
....
....
到目前为止,我有这个

SELECT count(customer_id) AS total, customer_id FROM floors GROUP BY customer_id, floors;

但这似乎不起作用。

任何想法

2 个答案:

答案 0 :(得分:2)

我的猜测是你只想要顾客计算不同的楼层:

select   customer_id,
         count(distinct floors)
from     floors
group by customer_id
having   count(distinct floors) between 6 and 9

答案 1 :(得分:2)

关闭!

SELECT count(distinct floors) AS total, customer_id FROM floors GROUP BY customer_id;