Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2009
    Posts
    4

    Help with counting Query

    Hello --- I have a database that has several fields including three date fields. I want to count the number of records in relationship to each date field grouped by year. This is my existing SQL statement, but it isn't counting everything correctly - mainly 2surg and 3surg. For example, it left off two records that did have a 2surg date, but they were not the same year as the 1surg date. Hope I am making sense. Any help is appreciate.



    Steve

    SELECT (Year([Surgery Date])) AS [Year], Count((Year([Surgery Date]))) AS 1Surg, Count((Year([Post op 2 date]))) AS 2Surg, Count((Year([Post op 3 date]))) AS 3Surg, ([1surg]+[2surg]+[3surg]) AS Total
    FROM [Tumor Data]
    GROUP BY (Year([Surgery Date]));

  2. #2
    thhui is offline Competent Performer
    Windows XP Access 2002 (version 10.0)
    Join Date
    Feb 2009
    Posts
    235
    (Group by) must be matched by count(Group)
    Therefore, you must separate your queries into 3 parts

    Code:
    SELECT (Year([Surgery Date])) AS [Year] , Count((Year([Surgery Date]))) AS 1Surg
    FROM [Tumor Data]
    GROUP BY (Year([Surgery Date])); 
     
    SELECT (Year([Post op 2 date]) AS [Year], Count((Year([Post op 2 date]))) AS 2Surg, 
    FROM [Tumor Data]
    GROUP BY (Year([Post op 2 date])); 
     
    SELECT (Year([Post op 3 date]) AS [Year], Count((Year([Post op 3 date]))) AS 3Surg, 
    FROM [Tumor Data]
    GROUP BY (Year([Post op 3 date]));

  3. #3
    Join Date
    Apr 2009
    Posts
    4
    Can this be put in one MS Access Query? I put it in and when I try to run it says "Characters found after end of SQL statement". Pressing the help button on this messages states that there can't be characters after the semi colon.

    Thanks

    Steve

  4. #4
    thhui is offline Competent Performer
    Windows XP Access 2002 (version 10.0)
    Join Date
    Feb 2009
    Posts
    235
    I mean separate queries.

    Not one query.

    So, 3 queries instead of one.

    PS: ; is the end of sql statement query character

  5. #5
    Join Date
    Apr 2009
    Posts
    4
    Right --- so is there a way to combine these select statements into one query and give a totals grouped by year --- so that the output would be similar to:

    Year 1Surg 2Surg 3Surg Total
    2007 20 10 5 35
    2008 23 7 6 36

    etc...?

    Thanks

    Steve

  6. #6
    thhui is offline Competent Performer
    Windows XP Access 2002 (version 10.0)
    Join Date
    Feb 2009
    Posts
    235
    Try to use union to combine 3 separate queries into one!

  7. #7
    Join Date
    Apr 2009
    Posts
    4
    Tried that - get syntax error --- here is the code:

    SELECT (Year([Surgery Date])) AS [Year] , Count((Year([Surgery Date]))) AS 1Surg
    FROM [Tumor Data]
    GROUP BY (Year([Surgery Date]))
    UNION
    SELECT (Year([Post op 2 date]) AS [Year], Count((Year([Post op 2 date]))) AS 2Surg,
    FROM [Tumor Data]
    GROUP BY (Year([Post op 2 date]))
    UNION
    SELECT (Year([Post op 3 date]) AS [Year], Count((Year([Post op 3 date]))) AS 3Surg,
    FROM [Tumor Data]
    GROUP BY (Year([Post op 3 date]));

  8. #8
    thhui is offline Competent Performer
    Windows XP Access 2002 (version 10.0)
    Join Date
    Feb 2009
    Posts
    235
    Make a query something like this below:-

    Code:
    SELECT * INTO Dbase FROM 
    [SELECT *, 1 as gp
    FROM Dbase1
    union
    SELECT *, 2 as gp
    FROM Dbase2
    union
    SELECT *, 3 as gp
    FROM Dbase3];

  9. #9
    tinytree is offline Novice
    Windows XP Access 2003
    Join Date
    Apr 2009
    Posts
    28
    that' right "union"

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

Similar Threads

  1. Counting returned records in a query
    By johncob in forum Queries
    Replies: 0
    Last Post: 02-11-2009, 05:30 PM
  2. Counting rows
    By anishap in forum Access
    Replies: 0
    Last Post: 10-08-2008, 10:41 PM
  3. Counting records based on specified criteria
    By GrnISSO in forum Queries
    Replies: 2
    Last Post: 10-04-2007, 03:07 PM
  4. Need Help counting occurences
    By anyoder in forum Queries
    Replies: 0
    Last Post: 03-09-2007, 08:53 AM
  5. Counting distinct id's
    By jqljql in forum Access
    Replies: 1
    Last Post: 09-01-2006, 07:28 AM

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