android - SQLite elements in CREATE VIEW duplicating -


this view puts columns 3 tables

        db.execsql("create view " + viewcomps +             " select " + company + "." + colcompid + " _id," +             " " + accounts + "." + colname + "," +             " " + company + "." + colcompclass + "," +             " " + payments + "." + colgroupid + "," +             " " + payments + "." + colpaydue + "," +             " " + payments + "." + coldatedue + "" +             " " + payments + ", " + company +             " join " + accounts + " on " + payments + "." + colgroupid + " = " + accounts + "." + colid ); 

problem

  1. i have 3 companies a, b , c
  2. acc 1 assigned a, 1 acc = 1 company only
  3. acc 1 inserted , assigned a, problem if assign it duplicated b , c well.

the result:

  acc 1 | company | payment | date   acc 1 | company b | payment | date   acc 1 | company c | payment | date 

what should :

  acc 1 | company | payment | date 

every other account insert database same , results in companies containing duplicate of accounts irregardless of company account assigned to.

question

what doing wrong view? don't understand why duplicates entries putting copy of each account in each company. can show me mistake is? i'm new , use few pointers in area. i'm confident it's problem view have activity displays accounts based on company , works fine there @ least.

i'll leave 3 tables view references down below in case it's needed:

    db.execsql("create table " + company + " (" + colcompid + " integer primary key autoincrement, " +             colcompclass + " text)");      db.execsql("create table " + accounts + " (" + colid + " integer primary key autoincrement, " +             colname + " text, " +             colcomp + " integer not null," +             colamount + " integer, " +             colpurpose + " text, " +             colterms + " integer not null, " +             colperiod + " integer not null, " +             colbalance + " integer, "+             colstatus + " integer not null," +             coldate + " text, " +             coleditdate + " text, " +             colremarks + " text, " +             "foreign key (" + colcomp + ") references " + company + " (" + colcompid + " )" + "on delete cascade," +             "foreign key (" + colterms + ") references " + terms + " (" + coltermsid + " )" + "on delete cascade," +             "foreign key (" + colperiod + ") references " + periods + " (" + colperiodid + ") " + "on delete cascade,"+             "foreign key (" + colstatus + ") references " + status + " (" + colstatusid + ") on delete cascade);");      db.execsql("create table " + payments + " (" + colpayid + " integer primary key autoincrement, " +             colgroupid + " integer not null, " +             colpaybal + " text, " +             colinterest + " text, " +             colpaydue + " text, " +             coldatedue + " text, " +             colpaydate + " text, " +             "foreign key (" + colgroupid + ") references " + accounts + " (" + colid + ") on delete cascade);"); 

when perform join no constraint, produces cartesian product between 2 data sets -- each row left side combined each row of right side, 2 tables of size m , n respectively when joined produce result of size (m x n).

your view query performs 2 joins. 1 join (accounts) has constraint limits size of result set, other (company) not. means when join payments , company, instance of every combination of payment , company. supposing there's 3 of each, yields this:

company | payment 1 company | payment 2 company | payment 3 company b | payment 1 company b | payment 2 company b | payment 3 company c | payment 1 company c | payment 2 company c | payment 3 

your observation duplicated accounts is, in opinion, observation have duplicated payments. accounts joined based on payments, it's first join producing incorrect results.

what need join constraint both joins. looking @ schema, seems payments , accounts have relationship, think correct query this:

create view viewcomps select accounts.colname, company.colcompid _id, company.colcompclass, payments.colgroupid, payments.colpaydue, payments.coldatedue accounts join company on (company.colcompid = accounts.colcomp) join payments on (payments.colgroupid = accounts.colid); 

Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -