【发布时间】:2019-07-12 04:45:31
【问题描述】:
最初我正在尝试使用函数但无济于事。 Redshift 不允许表返回类型或从表中选择数据。我希望也许有一种方法可以使用存储过程来做到这一点,但我不再有信心了。我为下面列出的代码创建了一个 SP,是否可以为 SP 将两个查询连接到自身?
尝试做一个功能,但不可能发生
YTD current year
select
year
, month
, revenue
,transactiondate
, Flag
from (
select
extract(year from transactiondate) as year
, to_char(transactiondate, 'Mon') as month
, extract(month from transactiondate) as month_number
, sum(netamount) as revenue
,transactiondate
,Flag
from
vw_costs_of_businesss_copy
--where to_date(transactiondate, 'YYYY-MM-DD') <= '2019-06-07' and to_date(transactiondate, 'YYYY-MM-DD') ->= concat(to_char(extract(year from '2019-01-01'), 'YYYY'),'01-01') --Convert the date param to year and concatenate with '01/01'
--where to_date(transactiondate, 'YYYY-MM-DD') <= '2019-06-07' and to_date(concat(to_char(extract(year from to_date('2019-01-01', 'YYYY-MM-DD'))),'01-01') , 'YYYY-MM-DD') >= '2019-01-01' --Convert the date param to year and concatenate with '01/01'
--where to_date(transactiondate, 'YYYY-MM-DD') <= '2019-06-07' and to_date(concat(to_char(Cas(extract(year from to_date('2019-01-01', 'YYYY-MM-DD')) as text ),'-01-01') , 'YYYY-MM-DD') >= '2019-01-01' --Convert the date param to year and concatenate with '01/01'
--where to_date(transactiondate, 'YYYY-MM-DD') <= '2019-06-07' and to_date(concat(to_char(Cast(extract(year from to_date('2019-01-01', 'YYYY-MM-DD')) as Text),'0000'),'-01') , 'YYYY-MM-DD') >= '2019-01-01' --Convert the date param to year and concatenate with '01/01'
where to_date(transactiondate, 'YYYY-MM-DD') <= '2019-06-07' and to_date(transactiondate, 'YYYY-MM-DD')>= to_date(concat(to_char(Cast(extract(year from to_date('2019-01-01', 'YYYY-MM-DD')) as Text),'0000'),'-01') , 'YYYY-MM-DD')
group by
year
, month
, month_number
,transactiondate
, Flag
)
order by month_number, year
--YTD PREV YEAR
select
year
, month
, revenue
,transactiondate
, Flag
from (
select
extract(year from transactiondate) as year
, to_char(transactiondate, 'Mon') as month
, extract(month from transactiondate) as month_number
, sum(netamount) as revenue
,transactiondate
,Flag
from
vw_costs_of_businesss_copy
--where to_date(transactiondate, 'YYYY-MM-DD') <= '2019-06-07' and to_date(transactiondate, 'YYYY-MM-DD') ->= concat(to_char(extract(year from '2019-01-01'), 'YYYY'),'01-01') --Convert the date param to year and concatenate with '01/01'
--where to_date(transactiondate, 'YYYY-MM-DD') <= '2019-06-07' and to_date(concat(to_char(extract(year from to_date('2019-01-01', 'YYYY-MM-DD'))),'01-01') , 'YYYY-MM-DD') >= '2019-01-01' --Convert the date param to year and concatenate with '01/01'
--where to_date(transactiondate, 'YYYY-MM-DD') <= '2019-06-07' and to_date(concat(to_char(Cas(extract(year from to_date('2019-01-01', 'YYYY-MM-DD')) as text ),'-01-01') , 'YYYY-MM-DD') >= '2019-01-01' --Convert the date param to year and concatenate with '01/01'
--where to_date(transactiondate, 'YYYY-MM-DD') <= '2019-06-07' and to_date(concat(to_char(Cast(extract(year from to_date('2019-01-01', 'YYYY-MM-DD')) as Text),'0000'),'-01') , 'YYYY-MM-DD') >= '2019-01-01' --Convert the date param to year and concatenate with '01/01'
where to_date(transactiondate, 'YYYY-MM-DD') <= '2019-06-07' and to_date(transactiondate, 'YYYY-MM-DD')>= to_date(concat(to_char(Cast(extract(year from to_date('2019-01-01', 'YYYY-MM-DD')-1) as Text),'0000'),'-01') , 'YYYY-MM-DD')
group by
year
, month
, month_number
,transactiondate
, Flag
)
order by month_number, year
它需要列出 YTD 和 prev_YTD 以便我可以将其作为参数传递,这可能吗?我意识到我必须为此制作一张桌子。 我在哪里放置 Prev_year、Prev-month 列
【问题讨论】:
标签: stored-procedures amazon-redshift