Results 1 to 6 of 6
  1. #1
    Mattbro is offline Competent Performer
    Windows XP Access 2010 64bit
    Join Date
    Mar 2012
    Posts
    100

    Query to show counts across tables for different years

    Hi all,
    I have a table-TableCars. It has one column-[Cartype], in which the PK is all the car manufacturers I am repairing-say Fiat, Ford, Audi, Saab and BMW.
    I have another table-Table2020 which has two columns-the PK is the job ref number-[Jobno] and the second column [Car] tells the car manufacturer from the above list that I have repaired in the year 2020.
    I have two more tables-Table2021, and Table2022, both following the same pattern as Table2020.
    Can anyone advise how I could write a query to show how many cars each year, so I can see increase/decrease etc?
    2020 2021 2022
    Fiat 15 99 456
    Ford 16 45 44
    Audi 3 0 0
    Saab 6 7 3
    BMW 21 26 29

    I am struggling with the code below:
    Code:
    SELECT TableCars.CarType, Count([Table2020].Jobno) AS CountOfJobno, Count([Table2021].Jobno) AS CountOfJobno1, Count([Table2022].Jobno) AS CountOfJobno2
    FROM ((TableCars LEFT JOIN Table2020 ON TableCars.CarType = [Table2020].Car) LEFT JOIN Table2021 ON TableCars.CarType = [Table2021].Car) LEFT JOIN Table2022 ON TableCars.CarType = [Table2022].Car
    GROUP BY TbSampleType.CarType;
    Any input appreciated.


    Thanks,
    Mattbro451

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    That is because your data is not normalised.
    You should have one table and a date for when it was repaired. From that you can get the year and group.

    You will need to create a Union query to get that into that format with and additional field with the year.

    You are always going to have issues with a structure like you have. Best to fix it and move on.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    I agree, instead of 3 tables which means adding a new table for each year - where are 2023 and 2024? - should be one table.

    Then a CROSSTAB query could produce desired output.

    Otherwise, build 3 aggregate queries then join those queries to TableCars.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    Mattbro is offline Competent Performer
    Windows XP Access 2010 64bit
    Join Date
    Mar 2012
    Posts
    100
    Thanks for the input all. consolidated all into a single table-TbData, then used the following SQL;
    Code:
    SELECT [TbData].YearX, Count(IIf([Car]="Ford","1")) AS ExpFord, Count(IIf([Car]="BMW","1")) AS ExpBMW, Count(IIf([Car]="Fiat","1")) AS ExpFiat, Count(IIf([Car]="Audi","1")) AS ExpAudi, Count(IIf([Car]="SAAB","1")) AS ExpSAAB
    FROM TableCars INNER JOIN TbData ON TableCars.CarType=[TbData].Car
    GROUP BY [TbData].YearX;
    Thanks all for the suggestions.
    Mattbro451

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Should not need anything as complicated.
    Just group by Year and Car with a count on Car.

    What happens when you start with another manufacturer? You would be in the same boat as you were at the start.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  6. #6
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    consolidated all into a single table-TbData,
    Agreed. You have simply recreated the original issue but in a different fashion. Suggest you research db normalization.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

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

Similar Threads

  1. Replies: 5
    Last Post: 10-18-2023, 05:14 PM
  2. Replies: 9
    Last Post: 06-26-2021, 03:56 AM
  3. Names Across Multiple Years
    By Corinne S in forum Queries
    Replies: 2
    Last Post: 08-02-2019, 11:34 AM
  4. How to show BLANK (null) counts as ZERO "0"
    By KingOf206 in forum Queries
    Replies: 17
    Last Post: 07-25-2017, 03:52 PM
  5. Compare data across years
    By lvh519 in forum Queries
    Replies: 4
    Last Post: 02-16-2012, 01:46 PM

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