计算case语句生成的结果

时间:2015-04-25 05:03:54

标签: sql oracle

我正在处理一个分析两条记录的查询,并检查一条记录是否与第二条记录匹配。如果记录1列匹配记录2列,则没有错误。如果记录1列与记录2列不匹配则存在错误。我希望能够计算每个YPID的错误。对于我的生活......我无法弄明白。救命啊!

这是我的疑问:

select r1.ypid, r1.business_name as Base_Listed_Name, r1.street_address as Base_Listed_Address, r1.city as Base_Listed_City, r1.state as Base_Listed_State, r1.zip5 as Base_Listed_Zip, rmve1.value as Base_URL,
r2.business_name as Google_Scanned_Listed_Name, r2.street_address as Google_Scanned_Listed_Address, r2.city as Google_Scanned_Listed_City, r2.state as Google_Scanned_Listed_City, r2.zip5 as Google_Scanned_Listed_Zip, rmve2.value as Google_Scanned_URL,
ls.presence_score, ls.listing_score,
case
when r1.business_name = r2.business_name then 'no_error'
else 'error'
end Business_Name_Status,
case
when r1.latitude = r2.latitude then  'no_error'
when r1.longitude = r2.longitude then  'no_error'
when r1.latitude is null or r1.latitude in ('0') then (case when r1.street_address = r2.street_address then 'no_error' else 'error' end)
else 'error'
end Street_Address_Status,
case
when r1.city = r2.city then 'no_error'
else 'error'
end City_Status,
case
when r1.state = r2.state then 'no_error'
else 'error'
end State_Status,
case
when r1.zip5 = r2.zip5 then 'no_error'
else 'error'
end Zip_Status,
case
when lower(replace(replace(replace(replace(replace(replace(rmve1.value, 'http://www.', null), 'www.', null), 'https://www.', null), 'http://', null), 'https://', null), 'wwww.', null)) 
= lower(replace(replace(replace(replace(replace(replace(rmve2.value, 'http://www.', null), 'www.', null), 'https://www.', null), 'http://', null), 'https://', null), 'wwww.', null)) then 'no_error'
else 'error'
end URL_Status
from mdm2.records r1 join mdm2.records r2 on r1.ypid = r2.ypid join mdm2.presence_listing_statuses ls on r1.ypid = ls.ypid 
left outer join mdm2.record_mult_val_exts rmve1 on r1.id = rmve1.record_id and rmve1.extension_type = 'urls' and rmve1.value_type = 'primary'
left outer join mdm2.record_mult_val_exts rmve2 on r2.id = rmve2.record_id and rmve2.extension_type = 'urls' and rmve2.value_type = 'primary'
where r1.ypid in ('5625222','13846403','21974776','22806234','30303664','453728041')
and r1.source_code = 'PPA'
and r2.source_code = 'GOOG';  

results
|YPID|BASE_LISTED_NAME|BASE_LISTED_ADDRESS|BASE_LISTED_CITY|BASE_LISTED_STATE|BASE_LISTED_ZIP|BASE_URL|GOOGLE_SCANNED_LISTED_NAME|GOOGLE_SCANNED_LISTED_ADDRESS|GOOGLE_SCANNED_LISTED_CITY|GOOGLE_SCANNED_LISTED_CITY_1|GOOGLE_SCANNED_LISTED_ZIP|GOOGLE_SCANNED_URL|PRESENCE_SCORE|LISTING_SCORE|BUSINESS_NAME_STATUS|STREET_ADDRESS_STATUS|CITY_STATUS|STATE_STATUS|ZIP_STATUS|URL_STATUS|
|5625222|Affinity Insurance Agency|5702 S Staples St Suite G|Corpus Christi|TX|78413|http://affinityia.com|Affinity Insurance Agency|5702 S Staples St Suite G|Corpus Christi|TX|78413|http://corpuschristiinsuranceprovider.com|90|97|no_error|no_error|no_error|no_error|no_error|error|
|13846403|Party Bazaar|4435 Lovers Ln|Dallas|TX|75225|www.partybazaardallas.com|Party Bazaar|4435 Lovers Ln|Dallas|TX|75225|http://partybazaardallas.com|93|100|no_error|no_error|no_error|no_error|no_error|no_error|
|21974776|Alterations To Go|2100 Arden Way Ste 150|Sacramento|CA|95825|http://www.alterationstogosacramento.com|Alterations To Go|2100 Arden Way # 150|Sacramento|CA|95825|http://alterations-togo.com|91|99|no_error|no_error|no_error|no_error|no_error|error|
|22806234|RV America|3640 Chambers Rd|Aurora|CO|80011|http://www.rvamericainc.com/pages/rv%20financing|RV America|3640 Chambers Rd|Aurora|CO|80011|http://rvamericainc.com|83|91|no_error|no_error|no_error|no_error|no_error|error|
|30303664|Sorelli Hair Studio & Spa|400 Saint Andrews Blvd|Melbourne|FL|32940|http://sorellihairstudio.com|Sorelli Hair Studio & Spa|400 Saint Andrews Blvd|Melbourne|FL|32940|http://sorellihairstudio.com|91|99|no_error|no_error|no_error|no_error|no_error|no_error|
|453728041|Carolina Driving School|534 Walkertown Guthrie Rd|Winston Salem|NC|27101|http://www.carolinadriving.com|Carolina Driving School|534 Walkertown Guthrie Rd|Winston Salem|NC|27101|http://carolinadrivingschool.com|87|97|no_error|no_error|no_error|no_error|no_error|error|

1 个答案:

答案 0 :(得分:0)

我猜你需要使用LEAD,LAG功能。

点击此链接http://oracle-base.com/articles/misc/lag-lead-analytic-functions.php

LAG功能用于访问前一行的数据 LEAD函数用于从下一行返回数据。

通过这个你可以比较第一行和第二行