Teradata SQL - 更改到目前为止的时间戳

时间:2017-01-19 13:23:49

标签: sql-server teradata

我有一个包含两列的表格:

Column1: Email_address[VARCHAR(255), UNICODE]
Column2: newsletter_subscription_date [TIMESTAMP (0)] e.g. DD.MM.YYYY HH:MM:SS

目标:我想写一个查询,告诉我在特定时间范围内每天订阅了多少订阅。

问题:我不知道如何将时间范围转换为日期格式YYYY / MM / DD

到目前为止我的查询:

SELECT  CAST(newsletter_subscription_date AS DATE FORMAT 'YYYY/MM/DD'), COUNT(*)
FROM emailtable WHERE newsletter_subscription_date >= DATE - 30
GROUP BY newsletter_subscription_date
ORDER BY newsletter_subscription_date DESC

但我得到的是这样的:

 newsletter_subscription_date   Count(*)
1   18.01.2017  1
2   18.01.2017  1
3   18.01.2017  1
4   18.01.2017  1
5   18.01.2017  1
6   18.01.2017  1
7   18.01.2017  1
8   18.01.2017  1
9   18.01.2017  1
10  18.01.2017  1
11  18.01.2017  1

如何将时间戳转换为正确的日期?

2 个答案:

答案 0 :(得分:0)

您可以在呈现之前将日期转换为char并按照您的喜好进行格式化

sel to_char(CAST(newsletter_subscription_date AS date),  'YYYY/MM/DD'),  COUNT(*)
FROM emailtable WHERE newsletter_subscription_date >= DATE - 30
GROUP BY newsletter_subscription_date
ORDER BY newsletter_subscription_date DESC

答案 1 :(得分:0)

尝试为您的重新投射列添加新名称和组:

SELECT  CAST(newsletter_subscription_date AS DATE FORMAT 'YYYY/MM/DD') as sub_date, 
        COUNT(*) as num_subscriptions
FROM    emailtable 
WHERE   newsletter_subscription_date >= DATE - 30
GROUP BY sub_date
ORDER BY sub_date DESC