减少并分组记录并替换字段中的值

时间:2016-09-30 10:54:06

标签: sql oracle duplicates

出于示例目的,我在查询中仅选择了两个修复引用。输出显示每个修复引用的多个记录。对于每次修复,可以有多个序列,每个序列都有一个交易人员(如下所示):

╔══════════════════╦══════════════╦══════════════╦════════════╦═══════════════╗
║ Repair Reference ║ Tenant Name  ║ Job Sequence ║ Trade Code ║ Trade         ║
╠══════════════════╬══════════════╬══════════════╬════════════╬═══════════════╣
║         57257795 ║ MISS L SMITH ║            1 ║         41 ║ Plumber       ║
║         57257795 ║ MISS L SMITH ║           10 ║         40 ║ Plasterer     ║
║         57257795 ║ MISS L SMITH ║           11 ║         40 ║ Plasterer     ║
║         57257795 ║ MISS L SMITH ║           13 ║         40 ║ Plasterer     ║
║         57257795 ║ MISS L SMITH ║           14 ║         44 ║ Electrician   ║
║         57257795 ║ MISS L SMITH ║           15 ║         44 ║ Electrician   ║
║         57257795 ║ MISS L SMITH ║           17 ║         40 ║ Plasterer     ║
║         57257795 ║ MISS L SMITH ║           18 ║         41 ║ Plumber       ║
║         57257795 ║ MISS L SMITH ║           19 ║         41 ║ Plumber       ║
║         57257795 ║ MISS L SMITH ║            2 ║         41 ║ Plumber       ║
║         57257795 ║ MISS L SMITH ║           20 ║         32 ║ Joiner        ║
║         57257795 ║ MISS L SMITH ║            3 ║         40 ║ Plasterer     ║
║         57257795 ║ MISS L SMITH ║            5 ║         32 ║ Joiner        ║
║         57257795 ║ MISS L SMITH ║            6 ║         40 ║ Plasterer     ║
║         57257795 ║ MISS L SMITH ║            7 ║         40 ║ Plasterer     ║
║         57257795 ║ MISS L SMITH ║            8 ║         32 ║ Joiner        ║
║         57342819 ║ MISS A GREEN ║            1 ║         40 ║ Plasterer     ║
║         57342819 ║ MISS A GREEN ║            2 ║         40 ║ Plasterer     ║
║         57342819 ║ MISS A GREEN ║            3 ║         33 ║ Painter       ║
║         57342819 ║ MISS A GREEN ║            4 ║         33 ║ Painter       ║
║         57342819 ║ MISS A GREEN ║            5 ║         33 ║ Painter       ║
║         57342819 ║ MISS A GREEN ║            6 ║         40 ║ Plasterer     ║
╚══════════════════╩══════════════╩══════════════╩════════════╩═══════════════╝

客户端只需要根据修复参考显示一条记录(示例如下所示) - 我可以使用Job_Sequence = 1执行此操作。如果每个Trade有多个Multi Trade类型,我遇到的问题是让Trade显示Repair Reference。这就是我所挣扎的。是否可以查看所有Trades per Repair Reference,如果有多种类型,则显示文本多重交易,否则只有一个Trade/Trade Code Repair Reference然后只使用交易:

╔══════════════════╦══════════════╦══════════════╦════════════╦══════════════╗
║ Repair Reference ║ Tenant Name  ║ Job Sequence ║ Trade Code ║    Trade     ║
╠══════════════════╬══════════════╬══════════════╬════════════╬══════════════╣
║         57257795 ║ MISS L SMITH ║            1 ║         41 ║ Multi Trade  ║
║         57342819 ║ MISS A GREEN ║            1 ║         40 ║ Multi Trade  ║
╚══════════════════╩══════════════╩══════════════╩════════════╩══════════════╝

这是我目前获得的SQL:

SELECT
rhm_job_seqs.repair_ref AS "Repair Reference",
rhm_repairs.tenant_name AS "Tenant Name",
rhm_repairs.tenant_address1 AS "Tenant Address1",
rhm_repairs.tenant_address2 AS "Tenant Address2",
rhm_repairs.tenant_postcode AS "Tenant PostCode",
rhm_job_seqs.job_seq AS "Job Sequence",
rhm_trades.trade_code AS "Trade Code",
rhm_trades.trade_desc AS "Trade"
FROM (
RHM_REPAIRS
INNER JOIN RHM_JOB_SEQS ON RHM_REPAIRS.REPAIR_REF = RHM_JOB_SEQS.REPAIR_REF)
INNER JOIN RHM_OPERATORS ON RHM_JOB_SEQS.OPERATOR_ID1 = RHM_OPERATORS.OPERATOR_ID
INNER JOIN rhm_repair_type ON rhm_repairs.repair_type = rhm_repair_type.repair_type
INNER JOIN rhm_trades ON rhm_job_seqs.trade_code = rhm_trades.trade_code
INNER JOIN rhm_subareas ON rhm_repairs.subarea_no = rhm_subareas.subarea_no
INNER JOIN rhm_yhn_areas ON rhm_subareas.yhn_area_no = rhm_yhn_areas.area_no 
WHERE
NOT (RHM_REPAIRS.TENANT_ADDRESS1 LIKE '9999Z' OR
RHM_REPAIRS.TENANT_ADDRESS1 LIKE '9999z%' OR
RHM_REPAIRS.TENANT_ADDRESS1 LIKE 'block' OR
RHM_REPAIRS.TENANT_ADDRESS1 LIKE 'Block' OR
RHM_REPAIRS.TENANT_ADDRESS1 LIKE 'Block%' OR
RHM_REPAIRS.TENANT_ADDRESS1 LIKE 'BLOCK')
AND NOT (RHM_REPAIRS.JOB_PRIORITY LIKE 'I' OR RHM_REPAIRS.JOB_PRIORITY LIKE 'V')
AND (RHM_REPAIRS.REPAIR_TYPE LIKE '2' OR
RHM_REPAIRS.REPAIR_TYPE LIKE 'G' OR
RHM_REPAIRS.REPAIR_TYPE LIKE 'S' OR
RHM_REPAIRS.REPAIR_TYPE LIKE 'X')
AND RHM_REPAIRS.TENANT_NAME <> 'VOID'
AND RHM_REPAIRS.TENANT_NAME <> 'Void'
AND RHM_REPAIRS.TENANT_NAME IS NOT NULL
AND RHM_JOB_SEQS.JOB_SEQ_STATUS <> 'X'
AND rhm_repairs.repair_completed BETWEEN SYSDATE-20 AND SYSDATE
AND RHM_REPAIRS.SUBAREA_NO <> '99'
AND rhm_job_seqs.repair_ref IN ('57342819','57257795')
AND rhm_job_seqs.job_seq = '1'
ORDER BY
RHM_JOB_SEQS.REPAIR_REF, rhm_job_seqs.job_seq

