如何在SQL数据库中搜索客户数量?

问题描述 投票:0回答:7

我有一个名为“accounts”的SQL表,其中包含以下列:

registration_date account_id

并使用以下列创建另一个名为“revenue”的SQL表:

account_id revenue_month sales_revenue

我想要实现的是获得以下内容:

获取2017年1月至12月期间注册的客户数量,以及2017年有多少客户购买的数量(即sales_revenue超过0)。

我是SQL的新手,我尝试了以下但我无法做到。

SELECT COUNT account_id
FROM accounts
WHERE registration_date BETWEEN #01/01/2017# AND #12/31/2017#;

应该告诉我2017年注册的账户数量?

然后

SELECT COUNT account_id
FROM revenue
WHERE sales_revenue > 0;

应该给我收入超过0的账户数量?

我错过了什么?我将不胜感激任何帮助。

谢谢!

sql amazon-redshift
7个回答
0
投票

试试这个:

select count(account_id) as total, 
       sum(case when sales_revenue > 0 then 1 else 0 end) as sales_rev
FROM accounts
WHERE registration_date BETWEEN #01/01/2017# AND #12/31/2017#;

如有任何疑问,请告诉我。


0
投票
SELECT
    COUNT(DISTINCT accounts.account_id)
FROM
    accounts, revenue
WHERE 
    accounts.registration_date BETWEEN #01/01/2017# AND #12/31/2017#
    AND
    revenue.revenue_month BETWEEN #01/2017# AND #12/2017# 
    AND
    accounts.account_id = revenue.account_id
    AND
    sales_revenue > 0

0
投票

甲骨文(再次)。您的数据库可能使用不同的功能

假设REGISTRATION_DATE是DATE数据类型(是的,它应该是),那么这可能会完成这项工作:

select count(*) 
from accounts
where extract(year from registration_date) = 2017;

收入也是如此 - 假设REVENUE_MONTH是DATE。如果不是,它是什么?

select count(distinct account_id)
from revenue
where extract(year from revenue_month) = 2017
having sum(sales_revenue) > 0;

0
投票

请尝试以下方法

获取2017年1月至12月期间注册的客户数量

SELECT COUNT (account_id)
  FROM accounts
 WHERE registration_date BETWEEN '01-JAN-2017' AND '12-DEC-2017'

并计算2017年有多少人买了东西(即sales_revenue大于0)。

SELECT COUNT (account_id)
  FROM revenue
 WHERE sales_revenue > 0
 AND registration_date BETWEEN '01-JAN-2017' AND '12-DEC-2017'

以上是Oracle,您的数据库将类似。


0
投票

如果您发布SQLFiddle,示例数据和预期结果,您将获得更好的答案......

你错过了一个加入。表格帐户会告诉您在您关注的日期范围内注册的人员,而表格收入会告诉您他们购买了什么。

不是每个帐户都买了东西,所以你需要一个外部联接。

由于您只想计算购买东西的帐户数量而不是交易数量,因此您必须使用不同的计数。并非每个平台都支持。

你想要的东西:

SELECT COUNT (a.*), count(distinct r.account_id)
FROM accounts a
outer join revenue r
  on a.account_id = r.account_id
WHERE a.registration_date BETWEEN #01/01/2017# AND #12/31/2017#
and   r.revenue_Month between '01/2017' and '12/2017'

0
投票

这将为您提供2017年注册的所有客户以及2017年是否购买的信息:

select 
  account_id,
  case when exists 
  (
    select *
    from revenue r
    where r.account_id = a.account_id
    and r.sales_revenue > 0
    and r.revenue_month like '%2017%'
  ) then 1 else 0 end as has_bought_in_2017
from accounts a
where year(registration_date) = 2017;

(您可能想要更改LIKE子句;我不知道您的revenue_month是如何构建的。)

现在使用此查询来获取计数:

select 
  count(*) as registered_in_2017,
  sum(has_bought_in_2017) as and_have_bought_in_2017
from (<above query>) q;

0
投票
SELECT COUNT(account_id)
   FROM accounts
   WHERE registration_date BETWEEN '01/01/2017' AND '12/31/2017';

这将为您提供在01/01/201712/31/2017之间注册的帐户ID的计数

SELECT COUNT(account_id)
FROM revenue
WHERE sales_revenue > 0;

这将为您提供sales_revenue大于零的帐户ID的数量。

如果您想要01/01/201712/31/2017以及sales_revenue> 0之间的帐户数量,那么:

select count(account_id) 
from accounts a,revenue r
where a.registration_date BETWEEN '01/01/2017' AND '12/31/2017'
and r.sales_revenue > 0;
© www.soinside.com 2019 - 2024. All rights reserved.