排除列值以“836”开头的记录

Posted

技术标签:

【中文标题】排除列值以“836”开头的记录【英文标题】:Exclude records with column value starting with '836' 【发布时间】:2019-06-06 19:30:03 【问题描述】:

所以我的最新问题是我需要能够排除索赔编号以“836”开头的记录。这看起来很简单,但我尝试过的任何事情似乎都没有奏效,因为我仍在找回以“836”开头的索赔号码。我试过使用 NOT LIKE 和 NOT IN。

能否告知我做错了什么?我包括查询和我的 cmets。提前感谢您的意见。

DECLARE @StartDate AS DateTime = '1/1/2017'

IF OBJECT_ID('tempdb..##ProducerCommBridge') IS NOT NULL
  DROP TABLE ##ProducerCommBridge;

SELECT DISTINCT [ProducerCommBridgeKey]
      ,[PolicyAgreementNumber]
      ,[CommissionStartDate]
      ,[CommissionEndDate]
  INTO ##ProducerCommBridge
FROM [dbo].[DimProducerCommBridge]
;

IF OBJECT_ID('tempdb..##FctLossData') IS NOT NULL
  DROP TABLE ##FctLossData;

CREATE TABLE ##FctLossData (
    [QIPS_LPRG_SID] numeric(10,0) null,
    [PACClaimID] nvarchar(50) NULL,
    [EquipmentID] int null,
    [ClaimID] [int] NOT NULL,
    [ClaimNumber] [varchar](34) NOT NULL,
    [CreateDate] [datetime] NOT NULL,
    [LossDate] [datetime] NOT NULL,
    [ProCloseDate] [datetime] NULL,
    [ProducerCommBridgeKey] [int] NOT NULL,
    [ClaimStatus] [varchar](12) NULL,
    [ContractNumber] [varchar](30) NULL,
    [PolicyID] [varchar](20) NULL,
    [CustomerID] [nvarchar](15) NULL,
    [ProductID] [nvarchar](5) NULL,
    [CatastropheID] [int] NULL,
    [InsuranceClassCode] [varchar](1000) NULL,
    [EquipmentName] [varchar](1000) NULL,
    [EquipmentClassName] [varchar](1000) NULL,
    [EquipmentDescription] [nvarchar](1000) NULL,
    [RegionCode] [nvarchar](2) NULL,
    [CountryCode] [nvarchar](3) NULL,
    [ClaimLossType] [nvarchar](50) NULL,
    [CauseOfLoss] [nvarchar](255) NULL,
    [EnrollmentID] [nvarchar](50) NULL,
    [CoverageID] [int] NULL,
    [CoverageEffectiveDate] [datetime] NULL,
    [CustomerAccountNumber] [nvarchar](50) NULL,
    [PaidLossAmount] [money] NULL,
    [CaseReserveAmount] [money] NULL,
    [RecoveryReserveAmount] [money] NULL,
    [SalvageRecoveryAmount] [money] NULL,
    [SubroRecoveryAmount] [money] NULL,
    [TotalRecoveryAmount] [money] NULL,
    [AllocatedLAEAmount] [money] NULL
)
;

INSERT INTO ##FctLossData

--DECLARE @StartDate AS DateTime = '1/1/2017'

