I have an Android app. Inside my Android app I have an SQLite database. Inside my SQLite database I have a table that looks like this:
_id a b c d 1 1 1 1 0 2 0 1 1 1 3 1 0 0 1 4 0 1 0 1
I want to calculate for each row: of all the columns a, b, c and d that have a 1 in EITHER the current or previous row, what percentage have a 1 in BOTH the current and previous row? The output would look like this:
_id a b c d result 1 1 1 1 0 NULL 2 0 1 1 1 50% 3 1 0 0 1 25% 4 0 1 0 1 33%
I can do this in Java outside of SQLite, but I’d rather do it in SQL; it would be neater that way. What query should I use?
A significant factor in making this query difficult is that the data is denormalized. Thus, I’m having to normalize it in order to get the information you seek.
Knowing what columns
a,b,canddrepresent makes all the difference in the world. The complexity of the above query is indicative of a schema that does not map well to the business needs. Knowing that they represent student attendance, we can devise an alternate schema.The way your results read you actually was requesting the number of people that attended on one day and not the previous or on the previous day and not the current.
Results: