在mySQL select中划分两个计算值

时间:2015-07-17 14:34:54

标签: mysql

以下是我的查询:

SELECT  a.cartodb_id as cartdb_id, a.identifier AS store,
(SELECT count(*) FROM full_data_for_testing_deid_2) ,count(b.*) AS   customer_count 
FROM demo_locations_table a, full_data_for_testing_deid_2 b 
WHERE ST_DWithin(a.the_geom::geography,b.the_geom::geography,16093) 
GROUP BY a.cartodb_id

这是回来的:

| cartdb_id | store           | count | customer_count |
|-----------|-----------------|-------|----------------|
| 1         | Store Number: 1 | 32085 | 30             |
| 2         | Store Number: 2 | 32085 | 283            |
| 3         | Store Number: 3 | 32085 | 609            |
| 4         | Store Number: 4 | 32085 | 61             |
| 5         | Store Number: 5 | 32085 | 325            |

在这种情况下,count等于我的表中的客户总数(每个客户一行)。我想要的是另一列显示每个商店的客户百分比:customer_count除以count,但我无法弄清楚如何将其整合到我的查询中。 PS:我实际上并不需要count列。我只是把它作为参考。

感谢。

1 个答案:

答案 0 :(得分:1)

SELECT  a.cartodb_id as cartdb_id, a.identifier AS store,
count(b.*) AS   customer_count , count(b.*)/(SELECT count(*) FROM full_data_for_testing_deid_2)*100 as Percentage
FROM demo_locations_table a, full_data_for_testing_deid_2 b 
WHERE ST_DWithin(a.the_geom::geography,b.the_geom::geography,16093) 
GROUP BY a.cartodb_id