Advertisement
naimul64

Recovering mobile number using parent_name and NID

Jul 4th, 2016
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 4.27 KB | None | 0 0
  1. -- get the count of valid formatted mobile numbers against a parent name and national id
  2. SELECT
  3.     t.parent_name_unicode,
  4.     t.nationalidoriginal,
  5.     COUNT(DISTINCT t.mobile_number) validMobileCount,
  6.     GROUP_CONCAT(DISTINCT t.mobile_number
  7.         SEPARATOR ',  ')
  8. FROM
  9.     `stipend_info_latest_experiment` t
  10. WHERE
  11.     ((SUBSTR(t.`mobile_number`, 1, 3) IN ('017' , '016', '015', '019', '018', '011')
  12.         AND LENGTH(t.`mobile_number`) = 11)
  13.         OR (SUBSTR(t.`mobile_number`, 1, 2) IN ('17' , '16', '15', '19', '18', '11')
  14.         AND LENGTH(t.`mobile_number`) = 10))
  15.         AND t.parent_name_unicode is not NULL
  16.         AND t.nationalidoriginal is not NULL
  17.         AND lower(t.nationalidoriginal) != lower('NULL')
  18. GROUP BY t.parent_name_unicode , t.nationalidoriginal
  19. ORDER BY validMobileCount DESC;
  20.  
  21. -- if for same parent name and nId if there is more than one mobile number then view the wrong and correct
  22. -- there may be more than one proper formatted mobile number found. that's a problem. we will do if only one valid mobile number is found
  23. SELECT
  24.     f.parent_name_unicode,
  25.     f.nationalidoriginal,
  26.     f.mobile_number,
  27.     s.mobile_number
  28. FROM
  29.     `stipend_info_latest_experiment` f
  30.         INNER JOIN
  31.     `stipend_info_latest_experiment` s ON f.parent_name_unicode = s.parent_name_unicode
  32.         AND f.nationalidoriginal = s.nationalidoriginal
  33.         INNER JOIN
  34.     (SELECT
  35.         COUNT(DISTINCT t.mobile_number) validMobileCount,
  36.             t.mobile_number
  37.     FROM
  38.         `stipend_info_latest_experiment` t
  39.     WHERE
  40.         ((SUBSTR(t.`mobile_number`, 1, 3) IN ('017' , '016', '015', '019', '018', '011')
  41.             AND LENGTH(t.`mobile_number`) = 11)
  42.             OR (SUBSTR(t.`mobile_number`, 1, 2) IN ('17' , '16', '15', '19', '18', '11')
  43.             AND LENGTH(t.`mobile_number`) = 10))
  44.             AND t.mobile_flag = 0 AND t.amount_flag = 0 AND t.flag = 0
  45.     GROUP BY t.parent_name_unicode , t.nationalidoriginal
  46.     having validMobileCount = 1) t
  47.     ON f.mobile_number = t.mobile_number
  48. WHERE
  49.     f.mobile_number != s.mobile_number
  50.         AND f.nationalidoriginal != 'Null'
  51.         AND ((SUBSTR(f.`mobile_number`, 1, 3) IN ('017' , '016', '015', '019', '018', '011')
  52.         AND LENGTH(f.`mobile_number`) = 11)
  53.         OR (SUBSTR(f.`mobile_number`, 1, 2) IN ('17' , '16', '15', '19', '18', '11')
  54.         AND LENGTH(f.`mobile_number`) = 10))
  55.         AND NOT ((SUBSTR(s.`mobile_number`, 1, 3) IN ('017' , '016', '015', '019', '018', '011')
  56.         AND LENGTH(s.`mobile_number`) = 11)
  57.         OR (SUBSTR(s.`mobile_number`, 1, 2) IN ('17' , '16', '15', '19', '18', '11')
  58.         AND LENGTH(s.`mobile_number`) = 10));
  59.  
  60. -- if same (parent name, nId) have different mobile name and only one of them is in correct format replace the wrong ones with the correct
  61. UPDATE
  62.     `stipend_info_latest_experiment` f
  63.         INNER JOIN
  64.     `stipend_info_latest_experiment` s ON f.parent_name_unicode = s.parent_name_unicode
  65.         AND f.nationalidoriginal = s.nationalidoriginal
  66.         INNER JOIN
  67.     (SELECT
  68.         COUNT(DISTINCT t.mobile_number) validMobileCount,
  69.             t.mobile_number
  70.     FROM
  71.         `stipend_info_latest_experiment` t
  72.     WHERE
  73.         ((SUBSTR(t.`mobile_number`, 1, 3) IN ('017' , '016', '015', '019', '018', '011')
  74.             AND LENGTH(t.`mobile_number`) = 11)
  75.             OR (SUBSTR(t.`mobile_number`, 1, 2) IN ('17' , '16', '15', '19', '18', '11')
  76.             AND LENGTH(t.`mobile_number`) = 10))
  77.     GROUP BY t.parent_name_unicode , t.nationalidoriginal
  78.     having validMobileCount = 1) t
  79.     ON f.mobile_number = t.mobile_number
  80.     set s.mobile_number = f.mobile_number
  81. WHERE
  82.     f.mobile_number != s.mobile_number
  83.         AND f.nationalidoriginal != 'Null'
  84.         AND
  85.         ((SUBSTR(f.`mobile_number`, 1, 3) IN ('017' , '016', '015', '019', '018', '011')
  86.         AND LENGTH(f.`mobile_number`) = 11)
  87.         OR (SUBSTR(f.`mobile_number`, 1, 2) IN ('17' , '16', '15', '19', '18', '11')
  88.         AND LENGTH(f.`mobile_number`) = 10))
  89.         AND NOT
  90.         ((SUBSTR(s.`mobile_number`, 1, 3) IN ('017' , '016', '015', '019', '018', '011')
  91.         AND LENGTH(s.`mobile_number`) = 11)
  92.         OR (SUBSTR(s.`mobile_number`, 1, 2) IN ('17' , '16', '15', '19', '18', '11')
  93.         AND LENGTH(s.`mobile_number`) = 10));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement