Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5993969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:45:03+00:00 2026-05-22T23:45:03+00:00

I have an Android app. Inside my Android app I have an SQLite database.

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T23:45:04+00:00Added an answer on May 22, 2026 at 11:45 pm
    Select  CurAndNext.T1_id
        , Sum( Case When D1.Val + D2.Val = 1 Then 1 End ) As CntInEitherRow
        , Sum( Case When D1.Val + D2.Val = 2 Then 1 End ) / 4.000 As PercBoth
    From    (
            Select T1._id As T1_id, Max( T2._id ) As T2_id
            From MyTable As T1
                Left Join MyTable As T2
                    On T2._id < T1._id
            Group By T1._id
            ) As CurAndNext
        Join    (
                Select _id, 'a' As Col, a As Val From MyTable As T1
                Union All
                Select _id, 'b', b From MyTable As T1
                Union All
                Select _id, 'c', c From MyTable As T1
                Union All
                Select _id, 'd', d From MyTable As T1
                ) As D1
            On D1._id = CurAndNext.T1_id
        Left Join   (
                    Select _id, 'a' As Col, a As Val From MyTable As T1
                    Union All
                    Select _id, 'b', b From MyTable As T1
                    Union All
                    Select _id, 'c', c From MyTable As T1
                    Union All
                    Select _id, 'd', d From MyTable As T1
                    ) As D2
                On D2._id = CurAndNext.T2_id
                    And D2.Col = D1.Col
    Group By CurAndNext.T1_Id
    

    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, c and d represent 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.

    Create Table Student
        (
        Id int not null Primary Key
        , Name varchar(50) not null
        )
    
    Create Table Class
        (
        Id int not null Primary Key
        , Name varchar(50) not null
        )
    
    -- if using dates, this would be the equivalent
    -- of a calendar table
    Create Table ClassDay 
        ( 
        DayNum int not null Primary Key 
        )
    
    -- ClassDayNum would be better as a Date    
    Create Table Attendence
        (
        StudentId int References Student( Id )
        , ClassId int References Class( Id )
        , ClassDayNum int not null  References ClassDay( DayNum )
        , Unique( StudentId, ClassId, ClassDayNum )
        )
    
    Insert Student( Id, Name )
    Select 1, 'a'
    Union All Select 2, 'b'
    Union All Select 3, 'c'
    Union All Select 4, 'd'
    
    Insert Class( Id, Name )
    Values (1, 'Some Class' )
    
    Insert ClassDay( DayNum )
    Select 1
    Union All Select 2
    Union All Select 3
    Union All Select 4
    
    Insert Attendence( ClassId, StudentId, ClassDay )
    Select 1, 1, 1
    Union All Select 1, 1, 3
    Union All Select 1, 2, 1
    Union All Select 1, 2, 2
    Union All Select 1, 2, 4
    Union All Select 1, 3, 1
    Union All Select 1, 3, 2
    Union All Select 1, 4, 2
    Union All Select 1, 4, 3
    Union All Select 1, 4, 4
    

    of all the columns a, b, c and d that have a 1 in EITHER the current or previous row

    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.

    Select Class.Id, ClassDay.DayNum
        , Count(Distinct A.StudentId) As Attendence
        , Count(Distinct A.StudentId) / 4.000 As Ratio
    From Class
        Cross Join Student
        Cross Join ClassDay
        Left Join Attendence As A
            On A.ClassId = Class.Id
                And A.StudentId = Student.Id
                And A.ClassDayNum = ClassDay.DayNum
                And A.ClassDayNum > 1
        Left Join Attendence As A2
            On A2.ClassId = Class.Id
                And A2.StudentId = Student.Id
                And A2.ClassDayNum = ClassDay.DayNum - 1
    Where Not( A.StudentId Is Not Null And A2.StudentId Is Not Null )
    Group By Class.Id, ClassDay.DayNum
    

    Results:

    DayNum  Attendence | Ratio
    1      |    0      |   0
    2      |    1      |   .25
    3      |    1      |   .25
    4      |    1      |   .25
    

    what percentage have a 1 in BOTH the current and previous row

    Select ClassDay.DayNum
        , Sum( Case When A.StudentId Is Not Null And A2.StudentId Is Not Null Then 1 End )
        , Sum( Case When A.StudentId Is Not Null And A2.StudentId Is Not Null Then 1 End ) / 4.000
    From Class
        Cross Join Student
        Cross Join ClassDay
        Left Join Attendence As A
            On A.ClassId = Class.Id
                And A.StudentId = Student.Id
                And A.ClassDayNum = ClassDay.DayNum
                And A.ClassDayNum > 1
        Left Join Attendence As A2
            On A2.ClassId = Class.Id
                And A2.StudentId = Student.Id
                And A2.ClassDayNum = ClassDay.DayNum - 1
    Group By ClassDay.DayNum            
    
    DayNum | Attendence | Ratio
    1      |    NULL    |  NULL
    2      |    2       |  0.500000
    3      |    1       |  0.250000
    4      |    1       |  0.250000
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an SQLite query in my android app that seems to crash when
We have an Android app and a Web Service. We want to download part
I have a class that extends android.app.Dialog, the layout is done in an xml
I have an Android project that branched into three different applications, app-1 , app-2
i have xxx.db (SQLite file) where i want to add him to my android
In my Android app, I have some data that needs to be synced daily
I am developing an Android App and would like to have a video file
I am trying to create a simple 3-D app for android that will have
I have an android application that I want to always be running in landscape
Suppose that, I have an android app that launches browser with some url supplied

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.