0
SELECT (CASE WHEN ymd BETWEEN CURRENT_DATE -4 AND CURRENT_DATE -1 THEN '3day total' 
WHEN ymd BETWEEN CURRENT_DATE -11 AND CURRENT_DATE -1 THEN '10day total' 
WHEN ymd BETWEEN CURRENT_DATE -31 AND CURRENT_DATE -1 THEN '30day total' END) AS 'Period',
SUM(cost) cost
FROM table
WHERE ymd BETWEEN CURRENT_DATE -31 AND CURRENT_DATE -1
GROUP BY 1

结果实际上似乎给了我第 1-3 天、第 4-10 天和第 11-30 天的时段。我相信这是因为有重叠的条件,一旦满足第一个条件,SQL 就会停止处理 CASE 语句。

我想要的是每个桶的总数(即 3 天总和、10 天总和和 30 天总和)。

有没有办法在不添加额外字段的情况下做到这一点?

PS - 语法与传统 sql 有点不同,因为它是 vsql (vertica)。

4

4 回答 4

1

进行三个总计而不是一个总计,并使用条件来确定对每条记录进行计数的位置,而不是尝试确定单个总计的期间:

select
  sum(case when ymd between CURRENT_DATE - 4 and CURRENT_DATE - 1 then cost else 0 end) as '3day total',
  sum(case when ymd BETWEEN CURRENT_DATE - 11 and CURRENT_DATE - 5 then cost else 0 end) as '10day total',
  sum(case when ymd BETWEEN CURRENT_DATE - 31 and CURRENT_DATE - 12 then cost else 0 end) as '30day total'
from
  table
and
  ymd between CURRENT_DATE -31 and CURRENT_DATE -1
group by
  1

注意:我不确定您要在哪里计算哪些日期范围,所以我制作它们以便它们不会重叠,因为如果您使用between. 如果您仍然希望它们重叠,那么您可以进行比较,而不是使用between任何晚于CURRENT_DATE - 1已经被过滤掉的值。

编辑:

要获得行中的结果,您可以在选择之间进行联合:

select '3day total' as period, sum(cost) as cost
from table
where ymd between CURRENT_DATE - 4 and CURRENT_DATE - 1
union all
select '10day total', sum(cost)
from table
where ymd BETWEEN CURRENT_DATE - 11 and CURRENT_DATE - 5
union all
select '30day total', sum(cost)
from table
where ymd BETWEEN CURRENT_DATE - 31 and CURRENT_DATE - 12
于 2014-02-27T23:47:23.087 回答
0

尝试为每个存储桶设置不同的字段。我假设您想对每个存储桶的成本求和,THEN cost如果您想对不同的字段求和,您可能需要更改。

您的查询中还缺少一个WHERE子句。条件以AND语句开头,您可能想要更正它。

SELECT SUM(CASE WHEN ymd BETWEEN CURRENT_DATE -4 AND CURRENT_DATE -1 THEN cost ELSE 0 END) as '3day total',
SUM(CASE WHEN ymd BETWEEN CURRENT_DATE -11 AND CURRENT_DATE -1 THEN cost ELSE 0 END) as '10day total', 
SUM(CASE WHEN ymd BETWEEN CURRENT_DATE -31 AND CURRENT_DATE -1 THEN cost ELSE 0 END) as  '30day total' END) AS 'Period'
FROM table
WHERE ymd BETWEEN CURRENT_DATE -31 AND CURRENT_DATE -1
GROUP BY 1
于 2014-02-27T23:44:55.543 回答
0

尝试扭转你的条件:

SELECT (CASE WHEN ymd BETWEEN CURRENT_DATE -31 AND CURRENT_DATE -1 THEN '30day total' 
WHEN ymd BETWEEN CURRENT_DATE -11 AND CURRENT_DATE -1 THEN '10day total' 
WHEN ymd BETWEEN CURRENT_DATE -4 AND CURRENT_DATE -1 THEN '3day total' 
END) AS 'Period',
SUM(cost) cost
FROM table
WHERE ymd BETWEEN CURRENT_DATE -31 AND CURRENT_DATE -1
GROUP BY 1
于 2014-02-27T23:46:38.603 回答
0

如果要将其保留为三行一列,这是一种方法:

SELECT which,
       sum(CASE WHEN ymd BETWEEN CURRENT_DATE - diff AND CURRENT_DATE -1 THEN cost
                else 0
           end) as cost
FROM table t cross join
     (select '3day total' as which, 4 as diff union all
      select '10day total', 11 union all
      select '30day total', 31
     ) w
WHERE ymd BETWEEN CURRENT_DATE -31 AND CURRENT_DATE -1
GROUP BY which;

编辑:

通过cross join为原始表中的每一行创建三行来工作。每个组都考虑这些行中的每一行——因此重叠不是问题。这些组由它们的名称和diff值定义。

换句话说,如果数据从 7 天前的一行开始:

ymd                  cost
CURRENT_DATE - 7      $10

然后将其乘以 3 cross join

which         diff       ymd                 cost
3day total      4        CURRENT_DATE - 7     $10
10day total    11        CURRENT_DATE - 7     $10
30day total    31        CURRENT_DATE - 7     $10

然后,当这些由 聚合时which,将计算正确的值:

which         Total
3day total      $0
10day total    $10
30day total    $10
于 2014-02-28T01:36:44.767 回答