如何在没有last_day()或eomonth()的情况下获得一个月的最后一天?
我有一个桌子t:
日期 | 位置 | product_id | 金额 |
---|---|---|---|
2021-10-29 | 1 | 123 | 10 |
2021-10-30 | 1 | 123 | 9 |
2021-10-31 | 1 | 123 | 8 |
2021-10-29 | 1 | 456 | 100 |
2021-10-30 | 1 | 456 | 90 |
2021-10-31 | 1 | 456 | 80 |
2021-10-29 | 2 | 123 | 18 |
2021-10-30 | 2 | 123 | 17 |
2021-11-11-29 | 2 | 456 | 18 |
我需要找到每种product_ID的量,以适用于位置 + product_id的每种组合。
如果product_id当天没有进入,则金额为null。
因此结果应该看起来像:
日期 | 位置 | product_id | 金额 |
---|---|---|---|
2021-10-31 | 1 | 123 | 8 |
2021-10-31 | 1 | 456 | 80 |
2021-10-31 | 2 | 123 | NULL |
2021-11-11-30 | 2 | 456 | NULL |
可悲的是Exasol没有last_day()或eomonth()函数。我该如何解决?
I have a table t with:
DATE | LOCATION | PRODUCT_ID | AMOUNT |
---|---|---|---|
2021-10-29 | 1 | 123 | 10 |
2021-10-30 | 1 | 123 | 9 |
2021-10-31 | 1 | 123 | 8 |
2021-10-29 | 1 | 456 | 100 |
2021-10-30 | 1 | 456 | 90 |
2021-10-31 | 1 | 456 | 80 |
2021-10-29 | 2 | 123 | 18 |
2021-10-30 | 2 | 123 | 17 |
2021-11-29 | 2 | 456 | 18 |
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:
DATE | LOCATION | PRODUCT_ID | AMOUNT |
---|---|---|---|
2021-10-31 | 1 | 123 | 8 |
2021-10-31 | 1 | 456 | 80 |
2021-10-31 | 2 | 123 | NULL |
2021-11-30 | 2 | 456 | NULL |
Sadly EXASOL has no LAST_DAY() or EOMONTH() function. How can I solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用date_trunc函数与date_add结合使用date_add:
话虽如此,如果您将桌子组合成所有位置和产品的组合,则不会在未销售的情况如输出表中所示的月份。
当您对数据进行分组时,任何不存在的值都不会在输出表中显示。如果您想强迫零件出现,则可以创建一个新表格,其中包含所有产品,位置和硬编码的月末日期的组合。
然后,您可以按日期,位置和产品剩下这张新的硬编码桌来加入旧桌子。此方法将为您提供您期望的无效值。
You can get to the last day of the month using a date_trunc function in combination with date_add:
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.