从列中的所有值到新列的最大值
Max value from all values in column to new column
我需要在 select 语句中添加新列,其中包含来自所有记录的最大年份(存储为文本),然后将其标记为 CY/PY。才两年总是在table.
year value
2019 value1
2019 value2
2018 value3
到目前为止我已经试过了,但是 returns 我只从那一行开始,而不是整列。
select year, value, IIF(year=MAX(year), 'CY', 'PY') AS "CY/PY" from table
预计:
year value CY/PY
2019 value1 CY
2019 value2 CY
2018 value3 PY
尝试使用 MAX
作为解析函数:
WITH cte AS (
SELECT year, value, MAX(year) OVER () AS max_year
FROM yourTable
)
SELECT
year,
value,
IIF(year = max_year, 'CY', 'PY') AS "CY/PY"
FROM cte;
我想你想要一个 window 函数:
select year, value,
(case when year = MAX(year) over () then 'CY' else 'PY' end) AS "CY/PY"
from table
尝试在 IIF 中使用子查询:
select year, value, IIF(year=(select MAX(year) from table), 'CY', 'PY') AS "CY/PY" from table
我需要在 select 语句中添加新列,其中包含来自所有记录的最大年份(存储为文本),然后将其标记为 CY/PY。才两年总是在table.
year value
2019 value1
2019 value2
2018 value3
到目前为止我已经试过了,但是 returns 我只从那一行开始,而不是整列。
select year, value, IIF(year=MAX(year), 'CY', 'PY') AS "CY/PY" from table
预计:
year value CY/PY
2019 value1 CY
2019 value2 CY
2018 value3 PY
尝试使用 MAX
作为解析函数:
WITH cte AS (
SELECT year, value, MAX(year) OVER () AS max_year
FROM yourTable
)
SELECT
year,
value,
IIF(year = max_year, 'CY', 'PY') AS "CY/PY"
FROM cte;
我想你想要一个 window 函数:
select year, value,
(case when year = MAX(year) over () then 'CY' else 'PY' end) AS "CY/PY"
from table
尝试在 IIF 中使用子查询:
select year, value, IIF(year=(select MAX(year) from table), 'CY', 'PY') AS "CY/PY" from table