如何使 row_number () 在 case 子句之后工作?
How do I make row_number () work after a case clause?
我在 SQL 服务器上应用此 row_number() 函数时遇到问题。理想情况下,我想使用函数 dense_rank() 但仅在 Oracle 上编写时才有效。
当我 运行 查询时,我得到 SQL 错误 [207][S0001]:列名称无效 'tag_year'。
select
'last 7 days' as time_period,
CAST(dd.CalendarDate as DATE) as 'Date',
dd.CalendarID,
cast(CAST(dd.CalendarDate as DATE) as varchar) as time,
right(CAST(dd.CalendarDate as DATE), 5) as axis_date,
(case
when dd.CalendarDate between dateadd(month, -11,getdate()) and getdate() then 'cy'
else 'ly' end) as tag_year,
'DAY' + CAST(
ROW_NUMBER () over (partition by tag_year order by time desc ) as varchar) as time_index, --calendar week day
ROW_NUMBER() over (partition by tag_year order by time desc ) as last_n,
CAST(last_n AS varchar)+time_period as time_key
from dim.Calendar dd
where
(dd.CalendarDate between getdate() - 7 and getdate() - 1) -- last 31 days
or (dd.CalendarDate between getdate() - 7 - 52 * 7 and getdate() - 1 - 52 * 7);
不确定是什么问题,当我添加分组依据时,它仍然崩溃。
如有任何帮助或指导,我们将不胜感激
tag_year
是在查询中内联创建的表达式的结果。要在另一个表达式上使用它的值,您需要首先将此值创建为子查询中的列。
例如:
select *,
'DAY' + CAST(
ROW_NUMBER () over (partition by tag_year order by time desc ) as varchar)
as time_index, --calendar week day
ROW_NUMBER() over (partition by tag_year order by time desc ) as last_n,
from (
select
'last 7 days' as time_period,
CAST(dd.CalendarDate as DATE) as 'Date',
dd.CalendarID,
cast(CAST(dd.CalendarDate as DATE) as varchar) as time,
right(CAST(dd.CalendarDate as DATE), 5) as axis_date,
case
when dd.CalendarDate between dateadd(month, -11,getdate()) and getdate()
then 'cy'
else 'ly'
end as tag_year,
CAST(last_n AS varchar)+time_period as time_key
from dim.Calendar dd
where
(dd.CalendarDate between getdate() - 7 and getdate() - 1) -- last 31 days
or (dd.CalendarDate between getdate() - 7 - 52 * 7 and getdate() - 1 - 52 * 7)
) x
我在 SQL 服务器上应用此 row_number() 函数时遇到问题。理想情况下,我想使用函数 dense_rank() 但仅在 Oracle 上编写时才有效。
当我 运行 查询时,我得到 SQL 错误 [207][S0001]:列名称无效 'tag_year'。
select
'last 7 days' as time_period,
CAST(dd.CalendarDate as DATE) as 'Date',
dd.CalendarID,
cast(CAST(dd.CalendarDate as DATE) as varchar) as time,
right(CAST(dd.CalendarDate as DATE), 5) as axis_date,
(case
when dd.CalendarDate between dateadd(month, -11,getdate()) and getdate() then 'cy'
else 'ly' end) as tag_year,
'DAY' + CAST(
ROW_NUMBER () over (partition by tag_year order by time desc ) as varchar) as time_index, --calendar week day
ROW_NUMBER() over (partition by tag_year order by time desc ) as last_n,
CAST(last_n AS varchar)+time_period as time_key
from dim.Calendar dd
where
(dd.CalendarDate between getdate() - 7 and getdate() - 1) -- last 31 days
or (dd.CalendarDate between getdate() - 7 - 52 * 7 and getdate() - 1 - 52 * 7);
不确定是什么问题,当我添加分组依据时,它仍然崩溃。
如有任何帮助或指导,我们将不胜感激
tag_year
是在查询中内联创建的表达式的结果。要在另一个表达式上使用它的值,您需要首先将此值创建为子查询中的列。
例如:
select *,
'DAY' + CAST(
ROW_NUMBER () over (partition by tag_year order by time desc ) as varchar)
as time_index, --calendar week day
ROW_NUMBER() over (partition by tag_year order by time desc ) as last_n,
from (
select
'last 7 days' as time_period,
CAST(dd.CalendarDate as DATE) as 'Date',
dd.CalendarID,
cast(CAST(dd.CalendarDate as DATE) as varchar) as time,
right(CAST(dd.CalendarDate as DATE), 5) as axis_date,
case
when dd.CalendarDate between dateadd(month, -11,getdate()) and getdate()
then 'cy'
else 'ly'
end as tag_year,
CAST(last_n AS varchar)+time_period as time_key
from dim.Calendar dd
where
(dd.CalendarDate between getdate() - 7 and getdate() - 1) -- last 31 days
or (dd.CalendarDate between getdate() - 7 - 52 * 7 and getdate() - 1 - 52 * 7)
) x