如何在没有last_day()或eomonth()的情况下获得一个月的最后一天?

发布于 2025-01-26 15:56:15 字数 1374 浏览 3 评论 0原文

我有一个桌子t:

日期位置product_id金额
2021-10-29112310
2021-10-3011239
2021-10-3111238
2021-10-291456100
2021-10-30145690
2021-10-31145680
2021-10-29212318
2021-10-30212317
2021-11-11-29245618

我需要找到每种product_ID的量,以适用于位置 + product_id的每种组合。

如果product_id当天没有进入,则金额为null。

因此结果应该看起来像:

日期位置product_id金额
2021-10-3111238
2021-10-31145680
2021-10-312123NULL
2021-11-11-302456NULL

可悲的是Exasol没有last_day()或eomonth()函数。我该如何解决?

I have a table t with:

DATELOCATIONPRODUCT_IDAMOUNT
2021-10-29112310
2021-10-3011239
2021-10-3111238
2021-10-291456100
2021-10-30145690
2021-10-31145680
2021-10-29212318
2021-10-30212317
2021-11-29245618

I need to find the AMOUNT of each PRODUCT_ID for each combination of LOCATION + PRODUCT_ID.

If a PRODUCT_ID has no entry for that day the AMOUNT is NULL.

So the result should look like:

DATELOCATIONPRODUCT_IDAMOUNT
2021-10-3111238
2021-10-31145680
2021-10-312123NULL
2021-11-302456NULL

Sadly EXASOL has no LAST_DAY() or EOMONTH() function. How can I solve this?

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

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

发布评论

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

评论(1

恰似旧人归 2025-02-02 15:56:15

您可以使用date_trunc函数与date_add结合使用date_add:

case
    when t.date = date_add('day', -1, date_add('month', 1, date_trunc('month', t.date)))
    then 'Y' else 'N' end as end_of_month

话虽如此,如果您将桌子组合成所有位置和产品的组合,则不会在未销售的情况如输出表中所示的月份。

当您对数据进行分组时,任何不存在的值都不会在输出表中显示。如果您想强迫零件出现,则可以创建一个新表格,其中包含所有产品,位置和硬编码的月末日期的组合。

然后,您可以按日期,位置和产品剩下这张新的硬编码桌来加入旧桌子。此方法将为您提供您期望的无效值。

You can get to the last day of the month using a date_trunc function in combination with date_add:

case
    when t.date = date_add('day', -1, date_add('month', 1, date_trunc('month', t.date)))
    then 'Y' else 'N' end as end_of_month

That being said, if you group your table for all combinations of locations and products, you will not get NULLs for products without sales on the last day of the month as shown in your output table.

When you group your data, any value that does not exist will simply not show up in your output table. If you want to force nulls to show up, you can create a new table that contains all combinations of products, locations, and hard-coded end of month dates.

Then, you can left join your old table with this new hard-coded table by date, location, and product. This method will give you the NULL values you expect.

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