2 个答案:

答案 0 :(得分:0)

我尝试使用row_number()

import {BrandsService} from './brands.service';

export default [BrandsService];

export {BrandsService}

答案 1 :(得分:0)

这可能有用 - 基本上每行我都会询问是否存在更高的工作顺序:

SELECT
    rhm_job_seqs.repair_ref AS "Repair Reference",
    rhm_repairs.tenant_name AS "Tenant Name",
    rhm_repairs.tenant_address1 AS "Tenant Address1",
    rhm_repairs.tenant_address2 AS "Tenant Address2",
    rhm_repairs.tenant_postcode AS "Tenant PostCode",
    rhm_job_seqs.job_seq AS "Job Sequence",
    rhm_trades.trade_code AS "Trade Code",
    CASE WHEN EXISTS (
        SELECT *
        FROM RHM_JOB_SEQS job_seqs2
        WHERE rhm_job_seqs.repair_ref = job_seqs2.repair_ref
            AND rhm_job_seqs.trade_code <> job_seqs2.trade_code
            AND job_seqs2.job_seq > 1
    )
    THEN 'Multi Trade'
    ELSE rhm_trades.trade_desc
    END AS "Trade"
FROM RHM_REPAIRS
INNER JOIN RHM_JOB_SEQS ON RHM_REPAIRS.REPAIR_REF = RHM_JOB_SEQS.REPAIR_REF
INNER JOIN RHM_OPERATORS ON RHM_JOB_SEQS.OPERATOR_ID1 = RHM_OPERATORS.OPERATOR_ID
INNER JOIN rhm_repair_type ON rhm_repairs.repair_type = rhm_repair_type.repair_type
INNER JOIN rhm_trades ON rhm_job_seqs.trade_code = rhm_trades.trade_code
INNER JOIN rhm_subareas ON rhm_repairs.subarea_no = rhm_subareas.subarea_no
INNER JOIN rhm_yhn_areas ON rhm_subareas.yhn_area_no = rhm_yhn_areas.area_no 
WHERE
    NOT (
        RHM_REPAIRS.TENANT_ADDRESS1 LIKE '9999Z' OR
        RHM_REPAIRS.TENANT_ADDRESS1 LIKE '9999z%' OR
        RHM_REPAIRS.TENANT_ADDRESS1 LIKE 'block' OR
        /** RHM_REPAIRS.TENANT_ADDRESS1 LIKE 'Block' OR **/
        RHM_REPAIRS.TENANT_ADDRESS1 LIKE 'Block%' OR
        RHM_REPAIRS.TENANT_ADDRESS1 LIKE 'BLOCK'
    )
    AND NOT (RHM_REPAIRS.JOB_PRIORITY LIKE 'I' OR RHM_REPAIRS.JOB_PRIORITY LIKE 'V')
    AND (
        RHM_REPAIRS.REPAIR_TYPE LIKE '2' OR
        RHM_REPAIRS.REPAIR_TYPE LIKE 'G' OR
        RHM_REPAIRS.REPAIR_TYPE LIKE 'S' OR
        RHM_REPAIRS.REPAIR_TYPE LIKE 'X'
    )
    AND RHM_REPAIRS.TENANT_NAME <> 'VOID'
    AND RHM_REPAIRS.TENANT_NAME <> 'Void'
    AND RHM_REPAIRS.TENANT_NAME IS NOT NULL
    AND RHM_JOB_SEQS.JOB_SEQ_STATUS <> 'X'
    AND rhm_repairs.repair_completed BETWEEN SYSDATE-20 AND SYSDATE
    AND RHM_REPAIRS.SUBAREA_NO <> '99'
    AND rhm_job_seqs.repair_ref IN ('57342819','57257795')
    AND rhm_job_seqs.job_seq = '1'
ORDER BY RHM_JOB_SEQS.REPAIR_REF, rhm_job_seqs.job_seq

我还评论了一个无用的条件。