SELECT
  qle.LPRG_SID
  ,lc.PACClaimID
  ,ems.EquipmentID
  ,c.ClaimID
  ,(c.ClaimPrefix + '-' + CONVERT(varchar, c.ClaimNumber)) AS ClaimNumber
  ,CONVERT(date, CreateDate) AS CreateDate
  ,CONVERT(date, c.LossDate) AS LossDate
  ,CASE WHEN DATEPART(hh, EffectiveDate) < 17 then CONVERT(date, EffectiveDate) ELSE CONVERT(date, dateadd(dd, 1, EffectiveDate)) END as ProCloseDate
  ,-1 AS ProducerCommBridgeKey
  ,cs.ClaimStatus
  ,UPPER(LEFT(c.ContractNumber, 10)) AS ContractNumber
  ,CASE WHEN c.ContractNumber IS NOT NULL THEN LEFT(c.PolicyNumber, 7) ELSE c.PolicyNumber END AS PolicyID
  ,sCustomerID AS CustomerID
  ,CASE WHEN pro.sProductID = '9095' THEN '1095' ELSE pro.sProductID END AS ProductID
  ,c.CatastropheID
  ,CASE WHEN LTRIM(RTRIM(dbo.fn_clean_string(InsuranceClassCode))) = '' THEN 'UNKNOWN' ELSE COALESCE(UPPER(LTRIM(RTRIM(dbo.fn_clean_string(InsuranceClassCode)))), 'UNKNOWN')  END AS InsuranceClassCode
  ,CASE WHEN LTRIM(RTRIM(dbo.fn_clean_string(EquipmentName))) = '' THEN 'UNKNOWN' ELSE COALESCE(UPPER(LTRIM(RTRIM(dbo.fn_clean_string(EquipmentName)))), 'UNKNOWN')  END AS EquipmentName
  ,CASE WHEN EquipmentClassName IS NULL THEN 'UNKNOWN' ELSE UPPER(LTRIM(RTRIM(dbo.fn_clean_string(EquipmentClassName)))) END AS EquipmentClassName
  ,CASE WHEN EquipmentDescription IS NULL THEN 'UNKNOWN' ELSE UPPER(LTRIM(RTRIM(dbo.fn_clean_string(EquipmentDescription)))) END AS EquipmentDescription
  ,RegionCode
  ,CountryCode
  ,CASE WHEN ClaimLossType IS NULL THEN 'Unknown' ELSE ClaimLossType END AS ClaimLossType
  ,CASE WHEN CauseOfLoss IS NULL THEN 'Unknown' ELSE CauseOfLoss END AS CauseOfLoss
  ,CASE WHEN qc.CLAM_LPRG_SID IS NOT NULL THEN CONVERT(nvarchar(50), qle.LEAS_SID) WHEN lc.EnrollmentID IS NOT NULL THEN CONVERT(nvarchar(50), lc.EnrollmentID) ELSE NULL END AS EnrollmentID
  ,NULL AS CoverageID
  ,NULL AS CoverageEffectiveDate
  ,CASE WHEN qc.CLAM_LPRG_SID IS NOT NULL THEN CONVERT(nvarchar(50), qle.LEAS_LEASE_CODE) WHEN lc.EnrollmentID IS NOT NULL THEN CONVERT(nvarchar(50), lefs.ContractNumber) ELSE NULL END AS CustomerAccountNumber
  ,SUM(CASE WHEN Reserve = 0 AND Recovery = 0 AND TranTypeID = 1 THEN Amount END) AS PaidLossAmount
  ,SUM(CASE WHEN Reserve = 1 AND Recovery = 0 AND TranTypeID = 1 THEN Amount END) AS CaseReserveAmount
  ,SUM(CASE WHEN Reserve = 1 AND Recovery = 1 AND TranTypeID IN (1, 7, 8) THEN Amount END) AS RecoveryReserveAmount
  ,SUM(CASE WHEN Reserve = 0 AND Recovery = 1 AND TranTypeID = 8 THEN Amount END) AS SalvageRecoveryAmount
  ,SUM(CASE WHEN Reserve = 0 AND Recovery = 1 AND TranTypeID = 7 THEN Amount END) AS SubroRecoveryAmount
  ,SUM(CASE WHEN Reserve = 0 AND Recovery = 1 AND TranTypeID IN (1, 7, 8) THEN Amount END) AS TotalRecoveryAmount
  ,SUM(CASE WHEN Reserve = 0 AND Recovery = 0 AND TranTypeID = 6 THEN Amount END) AS AllocatedLAEAmount     
  FROM dbo.ExtPACTransaction t
  INNER JOIN dbo.ExtPACClaim c ON t.ClaimID = c.ClaimID
                              --AND c.ClaimNumber NOT LIKE '836%'
  INNER JOIN dbo.ExtPRSDEPolicy pol ON (CASE WHEN [ContractNumber] IS NOT NULL THEN LEFT(c.PolicyNumber, 7) ELSE c.PolicyNumber END) = pol.sPolicyID
  INNER JOIN dbo.ExtPRSDECustomer cus ON pol.iCustomerKey = cus.iCustomerKey
  INNER JOIN dbo.ExtPRSDEProduct pro ON pol.iProductKey = pro.iProductKey
  LEFT OUTER JOIN dbo.ExtPACClaimStatus cs ON c.ClaimID = cs.ClaimID
  LEFT OUTER JOIN dbo.ExtJWProductSegment jw ON pro.sProductID = jw.sProductID
  LEFT OUTER JOIN dbo.ExtEMSEquipment ems ON c.EMIEquipmentID = CONVERT(varchar, ems.EquipmentNumber)
  LEFT OUTER JOIN dbo.ExtQIPSClaims qc ON qc.CLAM_PAC_CLAIMID = c.ClaimID
  LEFT OUTER JOIN dbo.ExtQIPSLeaseEnrollment qle ON qle.LPRG_SID = qc.CLAM_LPRG_SID
  LEFT OUTER JOIN dbo.ExtLP2Claim lc ON lc.PACClaimID = c.ClaimID
  LEFT OUTER JOIN dbo.ExtLP2EnrollmentForSearch lefs ON lefs.EnrollmentID = lc.EnrollmentID
  WHERE c.LossDate >= @StartDate
  --AND c.ClaimNumber NOT LIKE '836%' -- To remove FIS claims
  --AND c.ClaimNumber NOT IN (SELECT c2.ClaimNumber FROM ExtPACClaim c2 WHERE ClaimNumber LIKE '836%')
  AND c.isVoided = 0
  AND (pro.sProductID = '9095' OR jw.sProductID IS NOT NULL)
  GROUP BY qle.LPRG_SID
    ,lc.PACClaimID
    ,ems.EquipmentID
    ,c.ClaimID
    ,(c.ClaimPrefix + '-' + CONVERT(varchar, c.ClaimNumber)) 
    ,CONVERT(date, CreateDate) 
    ,CONVERT(date, c.LossDate) 
    ,CASE WHEN DATEPART(hh, t.EffectiveDate) < 17 then CONVERT(date, t.EffectiveDate) ELSE CONVERT(date, dateadd(dd, 1, t.EffectiveDate)) END
    ,cs.ClaimStatus
    , UPPER(LEFT(c.ContractNumber, 10))
    ,CASE WHEN c.ContractNumber IS NOT NULL THEN LEFT(c.PolicyNumber, 7) ELSE c.PolicyNumber END 
    ,sCustomerID 
    ,CASE WHEN pro.sProductID = '9095' THEN '1095' ELSE pro.sProductID END 
    ,c.CatastropheID
    ,CASE WHEN LTRIM(RTRIM(dbo.fn_clean_string(InsuranceClassCode))) = '' THEN 'UNKNOWN' ELSE COALESCE(UPPER(LTRIM(RTRIM(dbo.fn_clean_string(InsuranceClassCode)))), 'UNKNOWN')  END
    ,CASE WHEN LTRIM(RTRIM(dbo.fn_clean_string(EquipmentName))) = '' THEN 'UNKNOWN' ELSE COALESCE(UPPER(LTRIM(RTRIM(dbo.fn_clean_string(EquipmentName)))), 'UNKNOWN')  END
    ,CASE WHEN EquipmentClassName IS NULL THEN 'UNKNOWN' ELSE UPPER(LTRIM(RTRIM(dbo.fn_clean_string(EquipmentClassName)))) END
    ,CASE WHEN EquipmentDescription IS NULL THEN 'UNKNOWN' ELSE UPPER(LTRIM(RTRIM(dbo.fn_clean_string(EquipmentDescription)))) END
    ,RegionCode
    ,CountryCode
    ,CASE WHEN ClaimLossType IS NULL THEN 'Unknown' ELSE ClaimLossType END
    ,CASE WHEN CauseOfLoss IS NULL THEN 'Unknown' ELSE CauseOfLoss END 
    ,CASE WHEN qc.CLAM_LPRG_SID IS NOT NULL THEN CONVERT(nvarchar(50), qle.LEAS_SID) WHEN lc.EnrollmentID IS NOT NULL THEN CONVERT(nvarchar(50), lc.EnrollmentID) ELSE NULL END
    ,CASE WHEN qc.CLAM_LPRG_SID IS NOT NULL THEN CONVERT(nvarchar(50), qle.LEAS_LEASE_CODE) WHEN lc.EnrollmentID IS NOT NULL THEN CONVERT(nvarchar(50), lefs.ContractNumber) ELSE NULL END

  --ORDER BY ClaimNumber DESC
