如何按日期列选择分组数据? [ mysql ]

发布于 2025-01-18 21:54:07 字数 1321 浏览 1 评论 0原文

我有 3 个表:usersuser_spentsuser_venues

  • user_spentsuser_venues 表包含用户每个日期的活动。
  • 每个用户在一个日期内可以进行多项活动。 (例如,用户可以在 2022 年 4 月 1 日有多次支出或收入)。
  • 强制要求用户在给定日期内必须有支出收入

我想选择单个用户的活动按日期分组和过滤。

Sql Fiddle Link 和 sql 到目前为止我尝试过的: http://sqlfiddle.com/#! 9/846c851/1

我做错了什么?你能给我一些建议吗?

我想要实现的示例数据:

name    spent   income  date
----------------------------------
Jack    8       12      2022-04-01
Jack    4       56      2022-04-02
Jack    NULL    44      2022-04-03
Jack    7       NULL    2022-04-04

示例数据。 (也包含在 sql fiddle 中)

Users Table:
id  name
1     Jack

User Spents:
id  user_id spent   date
1     1       2     2022-04-01
2     1       1     2022-04-01
3     1       5     2022-04-01
4     1       3     2022-04-02
5     1       1     2022-04-02

User Spents:
id  user_id income  date
1     1       44        2022-04-03
2     1       15        2022-04-02
3     1       41        2022-04-02
4     1       12        2022-04-01
5     2       54        2022-04-01

任何帮助将不胜感激。谢谢。

I have 3 tables, users, user_spents, and user_incomes.

  • user_spents and user_incomes tables contains activities of users per date.
  • There can be multiple activities in one date per user. (e.g. user can have multiple spent or income in 2022-04-01 date).
  • It's not mandatory that user must have spent or income in a given date

I want to select activities of single user grouped and filtered by date.

Sql Fiddle Link and sql What I have tried so far: http://sqlfiddle.com/#!9/846c851/1

What am I doing wrong? Can you give me any suggestions?

Example data what I want to achieve:

name    spent   income  date
----------------------------------
Jack    8       12      2022-04-01
Jack    4       56      2022-04-02
Jack    NULL    44      2022-04-03
Jack    7       NULL    2022-04-04

Sample data. (Alse included in sql fiddle)

Users Table:
id  name
1     Jack

User Spents:
id  user_id spent   date
1     1       2     2022-04-01
2     1       1     2022-04-01
3     1       5     2022-04-01
4     1       3     2022-04-02
5     1       1     2022-04-02

User Spents:
id  user_id income  date
1     1       44        2022-04-03
2     1       15        2022-04-02
3     1       41        2022-04-02
4     1       12        2022-04-01
5     2       54        2022-04-01

Any help will be appreciated. Thanks.

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

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

发布评论

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

评论(1

素罗衫 2025-01-25 21:54:07

您可以使用 UNION ALL 获取 user_spentsuser_venues 的所有行,然后加入 users 进行聚合:

SELECT u.id, u.name,
       SUM(t.spent) spent,
       SUM(t.income) income, 
       t.date
FROM users u
INNER JOIN (
  SELECT user_id, spent, null income, date FROM user_spents
  UNION ALL
  SELECT user_id, null, income, date FROM user_incomes
) t ON t.user_id = u.id
WHERE u.name = 'Jack' -- remove this condition to get results for all users
GROUP BY u.id, t.date;

请参阅演示

You can use UNION ALL to get all the rows of user_spents and user_incomes and then join to users to aggregate:

SELECT u.id, u.name,
       SUM(t.spent) spent,
       SUM(t.income) income, 
       t.date
FROM users u
INNER JOIN (
  SELECT user_id, spent, null income, date FROM user_spents
  UNION ALL
  SELECT user_id, null, income, date FROM user_incomes
) t ON t.user_id = u.id
WHERE u.name = 'Jack' -- remove this condition to get results for all users
GROUP BY u.id, t.date;

See the demo.

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