我一直在“定义不明确的列”。
WITH CountData as (
SELECT BOARD_RATE_T1,
BOARD_RATE_T3,
RENTAL_START_DATE,
RENTAL_END_DATE,
(BOARD_RATE_T3 - BOARD_RATE_T1) AS Countt
FROM PPSS.PA_OA_RATE_JUST
),
GroupCount AS
(
SELECT BOARD_RATE_T1,
BOARD_RATE_T3,
Countt,
CASE
WHEN Countt < 500 THEN 'Under 500'
WHEN Countt BETWEEN 500 AND 5000 THEN '500 - 5000'
WHEN Countt BETWEEN 5000 AND 10000 THEN '5000 - 10000'
WHEN Countt > 10000 THEN 'Over 10000'
ELSE 'Invalid Count'
END AS Countt
FROM CountData
)
SELECT COUNT(*) AS CountGrpCount,
Countt Groups
FROM GroupCount;
答案 0 :(得分:1)
最后一个查询引发错误,因为您引用的是Countt
,并且在GroupCount
中定义了两次。 Oracle不知道您的意思是什么。
一旦克服了模棱两可的列错误,您将无法进行最终查询。 Oracle严格要求对聚合查询中的每个非聚合值都使用GROUP BY
,这与MySQL有所不同。有一些例外,例如常量和诸如SYSDATE
之类的函数,但这超出了此问题的范围。
如果您要按“低于500”,“ 500-5000”等进行计数,那么以下是我注意到的有关查询的几件事:
您不需要BOARD_RATE_T1
,BOARD_RATE_T2
,RENTAL_START_DATE
和RENTAL_END_DATE
列。
可以通过稍微更改逻辑来缩短“费率差异”类别CASE
语句,并减少出错的可能性。
在ELSE
语句中达到CASE
条件的唯一方法是BOARD_RATE_T1
和/或BOARD_RATE_T3
为空。如果它们都是必需的(NOT NULL
)值,则可以省去ELSE
。
此查询将给出计数。如果您确实需要其他价值观,并且为了使问题更短而乱砍,我深表歉意:)
WITH CountData AS (
SELECT BOARD_RATE_T3 - BOARD_RATE_T1 AS RateDiff
FROM PPSS.PA_OA_RATE_JUST
),
GroupCount AS (
SELECT
CASE
WHEN RateDiff < 500 THEN 'Under 500'
WHEN RateDiff < 5000 THEN '500 - 5000'
WHEN RateDiff < 10000 THEN '5000 - 10000'
ELSE 'Invalid Count'
END AS RateDiffCategory
FROM CountData
)
SELECT
RateDiffCategory,
COUNT(*)
FROM GroupCount
GROUP BY
RateDiffCategory
ORDER BY
RateDiffCategory