;

--To view records
SELECT *
FROM ##FctLossData
WHERE ClaimNumber LIKE '836%'

【问题讨论】:

尝试添加一个(简单的)数据样本你的预期结果和你的实际结果.. 这看起来不像标准 SQL。这是什么 DBMS? 我看到您在查询中有“CONVERT(varchar, c.ClaimNumber))”,所以声明编号不是 varchar。查看在应用 LIKE 时是否有隐式转换为字符的情况。 (在我看来就像 SQL Server。) 您的查询看起来很完美。你能分享一些数据来检查吗? 【参考方案1】:

我添加了一个列 ClaimNumberWithoutPrefix 并基于此过滤。您最初的 ClaimNumber 在开头 c.ClaimPrefix + '-' 有类似的内容。请注意,如果 c.ClaimPrefix is null 则整个表达式 is null。为避免这种情况,您可以使用concat(c.ClaimPrefix + '-', c.ClaimNumber)

SELECT
  qle.LPRG_SID
  ,lc.PACClaimID
  ,ems.EquipmentID
  ,c.ClaimID
  ,(c.ClaimPrefix + '-' + CONVERT(varchar, c.ClaimNumber)) AS ClaimNumber
  ,ClaimNumber as ClaimNumberWithoutPrefix
  ,CONVERT(date, CreateDate) AS CreateDate
  ,CONVERT(date, c.LossDate) AS LossDate
  ,CASE WHEN DATEPART(hh, EffectiveDate) < 17 then CONVERT(date, EffectiveDate) ELSE CONVERT(date, dateadd(dd, 1, EffectiveDate)) END as ProCloseDate
  ,-1 AS ProducerCommBridgeKey
  ,cs.ClaimStatus
  ,UPPER(LEFT(c.ContractNumber, 10)) AS ContractNumber
  ,CASE WHEN c.ContractNumber IS NOT NULL THEN LEFT(c.PolicyNumber, 7) ELSE c.PolicyNumber END AS PolicyID
  ,sCustomerID AS CustomerID
  ,CASE WHEN pro.sProductID = '9095' THEN '1095' ELSE pro.sProductID END AS ProductID
  ,c.CatastropheID
  ,CASE WHEN LTRIM(RTRIM(dbo.fn_clean_string(InsuranceClassCode))) = '' THEN 'UNKNOWN' ELSE COALESCE(UPPER(LTRIM(RTRIM(dbo.fn_clean_string(InsuranceClassCode)))), 'UNKNOWN')  END AS InsuranceClassCode
  ,CASE WHEN LTRIM(RTRIM(dbo.fn_clean_string(EquipmentName))) = '' THEN 'UNKNOWN' ELSE COALESCE(UPPER(LTRIM(RTRIM(dbo.fn_clean_string(EquipmentName)))), 'UNKNOWN')  END AS EquipmentName
  ,CASE WHEN EquipmentClassName IS NULL THEN 'UNKNOWN' ELSE UPPER(LTRIM(RTRIM(dbo.fn_clean_string(EquipmentClassName)))) END AS EquipmentClassName
  ,CASE WHEN EquipmentDescription IS NULL THEN 'UNKNOWN' ELSE UPPER(LTRIM(RTRIM(dbo.fn_clean_string(EquipmentDescription)))) END AS EquipmentDescription
  ,RegionCode
  ,CountryCode
  ,CASE WHEN ClaimLossType IS NULL THEN 'Unknown' ELSE ClaimLossType END AS ClaimLossType
  ,CASE WHEN CauseOfLoss IS NULL THEN 'Unknown' ELSE CauseOfLoss END AS CauseOfLoss
  ,CASE WHEN qc.CLAM_LPRG_SID IS NOT NULL THEN CONVERT(nvarchar(50), qle.LEAS_SID) WHEN lc.EnrollmentID IS NOT NULL THEN CONVERT(nvarchar(50), lc.EnrollmentID) ELSE NULL END AS EnrollmentID
  ,NULL AS CoverageID
  ,NULL AS CoverageEffectiveDate
  ,CASE WHEN qc.CLAM_LPRG_SID IS NOT NULL THEN CONVERT(nvarchar(50), qle.LEAS_LEASE_CODE) WHEN lc.EnrollmentID IS NOT NULL THEN CONVERT(nvarchar(50), lefs.ContractNumber) ELSE NULL END AS CustomerAccountNumber
  ,SUM(CASE WHEN Reserve = 0 AND Recovery = 0 AND TranTypeID = 1 THEN Amount END) AS PaidLossAmount
  ,SUM(CASE WHEN Reserve = 1 AND Recovery = 0 AND TranTypeID = 1 THEN Amount END) AS CaseReserveAmount
  ,SUM(CASE WHEN Reserve = 1 AND Recovery = 1 AND TranTypeID IN (1, 7, 8) THEN Amount END) AS RecoveryReserveAmount
  ,SUM(CASE WHEN Reserve = 0 AND Recovery = 1 AND TranTypeID = 8 THEN Amount END) AS SalvageRecoveryAmount
  ,SUM(CASE WHEN Reserve = 0 AND Recovery = 1 AND TranTypeID = 7 THEN Amount END) AS SubroRecoveryAmount
  ,SUM(CASE WHEN Reserve = 0 AND Recovery = 1 AND TranTypeID IN (1, 7, 8) THEN Amount END) AS TotalRecoveryAmount
  ,SUM(CASE WHEN Reserve = 0 AND Recovery = 0 AND TranTypeID = 6 THEN Amount END) AS AllocatedLAEAmount     
  FROM dbo.ExtPACTransaction t
  INNER JOIN dbo.ExtPACClaim c ON t.ClaimID = c.ClaimID
                              --AND c.ClaimNumber NOT LIKE '836%'
  INNER JOIN dbo.ExtPRSDEPolicy pol ON (CASE WHEN [ContractNumber] IS NOT NULL THEN LEFT(c.PolicyNumber, 7) ELSE c.PolicyNumber END) = pol.sPolicyID
  INNER JOIN dbo.ExtPRSDECustomer cus ON pol.iCustomerKey = cus.iCustomerKey
  INNER JOIN dbo.ExtPRSDEProduct pro ON pol.iProductKey = pro.iProductKey
  LEFT OUTER JOIN dbo.ExtPACClaimStatus cs ON c.ClaimID = cs.ClaimID
  LEFT OUTER JOIN dbo.ExtJWProductSegment jw ON pro.sProductID = jw.sProductID
  LEFT OUTER JOIN dbo.ExtEMSEquipment ems ON c.EMIEquipmentID = CONVERT(varchar, ems.EquipmentNumber)
  LEFT OUTER JOIN dbo.ExtQIPSClaims qc ON qc.CLAM_PAC_CLAIMID = c.ClaimID
  LEFT OUTER JOIN dbo.ExtQIPSLeaseEnrollment qle ON qle.LPRG_SID = qc.CLAM_LPRG_SID
  LEFT OUTER JOIN dbo.ExtLP2Claim lc ON lc.PACClaimID = c.ClaimID
  LEFT OUTER JOIN dbo.ExtLP2EnrollmentForSearch lefs ON lefs.EnrollmentID = lc.EnrollmentID
  WHERE c.LossDate >= @StartDate
  --AND c.ClaimNumber NOT LIKE '836%' -- To remove FIS claims
  --AND c.ClaimNumber NOT IN (SELECT c2.ClaimNumber FROM ExtPACClaim c2 WHERE ClaimNumber LIKE '836%')
  AND c.isVoided = 0
  AND (pro.sProductID = '9095' OR jw.sProductID IS NOT NULL)
  GROUP BY qle.LPRG_SID
    ,lc.PACClaimID
    ,ems.EquipmentID
    ,c.ClaimID
    ,(c.ClaimPrefix + '-' + CONVERT(varchar, c.ClaimNumber)) 
    ,ClaimNumber
    ,CONVERT(date, CreateDate) 
    ,CONVERT(date, c.LossDate) 
    ,CASE WHEN DATEPART(hh, t.EffectiveDate) < 17 then CONVERT(date, t.EffectiveDate) ELSE CONVERT(date, dateadd(dd, 1, t.EffectiveDate)) END
    ,cs.ClaimStatus
    , UPPER(LEFT(c.ContractNumber, 10))
    ,CASE WHEN c.ContractNumber IS NOT NULL THEN LEFT(c.PolicyNumber, 7) ELSE c.PolicyNumber END 
    ,sCustomerID 
    ,CASE WHEN pro.sProductID = '9095' THEN '1095' ELSE pro.sProductID END 
    ,c.CatastropheID
    ,CASE WHEN LTRIM(RTRIM(dbo.fn_clean_string(InsuranceClassCode))) = '' THEN 'UNKNOWN' ELSE COALESCE(UPPER(LTRIM(RTRIM(dbo.fn_clean_string(InsuranceClassCode)))), 'UNKNOWN')  END
    ,CASE WHEN LTRIM(RTRIM(dbo.fn_clean_string(EquipmentName))) = '' THEN 'UNKNOWN' ELSE COALESCE(UPPER(LTRIM(RTRIM(dbo.fn_clean_string(EquipmentName)))), 'UNKNOWN')  END
    ,CASE WHEN EquipmentClassName IS NULL THEN 'UNKNOWN' ELSE UPPER(LTRIM(RTRIM(dbo.fn_clean_string(EquipmentClassName)))) END
    ,CASE WHEN EquipmentDescription IS NULL THEN 'UNKNOWN' ELSE UPPER(LTRIM(RTRIM(dbo.fn_clean_string(EquipmentDescription)))) END
    ,RegionCode
    ,CountryCode
    ,CASE WHEN ClaimLossType IS NULL THEN 'Unknown' ELSE ClaimLossType END
    ,CASE WHEN CauseOfLoss IS NULL THEN 'Unknown' ELSE CauseOfLoss END 
    ,CASE WHEN qc.CLAM_LPRG_SID IS NOT NULL THEN CONVERT(nvarchar(50), qle.LEAS_SID) WHEN lc.EnrollmentID IS NOT NULL THEN CONVERT(nvarchar(50), lc.EnrollmentID) ELSE NULL END
    ,CASE WHEN qc.CLAM_LPRG_SID IS NOT NULL THEN CONVERT(nvarchar(50), qle.LEAS_LEASE_CODE) WHEN lc.EnrollmentID IS NOT NULL THEN CONVERT(nvarchar(50), lefs.ContractNumber) ELSE NULL END

  --ORDER BY ClaimNumber DESC
