On 1/28/2014 2:26 PM, David Bicking wrote:
I have two tables:

ARB
  KEY (PRIMARY KEY)
  ASSIGN (NOT NECESSARILY UNIQUE)

DMC
  KEY (NOT UNIQUE)
  ASSIGN (NOT UNIQUE)
  VALUE

I need to report all the records from ARB, and sum up the values if the keys 
match OR if the keys don't match, then sum up the values where the ASSIGN 
matches, but only if the ASSIGN is unique in ARB.

SELECT ARB.KEY
, ARB.ASSIGN
, COALESCE((SELECT SUM(DMC.VALUE) FROM DMC WHERE DMC.KEY = ARB.KEY)
                       , (SELECT SUM(DMC.VALUE) FROM DMC WHERE DMC.ASSIGN = 
ARB.ASSIGN AND ...), 0)
FROM ARB;

I can't think of what to put after that AND....

Something like this perhaps:

and 1 = (select count(*) from ARB t2 where t2.ASSIGN=ARB.ASSIGN)

--
Igor Tandetnik

_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to