Guest User

Untitled

a guest
Jun 19th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 4.25 KB | None | 0 0
  1. ***********************************************************************************************************/
  2. SELECT * FROM (     --1,255,458
  3. --has region, not one of the "expand" cases     --151
  4. SELECT  a.person_id PersonId,
  5.         NULL CountryId,
  6.         NULL CountrySubdivisionId,
  7.         d.RegionId,
  8.         1 DesirableIndicator,
  9.         a.ent_oper, a.ent_date, a.chg_oper, a.chg_date, a.rec_sts
  10. FROM questnt.dbo.peo_desired_location a WITH (nolock)
  11. JOIN questnt.dbo.peo_master b WITH (nolock)
  12.     ON a.person_id = b.person_id
  13. JOIN questnt.dbo.cd_region c WITH (nolock)
  14.     ON LTRIM(RTRIM(a.region_code)) = LTRIM(RTRIM(c.region_code))
  15. JOIN q2.dbo.Region d WITH (nolock)
  16.     ON LTRIM(RTRIM(a.region_code)) = d.Code
  17. JOIN q2.dbo.RegionCategory e WITH (nolock)
  18.     ON  d.RegionCategoryId = e.RegionCategoryId
  19.     AND e.Code = 'CR'
  20. WHERE   a.state_code = ''
  21. AND     a.province_code = ''
  22. AND     a.country_code = ''
  23. AND     a.region_code <> ''
  24. AND     c.definition NOT LIKE 'Expand%'
  25.  
  26. UNION
  27. --continent only    --3896
  28. SELECT  a.person_id PersonId,
  29.         NULL CountryId,
  30.         NULL CountrySubdivisionId,
  31.         d.RegionId,
  32.         1 DesirableIndicator,
  33.         a.ent_oper, a.ent_date, a.chg_oper, a.chg_date, a.rec_sts
  34. FROM questnt.dbo.peo_desired_location a WITH (nolock)
  35. JOIN questnt.dbo.peo_master b WITH (nolock)
  36.     ON a.person_id = b.person_id
  37. LEFT JOIN questnt.dbo.cd_region c WITH (nolock)
  38.     ON LTRIM(RTRIM(a.region_code)) = LTRIM(RTRIM(c.region_code))
  39.     AND c.definition LIKE 'Expand%'
  40. JOIN q2.dbo.Region d WITH (nolock)
  41.     ON LTRIM(RTRIM(a.continent_code)) = d.Code 
  42. JOIN q2.dbo.RegionCategory e WITH (nolock)
  43.     ON  d.RegionCategoryId = e.RegionCategoryId
  44.     AND e.Code = 'CO'
  45. WHERE   a.state_code = ''
  46. AND     a.province_code = ''
  47. AND     a.country_code = ''
  48. AND     ((a.region_code <> '' AND c.region_code IS NOT NULL) OR a.region_code = '')
  49. AND     a.continent_code <> ''
  50.  
  51. UNION
  52. --worldwide only    --152
  53. SELECT  a.person_id PersonId,
  54.         c.CountryId,
  55.         NULL CountrySubdivisionId,
  56.         e.RegionId,
  57.         1 DesirableIndicator,
  58.         a.ent_oper, a.ent_date, a.chg_oper, a.chg_date, a.rec_sts
  59. FROM questnt.dbo.peo_desired_location a WITH (nolock)
  60. JOIN questnt.dbo.peo_master b WITH (nolock)
  61.     ON a.person_id = b.person_id
  62. LEFT JOIN q2.dbo.Country c WITH (nolock)
  63.     ON LTRIM(RTRIM(a.country_code)) = c.Code
  64. JOIN q2.dbo.RegionCategory d WITH (nolock)
  65.     ON d.Code = 'WW'
  66. JOIN q2.dbo.Region e WITH (nolock)
  67.     ON LTRIM(RTRIM(a.continent_code)) = e.Code
  68.     AND e.RegionCategoryId = d.RegionCategoryId
  69. WHERE   a.continent_code = 'WW'
  70.  
  71. UNION
  72. --country only  --1,229,169
  73. SELECT  a.person_id PersonId,
  74.         c.CountryId,
  75.         NULL CountrySubdivisionId,
  76.         NULL RegionId,
  77.         1 DesirableIndicator,
  78.         a.ent_oper, a.ent_date, a.chg_oper, a.chg_date, a.rec_sts
  79. FROM questnt.dbo.peo_desired_location a WITH (nolock)
  80. JOIN questnt.dbo.peo_master b WITH (nolock)
  81.     ON a.person_id = b.person_id
  82. LEFT JOIN q2.dbo.Country c WITH (nolock)
  83.     ON LTRIM(RTRIM(a.country_code)) = c.Code
  84. WHERE   LTRIM(RTRIM(a.country_code)) <> ''
  85. AND     LTRIM(RTRIM(a.province_code)) = ''
  86. AND     LTRIM(RTRIM(a.state_code)) = ''
  87.  
  88. UNION
  89. --province only     --335
  90. SELECT  a.person_id PersonId,
  91.         c.CountryId,
  92.         e.CountrySubdivisionId,
  93.         NULL RegionId,
  94.         1 DesirableIndicator,
  95.         a.ent_oper, a.ent_date, a.chg_oper, a.chg_date, a.rec_sts
  96. FROM questnt.dbo.peo_desired_location a WITH (nolock)
  97. JOIN questnt.dbo.peo_master b WITH (nolock)
  98.     ON a.person_id = b.person_id
  99. LEFT JOIN q2.dbo.Country c WITH (nolock)
  100.     ON LTRIM(RTRIM(a.country_code)) = c.Code
  101. LEFT JOIN q2.dbo.CountrySubdivision e WITH (nolock)
  102.     ON 'P_' + LTRIM(RTRIM(a.province_code)) = e.Code
  103. WHERE   LTRIM(RTRIM(a.country_code)) <> ''
  104. AND     LTRIM(RTRIM(a.province_code)) <> ''
  105. AND     LTRIM(RTRIM(a.state_code)) = ''
  106.  
  107. UNION
  108. --state only        --1559
  109. SELECT  a.person_id PersonId,
  110.         c.CountryId,
  111.         e.CountrySubdivisionId,
  112.         NULL RegionId,
  113.         1 DesirableIndicator,
  114.         a.ent_oper, a.ent_date, a.chg_oper, a.chg_date, a.rec_sts
  115. FROM questnt.dbo.peo_desired_location a WITH (nolock)
  116. JOIN questnt.dbo.peo_master b WITH (nolock)
  117.     ON a.person_id = b.person_id
  118. LEFT JOIN q2.dbo.Country c WITH (nolock)
  119.     ON LTRIM(RTRIM(a.country_code)) = c.Code
  120. LEFT JOIN q2.dbo.CountrySubdivision e WITH (nolock)
  121.     ON 'S_' + LTRIM(RTRIM(a.state_code)) = e.Code
  122. WHERE   LTRIM(RTRIM(a.country_code)) <> ''
  123. AND     LTRIM(RTRIM(a.province_code)) = ''
  124. AND     LTRIM(RTRIM(a.state_code)) <> ''
  125. ) un
Add Comment
Please, Sign In to add comment