SQL内部连接与过滤

发布于 2025-01-25 22:39:33 字数 815 浏览 2 评论 0原文

我有2个表,如下所示:

Table1:

ID  Date  

1   2022-01-01
2   2022-02-01
3   2022-02-05
Table2

ID   Date         Amount
 
1    2021-08-01     15
1    2022-02-10     15
2    2022-02-15      20
2    2021-01-01     15
2    2022-02-20     20
1    2022-03-01     15

我想在table2中选择行,以便在table1中仅超过date的行。 > table2 并计算每个子集的金额和max(date) in table2在每个子集 in ID中分组。 因此,结果看起来像

ID    Date         Amount
1     2022-03-01    30
2     2022-02-20    40

SQL Newbie ...我尝试了一个内部连接,但无法将日期过滤器传递...

尝试查询:

with table1 as (select * from table1)
,table2 as (select * from table2)
select * from table1 a
inner join table2 b on (a.id=b.id)

谢谢!

I have 2 tables as follows:

Table1:

ID  Date  

1   2022-01-01
2   2022-02-01
3   2022-02-05
Table2

ID   Date         Amount
 
1    2021-08-01     15
1    2022-02-10     15
2    2022-02-15      20
2    2021-01-01     15
2    2022-02-20     20
1    2022-03-01     15

I want to select the rows in Table2 such that only rows past the Date in Table1 are selected in Table2 and calculate a sum of amounts of each subset and max(date) in Table2 for each subset grouped by ID.
So the result would look like

ID    Date         Amount
1     2022-03-01    30
2     2022-02-20    40

SQL newbie here...I tried an inner join, but wasnt able to pass the date filter along...

Tried query:

with table1 as (select * from table1)
,table2 as (select * from table2)
select * from table1 a
inner join table2 b on (a.id=b.id)

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

终遇你 2025-02-01 22:39:33

就像保罗一样,我会使用一个加入,但我会在ON上放置条款,因此,如果您加入更多桌子,则SQL Optimizer的清洁程序更加清洁,以查看每个表/联接基础上的意图。我还将在桌子上使用别名并使用别名,因此该价值的来源没有混乱的余地,这再次使习惯使生活更轻松,而在编写更复杂的SQL或将其切成更大的代码块中。

因此,有了一些CTE的数据:

WITH table1(id, date) AS (
    SELECT * FROM VALUES 
        (1,   '2022-01-01'),
        (2  , '2022-02-01'),
        (3  , '2022-02-05')
), table2(id, date, amount) AS (
    SELECT * FROM VALUES
        (1, '2021-08-01'::date, 15),
        (1, '2022-02-10'::date, 15),
        (2, '2022-02-15'::date, 20),
        (2, '2021-01-01'::date, 15),
        (2, '2022-02-20'::date, 20),
        (1, '2022-03-01'::date, 15)
)

以下SQL:

SELECT a.id, 
    max(b.date) as max_date,
    sum(b.amount) as sum_amount
FROM table1 AS a
JOIN table2 AS b
    ON a.id = b.id AND a.date <= b.date
GROUP BY 1
ORDER BY 1;
IDMAX_DATESUM_AM_AMT
12022-03-0130
22022-02-2040 40

Much like Paul, I would use a JOIN but I would put the clauses on the ON, so if you join to more tables, it's cleaner for the SQL optimizer to see what is the intent on a per table/join basis. I would also use aliases on tables and use the alias, so there is no room for confusion where the value is coming from, which again as a habit makes life easier when composing more complex SQL or cut'n'pasting into bigger blocks of code.

so with some CTE's for the data:

WITH table1(id, date) AS (
    SELECT * FROM VALUES 
        (1,   '2022-01-01'),
        (2  , '2022-02-01'),
        (3  , '2022-02-05')
), table2(id, date, amount) AS (
    SELECT * FROM VALUES
        (1, '2021-08-01'::date, 15),
        (1, '2022-02-10'::date, 15),
        (2, '2022-02-15'::date, 20),
        (2, '2021-01-01'::date, 15),
        (2, '2022-02-20'::date, 20),
        (1, '2022-03-01'::date, 15)
)

The following SQL:

