Guest User

Untitled

a guest
Jun 19th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 2.85 KB | None | 0 0
  1.  
  2. SELECT * FROM (     --1,234,219     --1,235,100
  3. --no country, no region --4,037
  4. SELECT  a.person_id PersonId,
  5.         NULL CountryId,
  6.         NULL CountrySubdivisionId,
  7.         CASE    WHEN LTRIM(RTRIM(a.region_code)) <> '' THEN f.RegionId
  8.                 ELSE d.RegionId END RegionId,
  9.         1 DesirableIndicator,
  10.         a.ent_oper, a.ent_date, a.chg_oper, a.chg_date, a.rec_sts
  11. FROM questnt.dbo.peo_desired_location a WITH (nolock)
  12. JOIN questnt.dbo.peo_master b WITH (nolock)
  13.     ON a.person_id = b.person_id
  14. LEFT JOIN q2.dbo.RegionCategory c WITH (nolock)
  15.     ON c.Code = 'CO'
  16. LEFT JOIN q2.dbo.Region d WITH (nolock)
  17.     ON LTRIM(RTRIM(a.continent_code)) = d.Code
  18. LEFT JOIN q2.dbo.RegionCategory e WITH (nolock)
  19.     ON e.Code = 'CR'
  20. LEFT JOIN q2.dbo.Region f WITH (nolock)
  21.     ON LTRIM(RTRIM(a.region_code)) = f.Code
  22. LEFT JOIN questnt.dbo.cd_region g
  23.     ON a.region_code = g.region_code
  24. WHERE   a.state_code = ''
  25. AND     a.province_code = ''
  26. AND     a.country_code = ''
  27. AND     ((a.region_code <> '' AND g.definition LIKE 'Expand%') OR a.region_code = '')
  28. AND     a.continent_code <> ''
  29.  
  30. UNION
  31. --country only  --1,229,169
  32. SELECT  a.person_id PersonId,
  33.         c.CountryId,
  34.         NULL CountrySubdivisionId,
  35.         NULL RegionId,
  36.         1 DesirableIndicator,
  37.         a.ent_oper, a.ent_date, a.chg_oper, a.chg_date, a.rec_sts
  38. FROM questnt.dbo.peo_desired_location a WITH (nolock)
  39. JOIN questnt.dbo.peo_master b WITH (nolock)
  40.     ON a.person_id = b.person_id
  41. LEFT JOIN q2.dbo.Country c WITH (nolock)
  42.     ON LTRIM(RTRIM(a.country_code)) = c.Code
  43. WHERE   LTRIM(RTRIM(a.country_code)) <> ''
  44. AND     LTRIM(RTRIM(a.province_code)) = ''
  45. AND     LTRIM(RTRIM(a.state_code)) = ''
  46.  
  47. UNION
  48. --province only     --335
  49. SELECT  a.person_id PersonId,
  50.         c.CountryId,
  51.         e.CountrySubdivisionId,
  52.         NULL RegionId,
  53.         1 DesirableIndicator,
  54.         a.ent_oper, a.ent_date, a.chg_oper, a.chg_date, a.rec_sts
  55. FROM questnt.dbo.peo_desired_location a WITH (nolock)
  56. JOIN questnt.dbo.peo_master b WITH (nolock)
  57.     ON a.person_id = b.person_id
  58. LEFT JOIN q2.dbo.Country c WITH (nolock)
  59.     ON LTRIM(RTRIM(a.country_code)) = c.Code
  60. LEFT JOIN q2.dbo.CountrySubdivision e WITH (nolock)
  61.     ON 'P_' + LTRIM(RTRIM(a.province_code)) = e.Code
  62. WHERE   LTRIM(RTRIM(a.country_code)) <> ''
  63. AND     LTRIM(RTRIM(a.province_code)) <> ''
  64. AND     LTRIM(RTRIM(a.state_code)) = ''
  65.  
  66. UNION
  67. --state only        --1559
  68. SELECT  a.person_id PersonId,
  69.         c.CountryId,
  70.         e.CountrySubdivisionId,
  71.         NULL RegionId,
  72.         1 DesirableIndicator,
  73.         a.ent_oper, a.ent_date, a.chg_oper, a.chg_date, a.rec_sts
  74. FROM questnt.dbo.peo_desired_location a WITH (nolock)
  75. JOIN questnt.dbo.peo_master b WITH (nolock)
  76.     ON a.person_id = b.person_id
  77. LEFT JOIN q2.dbo.Country c WITH (nolock)
  78.     ON LTRIM(RTRIM(a.country_code)) = c.Code
  79. LEFT JOIN q2.dbo.CountrySubdivision e WITH (nolock)
  80.     ON 'S_' + LTRIM(RTRIM(a.state_code)) = e.Code
  81. WHERE   LTRIM(RTRIM(a.country_code)) <> ''
  82. AND     LTRIM(RTRIM(a.province_code)) = ''
  83. AND     LTRIM(RTRIM(a.state_code)) <> ''
  84. ) un
Add Comment
Please, Sign In to add comment