簡體   English   中英

從城市獲得客戶的百分比

[英]Getting percentage of customers from a city

我想檢索居住在某些城市的注冊客戶的百分比。

目前我有這個

SELECT
COUNT(city) / Count(CustID) * 100 AS percent, city
FROM customer
GROUP BY City

當前,它為每一行提供100%的結果。
我在這里缺少明顯的東西。 如何解決查詢?

您需要在單獨的子查詢中獲得客戶總數:

SELECT COUNT(city) / (SELECT Count(CustID) FROM Customer) * 100 AS percent, city
FROM customer
GROUP BY City

或者,使用CROSS JOIN

SELECT
COUNT(city) / x.cnt * 100 AS percent, city
FROM customer
CROSS JOIN (SELECT Count(CustID) AS cnt FROM Customer) AS x
GROUP BY City

嘗試這個

SELECT
COUNT(city) / (SELECT Count(CustID) FROM Customer) * 100 AS percent, city
FROM customer c
GROUP BY City

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM