SELECT p.PROJECT_NO,ROWNUM FROM MRP_PROJECT_PLAN p WHERE EXISTS(SELECT 'X' FROM FAULT_REPORT r WHERE r.PROJECT_NO = p.PROJECT_NO) AND ROWNUM > 5 AND ROWNUM < 15 GROUP BY p.PROJECT_NO,ROWNUM ORDER BY ROWNUM此SQL中 ROWNUM >0 < 10可以查出数据 如果改为ROWNUM > 5则查不出数据 怎么解决
时间: 2024-03-05 14:51:46 浏览: 98
在 Oracle 数据库中,ROWNUM 是一个伪列,它表示返回结果集中每一行的行号。在查询中使用 ROWNUM 时需要注意以下几点:
1. ROWNUM 是在结果集返回后才会分配的,因此在 WHERE 子句中使用 ROWNUM 没有意义,只能在结果集返回后使用 ROWNUM 进行过滤。
2. 当 ROWNUM > n 时,Oracle 数据库会返回前 n 行结果并将其丢弃,然后将行号重新开始计数。因此,如果您要选择特定范围的行,请使用子查询或其他方法,如使用 RANK 或 DENSE_RANK 函数。
在这个查询中,如果您想要选择第 6 - 15 行的数据,可以使用子查询将其包装起来,如下所示:
```
SELECT * FROM (
SELECT p.PROJECT_NO, ROWNUM rn
FROM MRP_PROJECT_PLAN p
WHERE EXISTS (
SELECT 'X' FROM FAULT_REPORT r WHERE r.PROJECT_NO = p.PROJECT_NO
)
) WHERE rn BETWEEN 6 AND 15;
```
在这个查询中,首先选择 MRP_PROJECT_PLAN 表中存在故障报告的项目编号,然后使用子查询将结果集的行号 ROWNUM 存储在 rn 列中。最后,在外部查询中选择 rn 值在 6 到 15 之间的行,以选择特定范围内的行。
相关问题
SELECT CS.CLAIMS_ID, CE.LONG_NAME CORPORATENAME, CS.PATIENT_NAME, CS.EMPLOYEE_NAME,CS.DEPARTMENT,E.SUBSIDIARY ,E.BRANCH, CS.ADMISSION_DATE, CS.DISCHARGEABLE_DATE , CS.PROVIDER_NAME, CS.PANEL_PROVIDER, TRIM(DS.ICD_CODE)||', '||DS.DESCRIPTION DIAGNOSISNAME, CS.MC_TAKEN_DAY ,CR.DESCRIPTION COVERAGE_DESCRIPTION, SD2.FDESC CLAIMTYPE,CS.REMARKS, CS.DUE_TOTAL, CS.PAID_TO_CLAIMANT, CS.PAID_BY_CLAIMANT, CS.AUTHORIZATION_CODE, CS.SERVICE_DATE,CS.RECORD_NO,CS.SUB_RECORD_NO,CS.PLAN_ID,CS.TRANSMISSION_DATE,CS.CLAIMS_REC_DATE, CS.CLAIMS_STATUS ,CS.APPROVED_BY, CS.HOSP_INVOICE_NO, CS.TERMINAL_ID, CS.TERMINAL_TYPE, CS.DEDUCTIBLE,CS.POLICY_NO,CS.PAYEE_NAME,CS.CARD_NO, CS.DOCRCV_BY, CS.CENTRE_CODE ,CS.DOCUMENT_NO,CS.MRN , NVL((SELECT UPLDT FROM (SELECT UPLDT FROM SYT_ATTACHDOC LD WHERE TO_NUMBER(TRIM(LD.KEY1))=CS.CLAIMS_ID AND LD.MATERIAL_TYPE IN('申诉材料','补充材料','调查材料') AND ROWNUM<2 ORDER BY UPLDT DESC) WHERE ROWNUM<2),CS.SERVICE_DATE) AS LAST_DOCUMENT_RECEIVED_DATE FROM CLAIMS CS, CORPORATE CE, COVERAGE_MASTER CR, SYC_REFCD SD1, SYC_REFCD SD2, DIAGNOSIS DS , EMPLOYEE E WHERE CS.COVERAGE_ID = CR.COVERAGE_CODE AND CS.CORPORATE_CODE = CE.CORP_CODE AND CS.PRIMARY_DIAGNOSIS = DS.ICD_CODE AND CS.CLAIM_TYPE = SD1.REFCD AND CS.CARD_NO = E.CARD_NO AND SD1.VAR1 = SD2.REFCD AND SD1.MODID = 'ES' AND SD1.REFGRP = 'CLAIMTYP' AND SD2.MODID = 'ES' AND SD2.REFGRP = 'CLAIM_APPLICABLE' AND CS.PLAN_ID!='TEST-2023-GLOBAL-PLAN-DEMO' AND (CS.PAYOR_CODE,CS.CENTRE_CODE) IN (SELECT SYFIELD(STNCD,'*',1,1), SYFIELD(STNCD,'*',2,1) FROM SYM_USRSTN WHERE USRID='SYSTEM' AND STNTYP IN ('PC')) AND (CS.CORPORATE_CODE IN (SELECT STNCD FROM SYM_USRSTN WHERE USRID='SYSTEM' AND STNTYP IN ('PY','CO')) OR (CS.PAYOR_CODE,CS.CENTRE_CODE) IN (SELECT SYFIELD(STNCD,'*',1,1), SYFIELD(STNCD,'*',2,1) FROM SYM_USRSTN WHERE USRID='SYSTEM' AND STNTYP IN ('PC'))) AND CS.CLAIMS_REC_DATE>=TO_DATE('1999-01-01','yyyy-MM-dd') AND CS.CLAIMS_REC_DATE<TO_DATE('2099-01-01','yyyy-MM-dd')+1
这是一个 SQL 查询语句,根据不同的表和条件,查询出一些索赔相关的信息,包括索赔 ID、公司名称、患者姓名、员工姓名、部门、分公司、入院日期、出院日期、供应商名称、面板供应商、诊断名称、医疗保险覆盖范围描述、索赔类型、备注、到期总额、向索赔人支付的金额、由索赔人支付的金额、授权代码、服务日期、记录编号、子记录编号、计划 ID、传输日期、索赔记录日期、索赔状态、批准人、医院发票号、终端 ID、终端类型、免赔额、保单号、收款人姓名、卡号、收到文件的人、中心代码、文件编号、病历号和最后一次收到文件的日期。需要注意的是,这个查询排除了测试计划,并且只查询系统中特定类型的用户可以访问的索赔信息。
SELECT CUST_ID agentId, AGE_NAME agentName, countReleased, dayAmountReleased, CASE o.agelimitType WHEN '1' THEN TO_CHAR(o.agelimitNum) ELSE TO_CHAR(o.issueamtlimit) END countLimit, CASE o.agelimitType WHEN '1' THEN '不限' ELSE TO_CHAR(o.daylimitamount) END dayMaxCount FROM (SELECT m.CUST_ID, m.AGE_NAME, ad.countReleased, t.dayAmountReleased, d.daylimitamount, d.issueamtlimit, (SELECT AGE_LIMIT_NUM FROM P_ACT_ALLOW_MANAGER WHERE ACTIVITY_ID = #{activityId} AND rownum = 1 ) AS agelimitNum, (SELECT AGE_LIMIT_NUM_TYPE FROM P_ACT_MANAGER_ACTIVITY WHERE ACTIVITY_ID = #{activityId} ) AS agelimitType FROM (SELECT c1.CUST_ID, c1.AGE_NAME FROM (SELECT CUST_ID, AGE_NAME FROM PM_AGET_INFO START WITH CUST_ID = #{agetId} CONNECT BY PRIOR CUST_ID = FATH_CUST_ID ) c1 INNER JOIN (SELECT DISTINCT CUST_ID, AGE_NAME FROM PM_AGET_INFO START WITH CUST_ID in (SELECT CUST_ID FROM P_ACT_ALLOW_MANAGER WHERE ACTIVITY_ID = #{activityId} ) CONNECT BY PRIOR CUST_ID = FATH_CUST_ID ) c2 ON c1.CUST_ID = c2.CUST_ID ) m LEFT JOIN (SELECT CUST_ID, count(RECORD_ID) countReleased FROM P_ACT_MANAGER_RECORD WHERE ACTIVITY_ID = #{activityId} AND RECORD_STATUS NOT IN (2, 5) AND to_date(CREATE_TIME, 'yyyy-MM-dd hh24:mi:ss') BETWEEN to_date(#{beginTime}, 'yyyyMMddhh24miss') AND to_date(#{endTime}, 'yyyyMMddhh24miss') GROUP BY CUST_ID ) ad ON m.CUST_ID = ad.CUST_ID LEFT JOIN (SELECT AGE_ID cust_id, DAY_LIMIT_AMOUNT daylimitAmount, ISSUE_AMT_LIMIT issueAmtLimit FROM P_ACT_MANAGER_LIMIT_ALLOT WHERE ACT_ID =#{activityId} ) d ON m.CUST_ID = d.CUST_ID LEFT JOIN (SELECT CUST_ID, count(RECORD_ID) dayAmountReleased FROM P_ACT_MANAGER_RECORD WHERE ACTIVITY_ID =#{activityId} AND RECORD_STATUS NOT IN (2, 5) AND to_date(CREATE_TIME, 'yyyy-MM-dd hh24:mi:ss') BETWEEN to_date(#{todayBegin}, 'yyyyMMddhh24miss') AND to_date(#{todayend}, 'yyyyMMddhh24miss') GROUP BY CUST_ID ) t ON m.CUST_ID = t.CUST_ID) o WHERE 1 = 1 AND o.CUST_ID = #{agentId} AND o.AGE_NAME = #{agentName} ORDER BY o.countReleased DESC NULLS LAST 如何优化此段sql代码,使得更加简洁?
这段 SQL 代码可以进行以下优化:
1. 使用 WITH 子句来代替嵌套查询,增加可读性。
2. 使用 LEFT JOIN 和 COALESCE 函数来代替子查询,避免多次查询。
3. 使用 CASE WHEN THEN ELSE END 语句来代替 IF 函数。
优化后的 SQL 代码如下:
WITH
cte1 AS (
SELECT
CUST_ID,
AGE_NAME
FROM
PM_AGET_INFO
START WITH
CUST_ID = #{agetId}
CONNECT BY
PRIOR CUST_ID = FATH_CUST_ID
),
cte2 AS (
SELECT
DISTINCT CUST_ID,
AGE_NAME
FROM
PM_AGET_INFO
START WITH
CUST_ID IN (
SELECT
CUST_ID
FROM
P_ACT_ALLOW_MANAGER
WHERE
ACTIVITY_ID = #{activityId}
)
CONNECT BY
PRIOR CUST_ID = FATH_CUST_ID
),
cte3 AS (
SELECT
CUST_ID,
count(RECORD_ID) countReleased
FROM
P_ACT_MANAGER_RECORD
WHERE
ACTIVITY_ID = #{activityId}
AND RECORD_STATUS NOT IN (2, 5)
AND to_date(CREATE_TIME, 'yyyy-MM-dd hh24:mi:ss') BETWEEN to_date(#{beginTime}, 'yyyyMMddhh24miss') AND to_date(#{endTime}, 'yyyyMMddhh24miss')
GROUP BY
CUST_ID
),
cte4 AS (
SELECT
CUST_ID,
count(RECORD_ID) dayAmountReleased
FROM
P_ACT_MANAGER_RECORD
WHERE
ACTIVITY_ID =#{activityId}
AND RECORD_STATUS NOT IN (2, 5)
AND to_date(CREATE_TIME, 'yyyy-MM-dd hh24:mi:ss') BETWEEN to_date(#{todayBegin}, 'yyyyMMddhh24miss') AND to_date(#{todayend}, 'yyyyMMddhh24miss')
GROUP BY
CUST_ID
),
cte5 AS (
SELECT
AGE_LIMIT_NUM
FROM
P_ACT_ALLOW_MANAGER
WHERE
ACTIVITY_ID = #{activityId}
AND rownum = 1
),
cte6 AS (
SELECT
AGE_LIMIT_NUM_TYPE
FROM
P_ACT_MANAGER_ACTIVITY
WHERE
ACTIVITY_ID = #{activityId}
),
cte7 AS (
SELECT
AGE_ID cust_id,
DAY_LIMIT_AMOUNT daylimitAmount,
ISSUE_AMT_LIMIT issueAmtLimit
FROM
P_ACT_MANAGER_LIMIT_ALLOT
WHERE
ACT_ID =#{activityId}
)
SELECT
m.CUST_ID agentId,
m.AGE_NAME agentName,
COALESCE(ad.countReleased, 0) countReleased,
COALESCE(t.dayAmountReleased, 0) dayAmountReleased,
CASE
WHEN o.agelimitType = '1' THEN TO_CHAR(o.agelimitNum)
ELSE TO_CHAR(o.issueamtlimit)
END countLimit,
CASE
WHEN o.agelimitType = '1' THEN '不限'
ELSE TO_CHAR(o.daylimitamount)
END dayMaxCount
FROM
cte1 m
INNER JOIN cte2 c ON m.CUST_ID = c.CUST_ID AND m.AGE_NAME = c.AGE_NAME
LEFT JOIN cte3 ad ON m.CUST_ID = ad.CUST_ID
LEFT JOIN cte4 t ON m.CUST_ID = t.CUST_ID
LEFT JOIN cte5 ON 1 = 1
LEFT JOIN cte6 ON 1 = 1
LEFT JOIN cte7 d ON m.CUST_ID = d.CUST_ID
LEFT JOIN (
SELECT
COALESCE(AGE_LIMIT_NUM, 0) agelimitNum,
COALESCE(AGE_LIMIT_NUM_TYPE, '1') agelimitType
FROM
dual
) o ON 1 = 1
WHERE
m.CUST_ID = #{agentId}
AND m.AGE_NAME = #{agentName}
ORDER BY
COALESCE(ad.countReleased, 0) DESC NULLS LAST
这样可以使得代码更加简洁易读,并且避免了重复查询和嵌套查询的问题。
阅读全文