Results 1 to 14 of 14
  1. #1
    rowlandsfc is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2019
    Posts
    7

    Linking data from rows in another table

    hi so i am trying to link some data from 1 table to another



    table 1
    Click image for larger version. 

Name:	table 1.png 
Views:	12 
Size:	23.5 KB 
ID:	37985
    table 2
    Click image for larger version. 

Name:	table 2.png 
Views:	12 
Size:	24.0 KB 
ID:	37986
    those are the 2 tables in question, table 1 shows all the wards in south wales and each ward is associated with a UA code, there is around 250 wards and 7 UA codes

    the second table shows the CRIMEID and the WARD associated with that crimeid and there is over 150,000 records there

    what im looking to do is creat a query that will show me the crimeid the ward associated with that crimeid but also insert a new column that will show the UA that would be associated with each record

    any help is appreciated

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Start a new query and add both tables to the design grid. If there isn't already a join line between the two tables on Ward, click/drag to create one. Then add both fields from the lower table and the UA field from the upper table and run the query.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    rowlandsfc is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2019
    Posts
    7
    Awesome thanks

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Happy to help and welcome to the site!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    rowlandsfc is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2019
    Posts
    7
    so it seemed to work great but i have since starting to count up how many crimes each UA code has and i seem to have an extra 2000-3000 compared to the amount of entries there actually are for the CRIMEID

    Code:
    SELECT Ward_UA.UA, COUNT(IIF(UA = '00NX',1,NULL)) AS 'Crimes'
    FROM Ward_UA INNER JOIN CrimeWardLUT ON Ward_UA.WARD = CrimeWardLUT.WARD
    WHERE UA = '00NX'
    GROUP BY Ward_UA.UA
    
    UNION ALL
    
    SELECT Ward_UA.UA, COUNT(IIF(UA = '00NZ',1,NULL)) AS 'Crimes'
    FROM Ward_UA INNER JOIN CrimeWardLUT ON Ward_UA.WARD = CrimeWardLUT.WARD
    WHERE UA = '00NZ'
    GROUP BY Ward_UA.UA
    
    UNION ALL
    
    SELECT Ward_UA.UA, COUNT(IIF(UA = '00PB',1,NULL)) AS 'Crimes'
    FROM Ward_UA INNER JOIN CrimeWardLUT ON Ward_UA.WARD = CrimeWardLUT.WARD
    WHERE UA = '00PB'
    GROUP BY Ward_UA.UA
    Thats the code im using to count

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Could there be wards in the first table more than once?

    What does this produce?

    SELECT Ward_UA.UA, COUNT(*) AS Crimes
    FROM Ward_UA INNER JOIN CrimeWardLUT ON Ward_UA.WARD = CrimeWardLUT.WARD
    GROUP BY Ward_UA.UA
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    rowlandsfc is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2019
    Posts
    7
    Will have a look tomorrow evening when I give it a go
    Thanks

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    No problem. You shouldn't need the UNION query to get 3 counts. That will give you all of the wards, but you could add a criteria like:

    IN('00NX', '00NZ', '00PB')

    to my query to limit it to just those 3.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    rowlandsfc is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2019
    Posts
    7
    Quote Originally Posted by pbaldy View Post
    No problem. You shouldn't need the UNION query to get 3 counts. That will give you all of the wards, but you could add a criteria like:

    IN('00NX', '00NZ', '00PB')

    to my query to limit it to just those 3.

    UA Crimes
    00NX 45992
    00NZ 14652
    00PB 15088
    00PD 10816
    00PF 26571
    00PH 8670
    00PT 54354

    thats what it returns but the amount is too much for the total of entries that are actually there

    CRIMEID WARD
    157973 00NZMH
    157974 00PTPK
    157975 00PHMJ
    157976 00PFQA
    157977 00NXPS
    157978 00PHMC
    157979 00PTPB
    157980 00PTPC

    thats the last load of entries that there is but the query when i add those totals adds up to 176143

  10. #10
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Can you attach the db here to play with?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  11. #11
    rowlandsfc is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2019
    Posts
    7
    it wont upload for some reason, nothing happens when i try adding the file

  12. #12
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Try doing a compact/repair and then zipping. That will normally work.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  13. #13
    rowlandsfc is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2019
    Posts
    7
    still not working sadly

  14. #14
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    How big is it zipped? You can email it to me if it's too big to attach here (2MB).

    pbaldy
    gmail
    com
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 29
    Last Post: 05-02-2017, 04:38 PM
  2. Linking Table Data
    By Lewis825 in forum Access
    Replies: 5
    Last Post: 04-20-2016, 07:10 AM
  3. Replies: 22
    Last Post: 12-20-2015, 02:46 AM
  4. Replies: 5
    Last Post: 08-25-2015, 08:39 AM
  5. Replies: 5
    Last Post: 12-19-2012, 07:26 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums