sql - 我可以从数据库中的另一列读取间隔值吗

标签 sql amazon-redshift

我有一张 table ,上面有 (custid,purchase_timestamp (dd-mm-yyyy), warranty_period)在几天。我需要购买时间和保修期的总和。我如何为此编写 SQL。

我试过

SELECT DATE_SUB(purchase_timestamp, INTERVAL warranty_period DAY); 

但它不起作用。请建议
 |custid |    warranty_period  |   purchase_timestamp
    |1        |      365            |   03-01-2017  
    |2        |      30             |   03-04-2017
    |3        |     10              |   25-05-2017
    |4        |      30             |   20-05-2017
    |5        |     365             |   04-06-2017
    |6        |     100             |   18-06-2017
    |7        |     90              |   30-06-2017
    |8        |     10              |   05-07-2017
    |9        |        30           |   09-07-2017
    |10       |     365             |   17-07-2017

最佳答案

假设这是您的表:

CREATE TABLE t
(
    custid integer,
    warranty_period integer,
    purchase_timestamp date /* timestamp ? */
);

有了这个数据:
INSERT INTO t
    (custid, warranty_period, purchase_timestamp)
VALUES
    ( 1, 365, '03-01-2017'),  
    ( 2,  30, '03-04-2017'),
    ( 3,  10, '25-05-2017'),
    ( 4,  30, '20-05-2017'),
    ( 5, 365, '04-06-2017'),
    ( 6, 100, '18-06-2017'),
    ( 7,  90, '30-06-2017'),
    ( 8,  10, '05-07-2017'),
    ( 9,  30, '09-07-2017'),
    (10, 365, '17-07-2017') ;

您只需使用以下 SELECT :
SELECT 
    custid,
    purchase_timestamp + cast(warranty_period || ' days' AS interval) AS end_of_warranty 
FROM
    t 
ORDER BY
    custid ;

或者
SELECT 
    custid,
    purchase_timestamp + make_interval(days := warranty_period) AS end_of_warranty 
FROM
    t 
ORDER BY
    custid ;

并得到
custid | end_of_warranty    
-----: | :------------------
     1 | 2018-01-03 00:00:00
     2 | 2017-05-03 00:00:00
     3 | 2017-06-04 00:00:00
     4 | 2017-06-19 00:00:00
     5 | 2018-06-04 00:00:00
     6 | 2017-09-26 00:00:00
     7 | 2017-09-28 00:00:00
     8 | 2017-07-15 00:00:00
     9 | 2017-08-08 00:00:00
    10 | 2018-07-17 00:00:00

Note just that PostgreSQL know how to add an interval to a date (or timestamp), and will return a timestamp.

To specify your interval you need to use

cast(warranty_period || ' days' AS interval)

或者
warranty_period * interval '1 day'

接近 SQL 标准,或使用
make_interval(days := warranty_period)

使用特定的 PostgreSQL date/time function .

您也可以使用(最简单的)形式:
SELECT 
    custid,
    purchase_timestamp + warranty_period AS end_of_warranty 
FROM
    t 
ORDER BY
    custid ;

这取决于存在 + 的事实。运算符 for (date + integer) 将整数视为天数,并执行相同的操作。在这种情况下,列 purchase_timestamp实际上应该是一个日期,或者是 CAST(... AS date) .

查看 dbfiddle here 上的所有选项

关于sql - 我可以从数据库中的另一列读取间隔值吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45365715/

相关文章:

sql - 从字段sql计算值

import - 使用 COPY 导入时 Redshift 添加列

amazon-redshift - 有没有一种方法可以更改列的数据类型而不更改列的顺序?

amazon-web-services - 将数据从单个 Kinesis Stream 流式传输到 Redshift 中的多个表

php - SQL 从多个表中选择 *

sql - 如何从SQL中的电话号码将电话号码前缀与国家/地区匹配

mysql - 检查教授和房间的时间表是否有冲突

php - 什么是处理 PHP 应用程序到 SQL 数据库的良好包装器/框架/库?

postgresql - postgres 中的 GROUP BY 列和子句

python - 使用 psycopg2 的 aws localstack redshift 连接问题