Advertisement
naimul64

Some column base data modification

Jul 2nd, 2016
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 5.31 KB | None | 0 0
  1. -- copy data from one column to another
  2. UPDATE `stipend` SET parent_name_modified = parent_name_unicode;
  3. -- view if properly copied
  4. SELECT t.parent_name_unicode, t.parent_name_modified FROM `stipend_info_10JulDisb_1Jul-1` t;
  5. -- replace a string by another string in a column
  6. UPDATE `stipend_info_10JulDisb_1Jul-1` SET `parent_name_modified` = REPLACE(`parent_name_modified`,' বেগম','')
  7. -- create a table by copying an existing table
  8. create table `stipend_info_latest_experiment` as select * from `stipend_info`;
  9.  
  10.  
  11. -- if a parent have valid name and nId and mismatched more than one phone number, compare them
  12. select f.parent_name_unicode, f.nationalidoriginal, f.mobile_number, s.mobile_number from `stipend_info_10_11JulDis_2Jul_1` f
  13. inner join `stipend_info_10_11JulDis_2Jul_1` s
  14. on f.parent_name_unicode = s.parent_name_unicode and f.nationalidoriginal = s.nationalidoriginal
  15. where f.mobile_number != s.mobile_number and f.nationalidoriginal!='Null'
  16. and ( -- first mobiel valid
  17.   (SUBSTR(f.`mobile_number`,1,3) IN ('017','016','015','019','018','011') AND LENGTH(f.`mobile_number`) = 11)
  18.   OR
  19.   (SUBSTR(f.`mobile_number`,1,2) IN ('17','16','15','19','18','11') AND LENGTH(f.`mobile_number`) = 10)
  20. )
  21. and not ( -- second mobile invalid
  22.   (SUBSTR(s.`mobile_number`,1,3) IN ('017','016','015','019','018','011') AND LENGTH(s.`mobile_number`) = 11)
  23.   OR
  24.   (SUBSTR(s.`mobile_number`,1,2) IN ('17','16','15','19','18','11') AND LENGTH(s.`mobile_number`) = 10)
  25. );
  26.  
  27. -- get the count of valid formatted mobile numbers against a parent name and national id
  28. select count(distinct t.mobile_number) validMobileCount, Group_Concat(distinct t.mobile_number separator ',  ') from `stipend_info_10_11JulDis_2Jul_1` t
  29. where
  30. (
  31.   (SUBSTR(t.`mobile_number`,1,3) IN ('017','016','015','019','018','011') AND LENGTH(t.`mobile_number`) = 11)
  32.   OR
  33.   (SUBSTR(t.`mobile_number`,1,2) IN ('17','16','15','19','18','11') AND LENGTH(t.`mobile_number`) = 10)
  34. )
  35. group by t.parent_name_unicode, t.nationalidoriginal
  36. order by validMobileCount desc;
  37.  
  38. -- if for same parent name and nId if there is more than one mobile number then view the wrong and correct
  39. -- 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
  40. SELECT
  41.     f.parent_name_unicode,
  42.     f.nationalidoriginal,
  43.     f.mobile_number,
  44.     s.mobile_number
  45. FROM
  46.     `stipend_info_10_11JulDis_2Jul_1` f
  47.         INNER JOIN
  48.     `stipend_info_10_11JulDis_2Jul_1` s ON f.parent_name_unicode = s.parent_name_unicode
  49.         AND f.nationalidoriginal = s.nationalidoriginal
  50.         INNER JOIN
  51.     (SELECT
  52.         COUNT(DISTINCT t.mobile_number) validMobileCount,
  53.             t.mobile_number
  54.     FROM
  55.         `stipend_info_10_11JulDis_2Jul_1` t
  56.     WHERE
  57.         ((SUBSTR(t.`mobile_number`, 1, 3) IN ('017' , '016', '015', '019', '018', '011')
  58.             AND LENGTH(t.`mobile_number`) = 11)
  59.             OR (SUBSTR(t.`mobile_number`, 1, 2) IN ('17' , '16', '15', '19', '18', '11')
  60.             AND LENGTH(t.`mobile_number`) = 10))
  61.     GROUP BY t.parent_name_unicode , t.nationalidoriginal) t ON f.mobile_number = t.mobile_number
  62. WHERE
  63.     f.mobile_number != s.mobile_number
  64.         AND f.nationalidoriginal != 'Null'
  65.         AND ((SUBSTR(f.`mobile_number`, 1, 3) IN ('017' , '016', '015', '019', '018', '011')
  66.         AND LENGTH(f.`mobile_number`) = 11)
  67.         OR (SUBSTR(f.`mobile_number`, 1, 2) IN ('17' , '16', '15', '19', '18', '11')
  68.         AND LENGTH(f.`mobile_number`) = 10))
  69.         AND NOT ((SUBSTR(s.`mobile_number`, 1, 3) IN ('017' , '016', '015', '019', '018', '011')
  70.         AND LENGTH(s.`mobile_number`) = 11)
  71.         OR (SUBSTR(s.`mobile_number`, 1, 2) IN ('17' , '16', '15', '19', '18', '11')
  72.         AND LENGTH(s.`mobile_number`) = 10));
  73.  
  74. -- 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
  75. UPDATE
  76.     `stipend_info_10JulDisb_1Jul-1` f
  77.         INNER JOIN
  78.     `stipend_info_10JulDisb_1Jul-1` s ON f.parent_name_unicode = s.parent_name_unicode
  79.         AND f.nationalidoriginal = s.nationalidoriginal
  80.         INNER JOIN
  81.     (SELECT
  82.         COUNT(DISTINCT t.mobile_number) validMobileCount,
  83.             t.mobile_number
  84.     FROM
  85.         `stipend_info_10JulDisb_1Jul-1` t
  86.     WHERE
  87.         ((SUBSTR(t.`mobile_number`, 1, 3) IN ('017' , '016', '015', '019', '018', '011')
  88.             AND LENGTH(t.`mobile_number`) = 11)
  89.             OR (SUBSTR(t.`mobile_number`, 1, 2) IN ('17' , '16', '15', '19', '18', '11')
  90.             AND LENGTH(t.`mobile_number`) = 10))
  91.     GROUP BY t.parent_name_unicode , t.nationalidoriginal) t ON f.mobile_number = t.mobile_number
  92.     set s.mobile_number = f.mobile_number
  93. WHERE
  94.     f.mobile_number != s.mobile_number
  95.         AND f.nationalidoriginal != 'Null'
  96.         AND ((SUBSTR(f.`mobile_number`, 1, 3) IN ('017' , '016', '015', '019', '018', '011')
  97.         AND LENGTH(f.`mobile_number`) = 11)
  98.         OR (SUBSTR(f.`mobile_number`, 1, 2) IN ('17' , '16', '15', '19', '18', '11')
  99.         AND LENGTH(f.`mobile_number`) = 10))
  100.         AND NOT ((SUBSTR(s.`mobile_number`, 1, 3) IN ('017' , '016', '015', '019', '018', '011')
  101.         AND LENGTH(s.`mobile_number`) = 11)
  102.         OR (SUBSTR(s.`mobile_number`, 1, 2) IN ('17' , '16', '15', '19', '18', '11')
  103.         AND LENGTH(s.`mobile_number`) = 10));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement