【问题标题】:SQL - Count number of transactions if at least one of the transactions is todaySQL - 如果今天至少有一个事务,则计算事务数
【发布时间】:2017-07-21 15:05:57
【问题描述】:

我有一个客户交易数据库,每笔交易都有一个特定的日期。仅当客户今天进行了交易时,我才需要计算每个客户在过去两个月中进行的交易次数。

我一直在想,它需要我使用 WHERE 设置两个月的完整范围,并使用另一个 HAVING 语句来确保最新日期(客户交易的 MAX)等于今天的日期,但我似乎无法得到它工作。这听起来像是解决这个问题的正确方法还是有更好的方法?

谢谢!

【问题讨论】:

  • 也许,也许......为了获得更好的答案,您必须添加更多关于您尝试但它们不起作用的表和查询的信息

标签: sql date count multiple-tables


【解决方案1】:

您没有提供有关架构如何的任何信息,但我假设您有一个 Customer 表和一个 Transaction 表。考虑这个有 4 个客户和 12 个交易的例子。

客户

| id |     name |
|----|----------|
|  1 |   Google |
|  2 | Facebook |
|  3 |    Hooli |
|  4 |   Yahoo! |

交易

| id | transaction_date | customer_id |
|----|------------------|-------------|
|  1 |       2017-04-15 |           1 |
|  2 |       2017-06-24 |           1 |
|  3 |       2017-07-09 |           1 |
|  4 |       2017-07-24 |           1 |
|  5 |       2017-07-23 |           2 |
|  6 |       2017-07-22 |           2 |
|  7 |       2017-07-21 |           2 |
|  8 |       2017-07-24 |           2 |
|  9 |       2017-07-24 |           3 |
| 10 |       2017-07-23 |           4 |
| 11 |       2017-07-22 |           4 |
| 12 |       2017-07-21 |           4 |

要计算每个客户过去两个月的交易次数,一个简单的 group by 就可以完成这项工作:

select name, count(*) as number_of_transactions
from transactions t
  inner join customers c on c.id = t.customer_id
where t.transaction_date > dateadd(month, -2, getdate())
group by c.name

这会产生

|     name | number_of_transactions |
|----------|------------------------|
| Facebook |                      4 |
|   Google |                      3 |
|    Hooli |                      1 |
|   Yahoo! |                      3 |

要仅检索交易日期等于今天的交易的客户,我们可以使用存在来检查这样的行是否存在。

select name, count(*) as number_of_transactions
from transactions t
  inner join customers c on c.id = t.customer_id
where t.transaction_date > dateadd(month, -2, getdate())
  and exists(select *
             from transactions
             where customer_id = t.customer_id
               and transaction_date = convert(date, getdate()))
group by c.name

因此,如果事务表中的某行的 transaction_date 等于今天,并且 customer_id 等于来自主查询的 customer_id,则将其包含在结果中。运行该查询(鉴于今天是 7 月 24 日)给我们这个结果:

|     name | number_of_transactions |
|----------|------------------------|
| Facebook |                      4 |
|   Google |                      3 |
|    Hooli |                      1 |

Check out this sql fiddle http://sqlfiddle.com/#!6/710c94/13

【讨论】:

    【解决方案2】:

    您可以在WHERE 子句中添加一个子查询来查找今天有销售的客户:

    SELECT count(*) /*count of transactions*/
    FROM transactions
    WHERE 
        /*Transactions in the last two months*/
        transaction_date > DATEADD(mm, -2, GETDATE())
        /*For customers that have had a sale today*/
        customer_number in (SELECT customer_number FROM transactions WHERE transaction_date = GETDATE());
    

    完全猜测您的表结构、表名和字段名,但这应该会让您接近。

    【讨论】:

      【解决方案3】:

      或者,您可以尝试进行内部连接:

      SELECT t2.CustomerID,count(*) as TransactionsCount 
      FROM [Tansaction] t1 INNER JOIN [Tansaction] t2
      ON t1.CustomerID= t2.CustomerID
      WHERE CONVERT(date,t1.TransactionDateTime) = CONVERT(date,GETDATE())
      AND t2.TransactionDateTime>= DATEADD(mm, -2, GETDATE())
      GROUP BY  t2.CustomerID
      

      【讨论】:

        【解决方案4】:

        首先,您需要获取今天进行交易的客户列表。我假设您有一个包含交易日期和客户详细信息的“交易表”。

        使用以下方法从此事务表中进行选择:

        Select count of distinct(transactiondate), Customer 
        from Transactiontable
        where transactiondate > dateadd(months,-2, getdate())
        and customer in (select customer from transactiontable 
        `where cast(transactiondate as date) = cast(getdate() as date))
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-02-13
          • 1970-01-01
          • 2019-02-09
          • 2016-02-02
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多