Oracle SQL中多个重复行超过3时如何更改值

0md85ypi  于 2023-01-30  发布在  Oracle
关注(0)|答案(1)|浏览(128)

我正在尝试选择A. POLICY_NO或A. POLICY_TITLE值是否重复超过3行,我希望将C. SMALL_CATEGORY_TITLE值更改为"Z",并且还希望将B. SMALL_CATEGORY_SID值转换为空。
我使用了3个表,其中YIP. YOUTH_POLICY为A,YIP. YOUTH_POLICY_AREA为B,YIP. YOUTH_SMALL_CATEGORY为C。PK和FK为政策编号。
例如,如果表
| A.政策_否|B.小类别SID|C.小类别标题|A.政策标题|
| - ------|- ------|- ------|- ------|
| 1个|八十|A类|数值1|
| 1个|九十|乙|数值1|
| 1个|九十五|C级|数值1|
| 第二章|八十|A类|数值2|
| 第二章|九十|乙|数值2|
| 第二章|九十五|C级|数值2|
| 三个|八十|A类|数值3|
| 三个|九十|乙|数值3|
| 四个|八十|A类|数值4|
我想选择类似
| A.政策_否|B.小类别SID|C.小类别标题|A.政策标题|
| - ------|- ------|- ------|- ------|
| 1个|零|Z型|数值1|
| 第二章|零|Z型|数值2|
| 三个|八十|A类|数值3|
| 三个|九十|乙|数值3|
| 四个|八十|A类|数值4|
这是查询选择除了当重复值大于3时,

SELECT
 A.POLICY_NO
 , B.SMALL_CATEGORY_SID
 , C.SMALL_CATEGORY_TITLE
 , A.POLICY_TITLE
 , COUNT(*) OVER() AS TOTAL_COUNT
FROM
 YIP.YOUTH_POLICY A
 LEFT JOIN
 YIP.YOUTH_POLICY_AREA B
 ON A.POLICY_NO = B.POLICY_NO
 LEFT JOIN
 YIP.YOUTH_SMALL_CATEGORY C
 ON B.SMALL_CATEGORY_SID = C.SMALL_CATEGORY_SID
 WHERE A.POLICY_NO IN (SELECT 
                        F.POLICY_NO 
                        FROM YIP.YOUTH_POLICY F 
                        LEFT JOIN
                         YIP.YOUTH_POLICY_AREA G
                         ON F.POLICY_NO = G.POLICY_NO
                         LEFT JOIN
                         YIP.YOUTH_SMALL_CATEGORY H
                         ON G.SMALL_CATEGORY_SID = H.SMALL_CATEGORY_SID
                        GROUP BY F.POLICY_NO 
                        HAVING COUNT(*) < 3)
ORDER BY A.POLICY_NO;

当POLICY_NO值重复超过3个时,我尝试更改C. SMALL_CATEGORY_TITLE值

SELECT
 A.POLICY_NO
 --, B.SMALL_CATEGORY_SID
 , SUM(CASE WHEN C.SMALL_CATEGORY_TITLE IN (SELECT 
                                        F.POLICY_NO 
                                        FROM YIP.YOUTH_POLICY F 
                                        LEFT JOIN
                                         YIP.YOUTH_POLICY_AREA G
                                         ON F.POLICY_NO = G.POLICY_NO
                                         LEFT JOIN
                                         YIP.YOUTH_SMALL_CATEGORY H
                                         ON G.SMALL_CATEGORY_SID = H.SMALL_CATEGORY_SID
                                        GROUP BY F.POLICY_NO 
                                        HAVING COUNT(*) > 2) THEN 1 ELSE NULL END) AS 'Z'
 , A.POLICY_TITLE
 , COUNT(*) OVER() AS TOTAL_COUNT
FROM
 YIP.YOUTH_POLICY A
 LEFT JOIN
 YIP.YOUTH_POLICY_AREA B
 ON A.POLICY_NO = B.POLICY_NO
 LEFT JOIN
 YIP.YOUTH_SMALL_CATEGORY C
 ON B.SMALL_CATEGORY_SID = C.SMALL_CATEGORY_SID
ORDER BY A.POLICY_NO;

我收到SQL错误[42000]:JDBC-8006:缺少FROM关键字。¶第17行第59列为空¶ HAVING COUNT(*)〉2)THEN 1 ELSE NULL END)AS 'Z'¶
有什么办法可以修好吗?我想了5个多小时,但是我修不好

q3aa0525

q3aa05251#

诀窍是使用窗口函数COUNT(*)OVER来获得整个行集的计数,以便以后对每一行做出决定。最后可以使用DISTINCT或GROUP BY将其折叠。

SELECT DISTINCT
       policy_no,
       CASE WHEN (policy_count >= 3 OR policy_title_count>= 3) THEN NULL 
            ELSE small_category_sid 
       END AS small_category_sid,
       CASE WHEN (policy_count >= 3 OR policy_title_count>= 3) THEN 'Z'
            ELSE small_category_title
       END AS small_category_title,
       policy_title
  FROM (SELECT x.*,
               COUNT(*) OVER (PARTITION BY policy_no) policy_count,
               COUNT(*) OVER (PARTITION BY policy_title) policy_title_count
          FROM yip.youth_policy x)

相关问题