简体   繁体   English

如何使用 SQL 从另一个表中的一个表中找到第一个匹配结果?

[英]How to use SQL find first matching result from one table in another table?

Suppose I had two tables:假设我有两张桌子:

Customer -
ID  | Name  | Etc
1   | One   |
2   | Two   |
3   | Three |
4   | Four  |
5   | Five  |
... | ...   |

Sales - 
Customer ID | Date | Amount
5           | 1/20 | $45
5           | 3/19 | $145
3           | 8/19 | $453
7           | 3/20 | $4513
3           | 9/20 | ...
1           | 3/20 | ...
1           | 1/20 | ...

What I want to do is write a query that will find the first sale for each customer.我想做的是编写一个查询,为每个客户找到第一笔销售。 I am not sure exactly how to do it.我不确定该怎么做。 I feel like this is group by problem, but the answer is not coming to me.我觉得这是按问题分组的,但答案并没有出现。

EDIT: I feel like my first data table did not fully explain my problem.编辑:我觉得我的第一个数据表并没有完全解释我的问题。 (Honestly, I didn't even realize this facet of my problem until, I was coding the solution) Note: There is more than one customer per sale. (老实说,在我编写解决方案之前,我什至没有意识到我的问题的这一方面) 注意:每次销售有多个客户。

Sales - 
Sale ID | Customer ID | Date | Amount
1       | 5           | 1/20 | $45
5       | 5           | 3/19 | $145
8       | 3           | 8/19 | $453
7       | 7           | 3/20 | $4513
3       | 4           | 9/20 | ...
2       | 1           | 3/20 | ...
1       | 1           | 1/20 | ...

You can use a subquery which assigns a row number to each sale for each customer, ordering by ascending date, and then select only the first rows:您可以使用子查询为每个客户的每次销售分配一个行号,按日期升序排序,然后 select 仅第一行:

SELECT "Customer ID", "Date", "Amount"
FROM (
  SELECT "Customer ID", "Date", "Amount",
         ROW_NUMBER() OVER (PARTITION BY "Customer ID" ORDER BY "Date") AS rn
  FROM Sales) s
WHERE rn = 1

Demo on SQLFiddle SQLFiddle 上的演示

Nick's solution is probably the most performant, but if you wanted to use GROUP BY here, you could do that: Nick 的解决方案可能是性能最高的,但如果你想在这里使用GROUP BY ,你可以这样做:

SELECT
    c.ID,
    c.Name,
    s1.Date,
    s1.Amount
FROM Customer c
INNER JOIN Sales s1 ON c.ID = s1."Customer ID"
INNER JOIN
(
    SELECT "Customer ID", MIN(Date) AS FirstSaleDate
    FROM Sales
    GROUP BY "Customer ID"
) s2
    ON s1."Customer ID" = s2."Customer ID" AND
       s1.Date = s2.FirstSaleDate
ORDER BY
    c.ID,
    c.Name;

In Oracle, you can use keep :在 Oracle 中,您可以使用keep

select customer_id, min(date) as first_sales_date,
       max(amount) keep (dense_rank first order by date asc) as first_amount
from sales
group by customer_id;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用一个 SQL 查询的结果从另一个表中查找结果? - Using result of one SQL query to find the result from another table? 从一个表与另一个表中查找匹配的人(MS SQL Server) - Find matching persons from one table with another (MS SQL Server) 从一个表中查询SQL并使用第一个查询表中的结果查询另一个表 - Sql query from one table and to query another table with result from first query table 如何使用 SQL select 的结果来获取另一个表中的记录? - How to use a result from a SQL select to get records in another table? 如何从第一个表中选择行并在另一个表中匹配条目? - How to select rows from first table & matching entry in another table? 在同一表中查找从一列到另一列的匹配值 SQL 服务器 - Find matching values from one column to another column in same table SQL Server 如何根据 SQL 中另一表的匹配字符串列更新一个表的数字列 - How to update numerical column of one table based on matching string column from another table in SQL 如何在一个表中对MYSQL中的一系列行求和,匹配另一个表中的信息以获得结果 - How to Sum a series of rows in MYSQL from one Table, matching the info from another to get the result select 如何将一张表中的所有数据与另一张表中的记录匹配,先选数据。 全部在一个查询中 - How to select all data from one table and records from another table matching data in first selection. All in one query 如何从一个与另一个表匹配的表中删除行? - How to delete rows from one table matching another table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM