Advertisement
Danack

Sorting data and pairing it thingy.

Feb 24th, 2014
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1.  
  2. Given the following table:
  3.  
  4. Code:
  5. --------------
  6. | id | event |
  7. --------------
  8. | 00 | start |
  9. | 01 | data |
  10. | 02 | end |
  11. | 03 | data |
  12. | 04 | data |
  13. | 05 | start |
  14. | 06 | data |
  15. | 07 | data |
  16. | 08 | end |
  17. | 09 | data |
  18. | 10 | start |
  19. | 11 | data |
  20. | 12 | end |
  21. --------------
  22. How would I write a SQL query to return all of the rows between "start" and "end" rows?
  23.  
  24. ////////////////////////////////////////////////////////////////////////
  25. //////////////////
  26.  
  27.  
  28. CREATE temporary TABLE lookup
  29. (id INT,
  30. startID INT,
  31. endID INT)
  32. ENGINE = MEMORY;
  33.  
  34.  
  35. CREATE temporary TABLE lookupSorted
  36. ( indexID int(10) unsigned NOT NULL auto_increment,
  37. id INT,
  38. startID INT,
  39. endID INT,
  40. PRIMARY KEY (`indexID`)
  41. )
  42. ENGINE = MEMORY;
  43.  
  44. CREATE temporary TABLE lookupSorted2
  45. ( indexID int(10) unsigned NOT NULL auto_increment,
  46. id INT,
  47. startID INT,
  48. endID INT,
  49. PRIMARY KEY (`indexID`)
  50. )
  51. ENGINE = MEMORY;
  52.  
  53.  
  54. insert into lookup select id, id as startID, -99 as endID from test.tce where event = 'start';
  55.  
  56.  
  57. insert into lookup select id, -99 as startID, id as endID from test.tce where event = 'end';
  58.  
  59. insert into lookupSorted (id, startID, endID) select id, startID, endID from lookup order by id;
  60.  
  61. insert into lookupSorted2 (id, startID, endID) select id, startID, endID from lookup order by id;
  62.  
  63. #select * from lookupSorted;
  64.  
  65.  
  66. CREATE temporary TABLE goodData
  67. ( indexID int(10) unsigned NOT NULL auto_increment,
  68. startID INT,
  69. endID INT,
  70. PRIMARY KEY (`indexID`)
  71. )
  72. ENGINE = MEMORY;
  73.  
  74. CREATE temporary TABLE goodData2
  75. ( indexID int(10) unsigned NOT NULL auto_increment,
  76. startID INT,
  77. endID INT,
  78. PRIMARY KEY (`indexID`)
  79. )
  80. ENGINE = MEMORY;
  81.  
  82.  
  83.  
  84.  
  85. insert into goodData (startID, endID) select (a.startID + 1) as startID, (b.endID - 1) as endID from lookupSorted a
  86. inner join lookupSorted2 b
  87. on a.indexID + 1 = b.indexID
  88. where a.startID != -99
  89. and b.endID != -99;
  90.  
  91. insert into goodData2 (startID, endID) select (a.startID + 1) as startID, (b.endID - 1) as endID from lookupSorted a
  92. inner join lookupSorted2 b
  93. on a.indexID + 1 = b.indexID
  94. where a.startID != -99
  95. and b.endID != -99;
  96.  
  97.  
  98. #select * from goodData;
  99.  
  100. select a.id, a.event from test.tce a
  101. inner join goodData b on
  102. a.id >= b.startID
  103. inner join goodData2 c
  104. on a.id <= c.endID
  105. and b.indexID = c.indexID;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement