使用WITH子句的语法错误

时间:2016-11-01 19:35:29

标签: sql amazon-redshift

我正在尝试设置一系列季度日期的CTE表 查询返回[42601]错误:语法错误等于或接近"值"职位:38

with q(qqyy, firstday, lastday) as (
values
('Q4_10', '09-30-2010', '12-31-2010'),
('Q1_11', '12-31-2010', '03-31-2011'),
('Q2_11', '03-31-2011', '06/30/2011'),
('Q3_11', '06/30/2011', '09/30/2011'),
('Q4_11', '09/30/2011', '12/31/2011'),
('Q1_12', '12/31/2011', '03/31/2012'),
('Q2_12', '03/31/2012', '06/30/2012'),
('Q3_12', '06/30/2012', '09/30/2012'),
('Q4_12', '09/30/2012', '12/31/2012'),
('Q1_13', '12/31/2012', '03/31/2013'),
('Q2_13', '03/31/2013', '06/30/2013'),
('Q3_13', '06/30/2013', '09/30/2013'),
('Q4_13', '09/30/2013', '12/31/2013'),
('Q1_14', '12/31/2013', '03/31/2014'),
('Q2_14', '03/31/2014', '06/30/2014'),
('Q3_14', '06/30/2014', '09/30/2014'),
('Q4_14', '09/30/2014', '12/31/2014'),
('Q1_15', '12/31/2014', '03/31/2015'),
('Q2_15', '03/31/2015', '06/30/2015'),
('Q3_15', '06/30/2015', '09/30/2015'),
('Q4_15', '09/30/2015', '12/31/2015'),
('Q1_16', '12/31/2015', '03/31/2016'),
('Q2_16', '03/31/2016', '06/30/2016'),
('Q3_16', '06/30/2016', '09/30/2016'),
('Q4_16', '09/30/2016', '12/31/2016')
  )

SELECT q.qqyy, cobrand_id, sum(calc)
into   temp_08.cmg_calc
from temp_08.cmg s 
join q on 
s.transaction_date >= q.firstday 
and s.transaction_date <= q.lastday
GROUP BY q.qqyy, cobrand_id;

上面的查询似乎停留在&#34;值&#34;由于Redshift使用较旧版本的postgresql(http://docs.aws.amazon.com/redshift/latest/dg/c_unsupported-postgresql-features.html)。但由于某种原因,以下查询也使用&#34;值&#34;工作良好。知道如何让上面的查询使用redshift工作吗?

 create table temp_08.cmgquarters (
quarter_col text
  , date_from   date
  , date_to     date
);


 insert into temp_08.cmgquarters
   values
('Q4_10', '09-30-2010', '12-31-2010'),
('Q1_11', '12-31-2010', '03-31-2011'),
('Q2_11', '03-31-2011', '06/30/2011'),
('Q3_11', '06/30/2011', '09/30/2011'),
('Q4_11', '09/30/2011', '12/31/2011'),
('Q1_12', '12/31/2011', '03/31/2012'),
('Q2_12', '03/31/2012', '06/30/2012'),
('Q3_12', '06/30/2012', '09/30/2012'),
('Q4_12', '09/30/2012', '12/31/2012'),
('Q1_13', '12/31/2012', '03/31/2013'),
('Q2_13', '03/31/2013', '06/30/2013'),
('Q3_13', '06/30/2013', '09/30/2013'),
('Q4_13', '09/30/2013', '12/31/2013'),
('Q1_14', '12/31/2013', '03/31/2014'),
('Q2_14', '03/31/2014', '06/30/2014'),
('Q3_14', '06/30/2014', '09/30/2014'),
('Q4_14', '09/30/2014', '12/31/2014'),
('Q1_15', '12/31/2014', '03/31/2015'),
('Q2_15', '03/31/2015', '06/30/2015'),
('Q3_15', '06/30/2015', '09/30/2015'),
('Q4_15', '09/30/2015', '12/31/2015'),
('Q1_16', '12/31/2015', '03/31/2016'),
('Q2_16', '03/31/2016', '06/30/2016'),
('Q3_16', '06/30/2016', '09/30/2016'),
('Q4_16', '09/30/2016', '12/31/2016');

2 个答案:

答案 0 :(得分:0)

我不太了解Postgres,但是使用SQL-Server,你不能直接使用Function LogNormProb(Data As Range, Probability As Double) As Double Dim Cell As Range Dim lnRange As Variant, lnAvg As Double Dim lnStdDev As Double For Each Cell In Data lnRange = WorksheetFunction.Ln(Cell.Value) Next Cell lnAvg = WorksheetFunction.Average(lnRange) lnStdDev = WorksheetFunction.StdDev.S(lnRange) LogNormProb = WorksheetFunction.LogNorm.INV(Probability, lnAvg, lnStdDev) End Function 表。必须使用括号并提供带有列名的表别名来定义派生表。

这将是这样的:

VALUES

注意

你处于危险之中!

  • 您正在使用与文化相关的日期格式。这可能适用于您的系统,但可以在另一个系统中使用...
  • 此外,你甚至不一致!
  • 您的with q as ( select * from ( values ('Q4_10', '09-30-2010', '12-31-2010'), ('Q1_11', '12-31-2010', '03-31-2011'), ('Q2_11', '03-31-2011', '06/30/2011'), ('Q3_11', '06/30/2011', '09/30/2011'), ('Q4_11', '09/30/2011', '12/31/2011'), ('Q1_12', '12/31/2011', '03/31/2012'), ('Q2_12', '03/31/2012', '06/30/2012'), ('Q3_12', '06/30/2012', '09/30/2012'), ('Q4_12', '09/30/2012', '12/31/2012'), ('Q1_13', '12/31/2012', '03/31/2013'), ('Q2_13', '03/31/2013', '06/30/2013'), ('Q3_13', '06/30/2013', '09/30/2013'), ('Q4_13', '09/30/2013', '12/31/2013'), ('Q1_14', '12/31/2013', '03/31/2014'), ('Q2_14', '03/31/2014', '06/30/2014'), ('Q3_14', '06/30/2014', '09/30/2014'), ('Q4_14', '09/30/2014', '12/31/2014'), ('Q1_15', '12/31/2014', '03/31/2015'), ('Q2_15', '03/31/2015', '06/30/2015'), ('Q3_15', '06/30/2015', '09/30/2015'), ('Q4_15', '09/30/2015', '12/31/2015'), ('Q1_16', '12/31/2015', '03/31/2016'), ('Q2_16', '03/31/2016', '06/30/2016'), ('Q3_16', '06/30/2016', '09/30/2016'), ('Q4_16', '09/30/2016', '12/31/2016') ) AS tbl(qqyy, firstday, lastday) ) SELECT * from q 将日期值设为字符串。

在SQL-Server中,我建议使用ISO8601,unseparated或 - 我最喜欢的 - ODBC。但我确信在Postgres中也有文化独立的文字日期格式。

我建议让CTE返回键入值或使用带有类型列的临时表。

答案 1 :(得分:0)

由于Redshift不支持values()作为“表格替换”,您需要将其重新编写为union

with q(qqyy, firstday, lastday) as (
  select 'Q4_10', '09-30-2010', '12-31-2010' union all
  select 'Q1_11', '12-31-2010', '03-31-2011' union all
  ....
)
SELECT ...;

但是你应该使用正确的DATE文字:

with q(qqyy, firstday, lastday) as (
  select 'Q4_10', DATE '2010-09-30', DATE '2010-12-31' union all
  select 'Q1_11', DATE '2010-12-31', DATE '2011-03-31' union all
  ....
)
SELECT ...;