Advertisement
Guest User

Untitled

a guest
Apr 26th, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. -- PostgreSQL
  2. -- combine data for all years into one table
  3. -- to find distinct addresses & BBL numbers
  4. create table dhcr_all (
  5. zip integer,
  6. bldgno1 text,
  7. street_name1 text,
  8. street_suffix1 text,
  9. bldgno2 text,
  10. street_name2 text,
  11. street_suffix2 text,
  12. bldgno3 text,
  13. street_name3 text,
  14. street_suffix3 text,
  15. status1 text,
  16. status2 text,
  17. status3 text,
  18. block text,
  19. lot integer,
  20. boro_code integer
  21. );
  22.  
  23. insert into dhcr_all (
  24. zip,
  25. bldgno1,
  26. street_name1,
  27. street_suffix1,
  28. bldgno2,
  29. street_name2,
  30. street_suffix2,
  31. bldgno3,
  32. street_name3,
  33. street_suffix3,
  34. status1,
  35. status2,
  36. status3,
  37. block,
  38. lot,
  39. boro_code
  40. )
  41. SELECT * FROM dhcr2009tmp;
  42.  
  43. insert into dhcr_all (
  44. zip,
  45. bldgno1,
  46. street_name1,
  47. street_suffix1,
  48. bldgno2,
  49. street_name2,
  50. street_suffix2,
  51. bldgno3,
  52. street_name3,
  53. street_suffix3,
  54. status1,
  55. status2,
  56. status3,
  57. block,
  58. lot,
  59. boro_code
  60. )
  61. SELECT * FROM dhcr2011;
  62.  
  63. insert into dhcr_all (
  64. zip,
  65. bldgno1,
  66. street_name1,
  67. street_suffix1,
  68. bldgno2,
  69. street_name2,
  70. street_suffix2,
  71. bldgno3,
  72. street_name3,
  73. street_suffix3,
  74. status1,
  75. status2,
  76. status3,
  77. block,
  78. lot,
  79. boro_code
  80. )
  81. SELECT * FROM dhcr2012;
  82.  
  83. -- 2013 doesn't have a bldg03, street3, stsufx3 columns
  84. insert into dhcr_all (
  85. zip,
  86. bldgno1,
  87. street_name1,
  88. street_suffix1,
  89. bldgno2,
  90. street_name2,
  91. street_suffix2,
  92. status1,
  93. status2,
  94. status3,
  95. block,
  96. lot,
  97. boro_code
  98. )
  99. SELECT
  100. "ZIP",
  101. "BLDGNO1",
  102. "STREET1",
  103. "STSUFX1",
  104. "BLDGNO2",
  105. "STREET2",
  106. "STSUFX2",
  107. "STATUS1",
  108. "STATUS2",
  109. "STATUS3",
  110. "BLOCK",
  111. "LOT",
  112. "BORO_CODE"
  113. FROM dhcr2013;
  114.  
  115. -- should return about 170,000 rows
  116. select count(*) from dhcr_all;
  117.  
  118. -- split bldgno1 column into two separate numbers for geocoding with NYC Geoclient API
  119. alter table dhcr_all add column bldgno1a text;
  120. alter table dhcr_all add column bldgno1b text;
  121.  
  122. update dhcr_all set bldgno1a = split_part(bldgno1, 'TO', 1);
  123. update dhcr_all set bldgno1b = split_part(bldgno1, 'TO', 2;
  124.  
  125. -- find all distinct addresses
  126. select bldgno1, bldgno1a, bldgno1b, street_name1, street_suffix1, boro_code, zip
  127. from dhcr_all
  128. group by bldgno1, bldgno1a, bldgno1b, street_name1, street_suffix1, boro_code, zip;
  129.  
  130. -- returns a measley 4,755 rows, DHCR's data is obviously not complete!
  131. select count(*) from (
  132. select bldgno1, bldgno1a, bldgno1b, street_name1, street_suffix1, boro_code, zip
  133. from dhcr_all
  134. group by bldgno1, bldgno1a, bldgno1b, street_name1, street_suffix1, boro_code, zip
  135. ) as distinct_addresses;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement