Wednesday 15 May 2013

sqlite3 - SQLite Query to group continuous range of numbers into different grouping sets -


I want to get the islands of this table below:

  Group members no 100 A 101 a 200 A101A202A203X100X101A204X301X302A500A600   

I want to use this result on the SQL (islands):

  GroupMeelNewsNumberA 100 101A 200 204X100 101X301 302A 500 500A600 600   

I have seen many code / forums for this, but not Working with SQLite because SQLit There is no CTE in e.

100-101 is constant so that the group is in one.

Does anyone have to do this in SQLite?

The fastest way to do this is to move the serial record of this table into a loop and collect Do the islands manually

Pure SQL (as a set-oriented language), it's not that easy.

First of all, we find out which records are the first in any island. The first record does not have a previous record, i.e., a record with the same group but not a member a small:

  SELECT "Group", MemberNo AS FromMemberN T1 where this table does not exist (Select from this table 1 T2 WHERE T 2. "Group" = T. "Group" and T2 .mail No = T 1. No member - 1)   

To find the last record of an island, we need to find the record with the largest not a member which is still the same island It is related, that is, the same group, and where there are all not members in the island, we can not consistently compute the difference between their values ​​in the first and last records; Code> find out. The last no member of the island can be calculated in this way with the group g and first MemberNo M

  select MAX (not member) is not the last meeting as T3 WHERE T3 as the TAL "Group" = G and T3 .mailo - M + 1 = (SELECT COUNT (*) TLL to T4 WHE T4. "Group" = G & T 4. Not with MMN and T3.MMNL) < / Code>  

Finally, plug it into the first query:

  SELECT "Group", MemberNo AS FromMemberNo, ( In this number select Max (member no) AS T3 WHERE T3. "Group" = T. "Group" and T3 .Mailo - T1.MemberNo + 1 = (SELECT COUNT (*) T4 WHERE t4 from this table Last Month as "Group" = T1 "Group" and t4.MemberNo t1.MemberNo and t3.MemberNo) This Table AS t1 WHERE NOT "Even From this table, select 1 to TT2 WHERE T2.

No comments:

Post a Comment