Results 1 to 9 of 9
  1. #1
    Sheba is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2014
    Posts
    239

    Exclamation Combining queries

    Hi,

    I have these two columns in my table. I want my query to tell me how many records have oxygen indicated and also, how many of the OxyIndic instances were given oxygen. According to my table below, the answers should be 4 and 3. I created the queries:

    Query1
    SELECT Sum(IIf(OxyIndic="Yes",1,0)) AS Oxygen_Indication
    from table1;
    and when I run it separately, it gives me 4

    Query2
    SELECT Sum(IIf(O2Indic="Yes",1,0)) AS HowMany
    FROM table1
    WHERE O2Given="Yes";


    when I run them separately, they give me 4 and 3 respectively but when I try to combine both queries I get output 3 and 3. Could someone advise on how to fix this please? Your help is much appreciated

    table1
    OxyIndic OxyGiven
    Yes Yes
    Yes No
    Yes Yes
    Yes Yes
    No Yes





  2. #2
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    How about:
    Code:
    SELECT Sum(IIf(OxyIndic="Yes",1,0)) AS Oxygen_Indication, Sum(IIf((OxyIndic="Yes") And (O2Indic="Yes"),1,0)) AS HowMany
    FROM table1;

  3. #3
    Sheba is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2014
    Posts
    239

    Exclamation

    okay so I used UNION. It is giving me the correct figures only it is listing them under one column. I want them in separate columns

  4. #4
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    You probably missed my reply above in passing. Try that out.

  5. #5
    Sheba is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2014
    Posts
    239
    Hi JoeM, thanks for your response. I tried your SQL code but it asked me to "Enter Parameter Value"...

  6. #6
    Sheba is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2014
    Posts
    239
    what is wrong with this code? It is giving the correct values but listing them under one column... I want them in two columns

    SELECT Sum(IIf([u].[oxyIndic]="Yes",1,0)) AS Oxygen_Indication FROM table1 u
    UNION
    SELECT Sum(IIf([t].[oxyIndic]"Yes",1,0)) AS HowMany FROM table1 t
    WHERE [t].[oxyGiven]="Yes";

  7. #7
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    There seems to be an issue with the fields used in Query2 in your original post (i.e. field names are different than what you are showing in your table).
    Try changing "O2Ind" to "OxyGiven" in the SQL code I gave you (make sure those two field names match whatever field names you are really using).

  8. #8
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    what is wrong with this code? It is giving the correct values but listing them under one column... I want them in two columns
    Union Queries do not add fields (columns), they add records (rows).

    If you clean up your field names, the code I gave you should work (see previous reply).

  9. #9
    Sheba is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2014
    Posts
    239
    It worked. Very much appreciated.

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

Similar Threads

  1. Combining queries
    By betmck in forum Queries
    Replies: 1
    Last Post: 09-22-2014, 12:26 PM
  2. combining 2 queries
    By sdel_nevo in forum Queries
    Replies: 2
    Last Post: 07-23-2014, 04:41 AM
  3. Help with Combining Queries
    By racefan91 in forum Queries
    Replies: 13
    Last Post: 10-07-2013, 02:42 PM
  4. combining two queries
    By camell in forum Queries
    Replies: 4
    Last Post: 03-04-2011, 02:41 PM
  5. Combining queries
    By wildlifeaccess in forum Queries
    Replies: 10
    Last Post: 09-20-2010, 07:35 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