有条件地以大写形式显示值
Conditionally displaying a value in upper case
我有一个非常简单的查询,但我需要这样,如果 Conditional
小于二,那么 Title
应该显示为全部大写
SELECT DISTINCT Title, COUNT(Title) AS NumberOfTitles
FROM Table
WHERE Conditional < 3
GROUP BY Title
ORDER BY Title
我试过 CASE
等,但似乎可以正常工作
如果条件 = 1
,我还需要将标题设置为 'MAIN'
SELECT
case when conditional = 1 then 'MAIN'
when conditional <> 1 and conditional < 2 then UPPER(Title)
else Title end as Title
, COUNT(Title) AS NumberOfTitles
FROM Table
GROUP BY case when conditional = 1 then 'MAIN'
when conditional <> 1 and conditional < 2 then UPPER(Title)
else Title end
ORDER BY case when conditional = 1 then 'MAIN'
when conditional <> 1 and conditional < 2 then UPPER(Title)
else Title end
您可以使用 case
语句进行尝试。
我会将逻辑包装在一个通用的 table 表达式中,以避免在 group by
和 order by
子句中重复它:
WITH cte AS (
SELECT
CASE WHEN conditional < 2 THEN UPPER(Title)
ELSE Title
END AS Title
FROM Table
)
SELECT Title, COUNT(Title) AS NumberOfTitles
FROM cte
GROUP BY Title
ORDER BY Title;
我有一个非常简单的查询,但我需要这样,如果 Conditional
小于二,那么 Title
应该显示为全部大写
SELECT DISTINCT Title, COUNT(Title) AS NumberOfTitles
FROM Table
WHERE Conditional < 3
GROUP BY Title
ORDER BY Title
我试过 CASE
等,但似乎可以正常工作
如果条件 = 1
,我还需要将标题设置为 'MAIN'SELECT
case when conditional = 1 then 'MAIN'
when conditional <> 1 and conditional < 2 then UPPER(Title)
else Title end as Title
, COUNT(Title) AS NumberOfTitles
FROM Table
GROUP BY case when conditional = 1 then 'MAIN'
when conditional <> 1 and conditional < 2 then UPPER(Title)
else Title end
ORDER BY case when conditional = 1 then 'MAIN'
when conditional <> 1 and conditional < 2 then UPPER(Title)
else Title end
您可以使用 case
语句进行尝试。
我会将逻辑包装在一个通用的 table 表达式中,以避免在 group by
和 order by
子句中重复它:
WITH cte AS (
SELECT
CASE WHEN conditional < 2 THEN UPPER(Title)
ELSE Title
END AS Title
FROM Table
)
SELECT Title, COUNT(Title) AS NumberOfTitles
FROM cte
GROUP BY Title
ORDER BY Title;