【问题标题】:Selecting new rows for a given date为给定日期选择新行
【发布时间】:2012-03-30 02:59:31
【问题描述】:

我在名为 bookkeeping 的表中有两列 customer_name 和 invoice_date。

所有已开具发票的客户都会在“invoice_date”列中输入适当的值。 “customer_name”列中的值肯定会有重复,但“invoice_date”列不会(因为一次只有一个客户开具发票)。

假设表中有一个月的数据,我如何从簿记表中获取给定日期的新客户列表。例如,假设表格包含 3 月整个月份的数据,我如何获取在 3 月 17 日首次开具发票的所有客户,不包括在之前(或未来)日期开具发票的所有客户?

我正在使用 Firebird 数据库。

【问题讨论】:

  • 这是正确的 SQL 吗?
  • select distinct customer_name from bookkeeping where cast(invoice_date as date) = 'march 29, 2012' and customer_name not in (select distinct customer_name from bookkeeping where cast(invoice_date as date) < 'march 29, 2012');

标签: sql database join firebird


【解决方案1】:
SELECT
  b.customer_name

FROM
  bookkeeping b LEFT JOIN bookkeeping b2
    ON b.customer_name = b2.customer_name
      AND b.invoice_date <> b2.invoice_date
WHERE
  b.invoice_date = '17.03.2012'
  AND
  b2.customer_name IS NULL

【讨论】:

    猜你喜欢
    • 2020-07-31
    • 1970-01-01
    • 2014-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多