SELECT a.id, 
    max(b.date) as max_date,
    sum(b.amount) as sum_amount
FROM table1 AS a
JOIN table2 AS b
    ON a.id = b.id AND a.date <= b.date
GROUP BY 1
ORDER BY 1;
IDMAX_DATESUM_AMOUNT
12022-03-0130
22022-02-2040
和我恋爱吧 2025-02-01 22:39:33

这是我将如何使用雪花完成的方式:

--create the tables and load data

--table1
CREATE TABLE TABLE1 (ID NUMBER, DATE DATE);

INSERT INTO TABLE1 VALUES (1,   '2022-01-01');
INSERT INTO TABLE1 VALUES (2  , '2022-02-01');
INSERT INTO TABLE1 VALUES (3  , '2022-02-05');

--table 2
CREATE TABLE TABLE2 (ID NUMBER, DATE DATE, AMOUNT NUMBER);
 
INSERT INTO TABLE2 VALUES(1,   '2021-08-01',    15);
INSERT INTO TABLE2 VALUES(1,   '2022-02-10',    15);
INSERT INTO TABLE2 VALUES(2,   '2022-02-15',    20);
INSERT INTO TABLE2 VALUES(2,   '2021-01-01',    15);
INSERT INTO TABLE2 VALUES(2,   '2022-02-20',    20);
INSERT INTO TABLE2 VALUES(1,   '2022-03-01',    15);

获取数据

SELECT TABLE1.ID, MAX(TABLE2.DATE), SUM(AMOUNT)
FROM TABLE1, TABLE2
WHERE TABLE1.ID = TABLE2.ID
  AND TABLE1.DATE < TABLE2.DATE 
  GROUP BY TABLE1.ID

现在使用SELECT RESTILE

IDMAX(TABLE2.DATE)总和(数量)
12022-03-0130
22022-02-2040

Here is how I would do this with Snowflake:

--create the tables and load data

--table1
CREATE TABLE TABLE1 (ID NUMBER, DATE DATE);

INSERT INTO TABLE1 VALUES (1,   '2022-01-01');
INSERT INTO TABLE1 VALUES (2  , '2022-02-01');
INSERT INTO TABLE1 VALUES (3  , '2022-02-05');

--table 2
CREATE TABLE TABLE2 (ID NUMBER, DATE DATE, AMOUNT NUMBER);
 
INSERT INTO TABLE2 VALUES(1,   '2021-08-01',    15);
INSERT INTO TABLE2 VALUES(1,   '2022-02-10',    15);
INSERT INTO TABLE2 VALUES(2,   '2022-02-15',    20);
INSERT INTO TABLE2 VALUES(2,   '2021-01-01',    15);
INSERT INTO TABLE2 VALUES(2,   '2022-02-20',    20);
INSERT INTO TABLE2 VALUES(1,   '2022-03-01',    15);

Now obtain the data using a select

SELECT TABLE1.ID, MAX(TABLE2.DATE), SUM(AMOUNT)
FROM TABLE1, TABLE2
WHERE TABLE1.ID = TABLE2.ID
  AND TABLE1.DATE < TABLE2.DATE 
  GROUP BY TABLE1.ID

Results

IDMAX(TABLE2.DATE)SUM(AMOUNT)
12022-03-0130
22022-02-2040
梦毁影碎の 2025-02-01 22:39:33

不熟悉雪花,但是应该有效的标准SQL查询是:

select id, Max(date) Date, Sum(Amount) Amount
from Table2 t2
where exists (
  select * from Table1 t1 
  where t1.Id = t2.Id and t1.Date < t2.Date
)
group by Id;

请注意,由于您仅需要Table2中的数据,因此存在 在内部连接中都可取,并且在几乎所有情况下都会更多表现胜于联赛,最糟糕的是。

Not personally familiar with Snowflake but a standard SQL query that should work would be:

select id, Max(date) Date, Sum(Amount) Amount
from Table2 t2
where exists (
  select * from Table1 t1 
  where t1.Id = t2.Id and t1.Date < t2.Date
)
group by Id;

Note that because you are only requiring data from Table2, an exists is preferable over an inner join and in almost all cases will be more performant than a join, at worst the same.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文