首页 > 解决方案 > 获取所有超过 25 个字符的记录 (LENGTH(hcp.phone_number) > 25)

问题描述

我试图根据这个条件从我的查询中获取所有记录但没有成功:

(LENGTH(hcp.phone_number) > 25)

实际上我正在使用这个条件:

CASE WHEN LENGTH(hcp.phone_number) > 25 then null else hcp.phone_number END

但是,对于单个帐号,当hcp.phone_number字段大于 25 时,它只会向我显示 NULL。

我的查询工作正常,但我想修改它以获取符合此条件的所有帐号:

LENGTH(hcp.phone_number) > 25

这是我的查询:

SELECT
    hp.party_name                              
  , hca.account_number
  , hca.cust_account_id                        
 -- , hcsu.LOCATION customer_site_name
  , hcas.cust_acct_site_id                     
  , hcp.phone_number
  , hcp.email_address
  , CASE WHEN LENGTH(hcp.phone_number) > 25 then null else hcp.phone_number END
  , hl.address1
  , hl.address2
  , hl.address3
  , hl.address4
  , hl.city
  , hl.province
  , hl.postal_code
  , hcas.status  as hcas_status                               
  , DECODE( hcas.attribute5, 'PUP', 'Y', 'N' ) 
  , hca.status  as hca_status                             
FROM apps.hz_cust_accounts hca
INNER JOIN apps.hz_cust_acct_sites_all hcas ON hca.cust_account_id = hcas.cust_account_id
INNER JOIN apps.hz_party_sites hps ON hcas.party_site_id = hps.party_site_id
INNER JOIN apps.hz_locations hl ON hps.location_id = hl.location_id
INNER JOIN apps.hz_parties hp ON hps.party_id = hp.party_id
LEFT JOIN (
        SELECT
            owner_table_id
          , max(case when contact_point_type = 'PHONE' then phone_number end) phone_number
          , max(case when contact_point_type = 'EMAIL' then email_address end) email_address
        FROM hz_contact_points
        WHERE status = 'A'
        AND primary_flag = 'Y'
        AND owner_table_name = 'HZ_PARTY_SITES'
        AND contact_point_type IN ('EMAIL','PHONE')
        GROUP BY 
            owner_table_id
    ) hcp ON hcas.party_site_id = hcp.owner_table_id 
WHERE hcas.status = 'A'
AND hps.status = 'A'
AND hca.status = 'A'
AND hca.account_number = ''
;

你能帮我解决这个问题吗?

标签: sql

解决方案


我认为您应该将您选择的列更改为此

, CASE WHEN LENGTH(hcp.phone_number) > 25 then hcp.phone_number END

并删除条件AND hca.account_number = ''

如果您想在结果中过滤掉所有使用 LENGTH(hcp.phone_number) <= 25 记录的内容,请在WHERE子句中添加条件:AND LENGTH(hcp.phone_number) > 25


推荐阅读