redshift删除时间戳为星期六的记录

时间:2016-08-18 11:23:49

标签: amazon-redshift

嗨,大家好,我是红色新手,需要帮助 要求是使用工作日星期六的dw_created_date从表中删除记录。 请帮忙

2 个答案:

答案 0 :(得分:0)

我还没有使用过Amazon Redshift,但在阅读documentation之后,你应该找到你的记录:

SELECT *, to_char(dw_created_date, 'D') dayofweek FROM table

“dayofweek”中的数字6表示星期六所以这将删除记录:

DELETE FROM table WHERE to_char(dw_created_date, 'D') = 6
祝你好运!

答案 1 :(得分:0)

Amazon Redshift功能TO_CHAR允许您从Redshift中提取日期部分和有关日期和时间戳的信息,并获取日期部分。

您可以找到有关TO_CHAR function here

的信息

为了从TO_CHAR函数中提取所需的信息,您需要使用适当的日期时间格式字符串。例如," D"返回星期几,DY返回工作日名称的缩写,DAY提供完整拼写的工作日名称。

您可以找到有关date time format strings for Redshift here

的信息

下面我将提供一段快速代码,展示TO_CHAR功能的工作原理。

create table tba (colint integer, colts timestamp) distkey (colint) sortkey (colts);
insert into tba (colint, colts) values (1, '2016-08-08 08:08:08');
insert into tba (colint, colts) values (1, '2016-08-09 09:09:09');
insert into tba (colint, colts) values (1, '2016-08-10 10:10:10');
insert into tba (colint, colts) values (1, '2016-08-11 10:11:11');
insert into tba (colint, colts) values (12, '2016-08-12 12:12:12');
insert into tba (colint, colts) values (13, '2016-08-13 13:13:13');
insert into tba (colint, colts) values (14, '2016-08-14 14:14:14');
insert into tba (colint, colts) values (15, '2016-08-15 15:15:15');
insert into tba (colint, colts) values (16, '2016-08-16 16:16:16');
insert into tba (colint, colts) values (17, '2016-08-17 17:17:17');
insert into tba (colint, colts) values (18, '2016-08-18 18:18:18');
insert into tba (colint, colts) values (20, '2016-08-20 20:20:20');
insert into tba (colint, colts) values (6, '2016-08-06 06:06:06');
select *
, to_char(colts,'D') day_of_week_number 
, to_char(colts,'DAY') day_of_week_name 
, to_char(colts,'DY') day_of_week_abbrev
from tba;

 colint |        colts        | day_of_week_number | day_of_week_name | day_of_week_abbrev 
--------+---------------------+--------------------+------------------+--------------------
     15 | 2016-08-15 15:15:15 | 2                  | MONDAY           | MON
     16 | 2016-08-16 16:16:16 | 3                  | TUESDAY          | TUE
     18 | 2016-08-18 18:18:18 | 5                  | THURSDAY         | THU
      1 | 2016-08-08 08:08:08 | 2                  | MONDAY           | MON
      1 | 2016-08-09 09:09:09 | 3                  | TUESDAY          | TUE
      1 | 2016-08-10 10:10:10 | 4                  | WEDNESDAY        | WED
      1 | 2016-08-11 10:11:11 | 5                  | THURSDAY         | THU
     12 | 2016-08-12 12:12:12 | 6                  | FRIDAY           | FRI
     13 | 2016-08-13 13:13:13 | 7                  | SATURDAY         | SAT
     14 | 2016-08-14 14:14:14 | 1                  | SUNDAY           | SUN
     17 | 2016-08-17 17:17:17 | 4                  | WEDNESDAY        | WED
     20 | 2016-08-20 20:20:20 | 7                  | SATURDAY         | SAT
      6 | 2016-08-06 06:06:06 | 7                  | SATURDAY         | SAT
(13 rows)

最后,但并非最不重要的是,如果您不熟悉Redshift,则应注意两个非常重要的事项。每次删除或更新大量数据时,都应该总是做两件事:

  1. VACUUM - Amazon Redshift确实"逻辑"删除"删除"或者"更新"数据。因此,如果您更改大量记录,则应运行vacuum以从表中物理删除数据。我的经验法则"是当超过5%的数据被删除或更新时,是时候运行VACUUM。您应该运行vacuum full tab;之类的命令。 You can find more info about VACUUM here.
  2. ANALYZE - Amazon Redshift依赖于有关表数据和分布的准确统计信息,以便创建最有效的查询计划。如果您删除,插入或更改重要的"部分数据。您应该在表上运行ANALYZE命令以确保数据库具有最新的统计信息。示例命令为ANALYZE VERBOSE TBA;You can find more information about ANALYZE here.