1

我正在此表上编写查询以获取所有目录的大小总和,按日期为昨天的目录分组。我没有从以下查询中得到任何输出。

test.id        test.path           test.size     test.date
1   this/is/the/path1/fil.txt      232.24           2019-06-01
2   this/is/the/path2/test.txt     324.0            2016-06-01
3   this/is/the/path3/index.txt    12.3             2017-05-01
4   this/is/the/path4/test2.txt    134.0            2019-03-23
5   this/is/the/path1/files.json   2.23             2018-07-23
6   this/is/the/path1/code.java    1.34             2014-03-23
7   this/is/the/path2/data.csv     23.42            2016-06-23
8   this/is/the/path3/test.html    1.33             2018-09-23
9   this/is/the/path4/prog.js      6.356            2019-06-23
4   this/is/the/path4/test2.txt    134.0            2019-04-23

SELECT regexp_replace(path,'[^/]+$',''), sum(cast(size as decimal)) 
from test WHERE date > date_sub(current_date, 1) GROUP BY path,size;

4

2 回答 2

2

你绝不能group by size,只能靠regexp_replace(path,'[^/]+$','')
另外,既然你只想要昨天的行,为什么要使用WHERE date > '2019%
您可以通过以下方式获取昨天的日期date_sub(current_date, 1)

select 
  regexp_replace(path,'[^/]+$',''), 
  sum(cast(size as decimal)) 
from test 
where date = date_sub(current_date, 1) 
group by regexp_replace(path,'[^/]+$','');
于 2019-06-02T11:59:41.650 回答
0

你可能想要WHERE date >= '2019-01-01'. 在匹配字符串中使用%,例如 your 2019%,仅适用于 LIKE,而不适用于不等式匹配。

您提供的示例看起来像您想要 2019 日历年中的所有行。

昨天,你想要

  WHERE date >= DATE_SUB(current_date, -1)
    AND date < current_date

即使您的date列包含时间戳,这也有效。

于 2019-06-02T11:59:26.517 回答