简体   繁体   中英

How to calculate the difference between values in two rows of one table using MS Access 2003 SQL? with EXTRA CRITERIA

Name     ExactDate  Presents
bob      2011        1
bob      2008        2
bob      2012        3
mary     1986        4
mary     2001        5
mary     2012        6
kate     2011        7
kate     2012        8
kate     2013        9
celia    2011       10
celia    1986       11
celia    1972       12
celia    2012       13
celia    1991       14

So the goal is we subtract the amount of presents kate got from celia on the same day and out put should be something like the following:

Name              ExactDate        Presents
celiaminuskate    2011              3
celiaminuskate    2012              5

Thank you so much I am a first time user of access and SQL. and have a data management task required of me at work. so this has really go me stuck This is done in ms access 2003 SQL



HEY SO I GOT THE FIRST PART THERE AND NOW IT GETS HARDER ASSUMING THE DATA SET IS NOW LIKE THIS

Name     ExactDate  Presents  Location
bob      2011        1          home
bob      2008        2          school     
bob      2012        3          school
mary     1986        4          school       
mary     2001        5          home
mary     2012        6          homw    
kate     2011        7          home
kate     2012        8          home
kate     2011        9          school 
celia    2011       10          school 
celia    1986       11          school  
celia    1972       12          home
celia    2012       14          home 
celia    2012       13          school

So the goal is we subtract the amount of presents kate got from celia on the same year ( but since there are a few different present values for the same year we choose to have priority of home > school....for example celia and kate both recieves presents in 2012 but celia gets both home presents and school presents in 2012 in which case we choose her home present value to do the calculation) and out put should be something like the following:

Name              ExactDate        Presents
celiaminuskate    2011              3
celiaminuskate    2012              6

This should do it:

SELECT
    'celiaminuskate' AS [NAME],
    T1.[date] AS [EXACT DATE],
    T1.presents - T2.presents AS [PRESENTS DIFF]
FROM
    Some_Table T1
INNER JOIN Some_Table T2 ON
    T2.[name] = 'kate' AND
    T2.[date] = T1.[date]
WHERE
    T1.[name] = 'celia'
ORDER BY
    T1.[date]

A couple of suggestions since you're new to SQL:

  1. Try to avoid using keywords, like "date" for column names
  2. Your "date" column looks like a year, not a date. It should be named appropriately and you should make sure that it's the correct data type.

Since it sounds like the version of Access that you're using doesn't support the now standard JOIN syntax, here is another query which should be equivalent:

SELECT
    'celiaminuskate' AS [NAME],
    T1.[date] AS [EXACT DATE],
    T1.presents - T2.presents AS [PRESENTS DIFF]
FROM
    Some_Table T1, Some_Table T2
WHERE
    T1.[name] = 'celia' AND
    T2.[name] = 'kate' AND
    T2.[date] = T1.[date]
ORDER BY
    T1.[date]

T1 and T2 in this query are simply aliases for the tables in the FROM clause so that you can distinguish them.

If you're trying to put the results into another table then you will need this to be part of an INSERT statement. Or, with Access you might be able to use a query to generate a table as part of some wizard. I'm afraid that I don't have a copy handy to give more specifics. In any case, here is what the INSERT statement would look like:

INSERT INTO Some_New_Table (name, exactdate, presentsdiff)
SELECT ...

(The ellipsis just means to use the query as I have it up above)

You will need to do a self-join , because you are comparing rows from the same table. Below we join table1 to itself using simular dates, but different names.
A more realistic answer would use unique row_ids instead of names. Next we tell the database that we want only celia 's rows in part1 and only kate 's rows in part2.

SELECT 'celiaminuskate' AS useless_filler
       , a.[date] AS whendidthishappen
       , (celia.presents - kate.presents) AS outcome
FROM table1 AS kate
INNER JOIN table1 AS celia ON (a.[date] = b.[date] and a.name <> b.name)
WHERE celia.name = 'celia' and kate.name = 'kate'

Note that date is a reserved word and you will need to enclose it in square brackets []

The key to answering this is a self-join:

SELECT 'Celia - Kate' AS tag, C.ExactDate, C.Presents - K.Presents
  FROM (SELECT ExactDate, Presents FROM AnonymousTable WHERE Name = 'celia') AS C
  JOIN (SELECT ExactDate, Presents FROM AnonymousTable WHERE Name = 'kate')  AS K
    ON C.ExactDate = K.ExactDate
 ORDER BY C.Date;
SELECT 'celiaminuskate', p1.exactdate, p1.presents - p2.presents
FROM presents p1 JOIN presents p2 ON p1.exactdate = p2.exactdate
WHERE p1.name = 'celia' AND p2.name = 'kate'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM