【发布时间】:2020-03-26 18:50:54
【问题描述】:
假设我在test_data 中有一些示例数据,如下所示,每周有 1-5 天的数据在数据库中(>=1 天'有数据'
code vol val num test_date
--------------------------------------------
1 00001 100 0.1 111 20191104
2 00001 100 0.1 111 20191105
3 00001 100 0.1 111 20191106
4 00001 100 0.1 111 20191107
5 00001 100 0.1 111 20191108
7 00001 100 0.1 111 20191111
8 00001 200 0.1 222 20191112
9 00001 200 0.1 111 20191113
10 00001 400 0.3 222 20191114
11 00001 200 0.2 333 20191118
12 00002 100 0.1 111 20191104
13 00002 200 0.1 222 20191105
14 00002 200 0.1 111 20191106
15 00002 400 0.3 222 20191107
16 00002 200 0.2 333 20191108
....................
....................
我想按周/年和code 来总结volume、number 和value,现在我可以通过下面的SQL 查询来总结它们,但我无法得到最后日期一周根据test_date,最后一天可能是一周或一年中的任何一天,因为业务/工作日,我们需要显示最后日期列
SELECT t.code
,date_add(concat_ws('-',substr(t.test_date,1,4),substr(t.test_date,5,2),substr(t.test_date,7,2)) ,
-pmod(datediff(concat_ws('-',substr(t.test_date,1,4),substr(t.test_date,5,2),substr(t.test_date,7,2)),'1990-01-01'),7)) AS test_date
,sum(t.number) AS num
,sum(t.volume) AS vol
,sum(t.value) AS val
FROM test_data t
GROUP BY t.code, test_date
现在我的输出如下:
code vol val num test_date(monday)
----------------------------------------------------
1 00001 500 0.5 555 20191104
2 00001 900 0.6 666 20191111
3 00001 200 0.1 111 20191118
4 00001 400 0.3 222 20191125
5 00001 200 0.2 333 20191202
但我的预期输出如下:
code vol val num test_date(the last date of week in database)
-------------------------------------------------------------------------------
1 00001 500 0.5 555 20191108
2 00001 900 0.6 666 20191114
3 00001 200 0.1 111 20191122
4 00001 400 0.3 222 20191129
5 00001 200 0.2 333 20191206
非常感谢您的建议。
【问题讨论】:
-
“一周的最后一天”是哪一天?你是说星期五吗?您已标记此问题
[oracle]以及[hive]和[impala]:您是否想要一个适用于 Oracle RDBMS 的解决方案以及或作为其他平台/语言的替代方案? -
@APC 由于业务/工作方式的原因,最后一天可能是一周中的任何一天。我想要一个 Hive 解决方案,我认为最好让您知道我们的基础数据库是 Oracle
-
最好让您的问题尽可能清晰。不同的数据库具有不同的功能和语法。对于日期争论尤其如此,因为没有 ANSI 标准。因此,请编辑您的问题以阐明您的要求。
-
@APC 我知道'不同的数据库有不同的功能和语法',这就是为什么我说我们的数据库是
Oracle
标签: sql oracle hive group-by dayofweek