;

--To view records
SELECT *
FROM ##FctLossData
WHERE ClaimNumberWithoutPrefix LIKE '836%'

【讨论】:

【参考方案2】:

您可以尝试在 LIKE 子句中使用[^] NotIn 模式匹配来过滤掉从 836 开始的值。我在下面给出了示例代码供您参考。阅读更多关于LIKE clause

CREATE TABLE #test(ClaimNumber VARCHAR(34))

INSERT INTO #test values('836789'),('567890'),('423897')

SELECT * FROM #test
WHERE claimNumber LIKE '[^8][^3][^6]%'

顺便说一句,我看到您使用的是CONVERT(VARCHAR, ClaimNumber)。始终指定 VARCHAR 的长度。如果您不指定长度,它将在不同的地方应用不同的默认长度。在 CONVERT 的情况下,它将应用 VARCHAR(30)。在表创建列定义中,它将应用VARCHAR(1)。我建议你阅读一下这个TSQL bad practice

【讨论】:

以上是关于排除列值以“836”开头的记录的主要内容,如果未能解决你的问题,请参考以下文章

R - 子集 - 基于列值的 grepl 选择排除行 [重复]

排除具有数字字符的行,仅在行的开头

C# 正则表达式 - 排除文本开头和结尾的下划线和空格

临时记录

php/Codeigniter——如何通过排除时间来仅比较日期

根据多个筛选条件 - SQL访问排除记录