sql - Doing a calculation between two rows with EXTRA CRITERIA? -


name      exactdate  presents      location bob1         2011        1          home bob2         2008        2          school      bob2         2012        3          school mary2        1986        4          school        mary1        2001        5          home mary1        2012        6          home     kate1        2011        7          home kate1        2012        8          home kate2        2011        9          school  celia2       2011       10          school  celia2       1986       11          school   celia1       1972       12          home celia1       2012       14          home  celia2       2012       13          school 

this problem done in sql in ms access 2003 query.

so goal subtract amount of presents kate got celia on same year ( since there few different present values same year choose have priority of home > school....for example celia , kate both receive presents in 2012 celia gets both home presents , school presents in 2012 in case choose home present value calculation) , out put should following:

name              exactdate        presentsdiff celiaminuskate     2011                      3 celiaminuskate     2012                      6 

so far have :

select 'celiaminuskate'as [name],t1.[date] [exact date], t1.presents t2.presents [presents diff] some_table t1, some_table t2  

part think needs fixed??

where (t1.name = 'celia1'>'celia2') , (t2.name = 'kate1'>'kate2') , t2.exactdate = t1.exactdate  

to indicate priority? i'm not sure how

order t1.exactdate 

i created query , saved qryceliaandkategiftdates. returns distinct exactdate values celia , kate both had gifts recorded.

notice re-named name field recipient, because name reserved word.

select distinct celia.exactdate     [select exactdate     some_table     recipient "celia*"     ]. celia     inner join [         select exactdate         some_table         recipient "kate*"         ]. kate     on celia.exactdate = kate.exactdate order celia.exactdate; 

then used correlated subqueries return correct presents values celia , kate on each of exactdates.

select     raw.recipients [name],     raw.exactdate  [exact date],     (raw.celia_presents - raw.kate_presents) [presents diff]     [select     'celiaminuskate' recipients,     dates.exactdate,     (select top 1 presents     some_table             recipient "celia*"         , exactdate = dates.exactdate     order location) celia_presents,     (select top 1 presents     some_table             recipient "kate*"         , exactdate = dates.exactdate     order location) kate_presents     qryceliaandkategiftdates dates]. raw; 

it returns results requested when run access 2003. correlated subqueries notoriously slow, i'll interested see other answers get.


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -