Results 1 to 6 of 6
  1. #1
    SPW_12 is offline Novice
    Windows XP Access 2007
    Join Date
    Jan 2012
    Posts
    14

    Two queries in one

    I built a dynamic query interface form for end users such that they can query any way they like; this is a financial db. One of the options is to query on date. If the user selects a date range, the sum of unique accts is aggregated by date. If the user wants to see the sum of a date range, but not return a row for each date within the date range, I need to first aggregate, then query on that aggregated. Querying a query...
    Is there a way to combine the following into one query? In one shot?



    Here's the SQL of the first:
    SELECT DISTINCT tbl_GL.Months, tbl_Physician.LastName, tbl_GL.Account, tbl_GL.Amount
    FROM tbl_Physician INNER JOIN tbl_GL ON tbl_Physician.PhysicianID = tbl_GL.PhysicianID
    GROUP BY tbl_GL.Months, tbl_Physician.LastName, tbl_GL.Account, tbl_GL.Amount;

    Then the other:
    SELECT Example.LastName, Example.Account, Sum(Example.Amount) AS SumOfAmount
    FROM Example
    GROUP BY Example.LastName, Example.Account;

    Thanks

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    You can do copy/paste in SQL View window of the first SQL string into the second. The result would be:

    SELECT Example.LastName, Example.Account, Sum(Example.Amount) AS SumOfAmount
    FROM (SELECT DISTINCT tbl_GL.Months, tbl_Physician.LastName, tbl_GL.Account, tbl_GL.Amount
    FROM tbl_Physician INNER JOIN tbl_GL ON tbl_Physician.PhysicianID = tbl_GL.PhysicianID
    GROUP BY tbl_GL.Months, tbl_Physician.LastName, tbl_GL.Account, tbl_GL.Amount) AS Example
    GROUP BY Example.LastName, Example.Account;
    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.

  3. #3
    SPW_12 is offline Novice
    Windows XP Access 2007
    Join Date
    Jan 2012
    Posts
    14
    thank you, this works, but I omitted the part where I tell the first query the date range parameter. Here is how I modified it:

    SELECT Example.LastName, Example.Account, Sum(Example.Amount) AS SumOfAmount
    FROM (SELECT DISTINCT tbl_GL.Months, tbl_Physician.LastName, tbl_GL.Account, tbl_GL.Amount
    FROM tbl_Physician INNER JOIN tbl_GL ON tbl_Physician.PhysicianID = tbl_GL.PhysicianID
    GROUP BY tbl_GL.Months, tbl_Physician.LastName, tbl_GL.Account, tbl_GL.Amount) AS Example
    GROUP BY Example.LastName, Example.Account
    HAVING (((tbl_GL.Months) Between #8/1/2012# And #7/1/2012#));

    thanks for the help!

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    The nested query works? I am surprised because the date criteria is in the outer SQL and not the inner.
    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.

  5. #5
    SPW_12 is offline Novice
    Windows XP Access 2007
    Join Date
    Jan 2012
    Posts
    14
    well, i thought it was working but the results are not correct. I added quite a few more parameters, I am trying to get it to work. Here are the two queries I need to combine:

    1st
    SELECT DISTINCT tbl_GL.Months, tbl_Physician.LastName, tbl_PhysSpecialtyID.Specialty, tbl_DemoCountyID.County, tbl_CoA.Sub_Rpt_Descr, tbl_PhysSpecialtyID.SpecialtyID, tbl_DemoCountyID.CountyID, tbl_CoA.Category_ID, tbl_GL.Amount
    FROM tbl_PhysSpecialtyID INNER JOIN (tbl_Physician INNER JOIN ((tbl_DemoCountyID INNER JOIN (tbl_DemoAddressID INNER JOIN tbl_Demographics ON tbl_DemoAddressID.AddressID = tbl_Demographics.AddressID) ON tbl_DemoCountyID.CountyID = tbl_Demographics.CountyID) INNER JOIN (tbl_CoA INNER JOIN tbl_GL ON tbl_CoA.Account = tbl_GL.Account) ON tbl_Demographics.PhysicianID = tbl_GL.PhysicianID) ON tbl_Physician.PhysicianID = tbl_GL.PhysicianID) ON tbl_PhysSpecialtyID.SpecialtyID = tbl_Physician.SpecialtyID
    GROUP BY tbl_GL.Months, tbl_Physician.LastName, tbl_PhysSpecialtyID.Specialty, tbl_DemoCountyID.County, tbl_CoA.Sub_Rpt_Descr, tbl_PhysSpecialtyID.SpecialtyID, tbl_DemoCountyID.CountyID, tbl_CoA.Category_ID, tbl_GL.Amount, tbl_Physician.Inactive
    HAVING (((tbl_GL.Months) Between #8/1/2011# And #7/1/2012#) AND ((tbl_CoA.Category_ID)=1 Or (tbl_CoA.Category_ID)=2) AND ((tbl_Physician.Inactive)=False));

    2nd
    SELECT Example.LastName, Example.Specialty, Example.County, Example.Sub_Rpt_Descr, Sum(Example.Amount) AS SumOfAmount
    FROM Example
    GROUP BY Example.LastName, Example.Specialty, Example.County, Example.Sub_Rpt_Descr;

  6. #6
    SPW_12 is offline Novice
    Windows XP Access 2007
    Join Date
    Jan 2012
    Posts
    14
    Thanks June7, I finally got it with your help.

    SELECT Example.LastName, Example.Specialty, Example.County, Example.Sub_Rpt_Descr, Sum(Example.Amount) AS SumOfAmount
    FROM (SELECT DISTINCT tbl_GL.Months, tbl_Physician.LastName, tbl_PhysSpecialtyID.Specialty, tbl_DemoCountyID.County, tbl_CoA.Sub_Rpt_Descr, tbl_PhysSpecialtyID.SpecialtyID, tbl_DemoCountyID.CountyID, tbl_CoA.Category_ID, tbl_GL.Amount
    FROM tbl_PhysSpecialtyID INNER JOIN (tbl_Physician INNER JOIN ((tbl_DemoCountyID INNER JOIN (tbl_DemoAddressID INNER JOIN tbl_Demographics ON tbl_DemoAddressID.AddressID = tbl_Demographics.AddressID) ON tbl_DemoCountyID.CountyID = tbl_Demographics.CountyID) INNER JOIN (tbl_CoA INNER JOIN tbl_GL ON tbl_CoA.Account = tbl_GL.Account) ON tbl_Demographics.PhysicianID = tbl_GL.PhysicianID) ON tbl_Physician.PhysicianID = tbl_GL.PhysicianID) ON tbl_PhysSpecialtyID.SpecialtyID = tbl_Physician.SpecialtyID
    GROUP BY tbl_GL.Months, tbl_Physician.LastName, tbl_PhysSpecialtyID.Specialty, tbl_DemoCountyID.County, tbl_CoA.Sub_Rpt_Descr, tbl_PhysSpecialtyID.SpecialtyID, tbl_DemoCountyID.CountyID, tbl_CoA.Category_ID, tbl_GL.Amount, tbl_Physician.Inactive
    HAVING (((tbl_GL.Months) Between #8/1/2011# And #7/1/2012#) AND ((tbl_CoA.Category_ID)=1 Or (tbl_CoA.Category_ID)=2) AND ((tbl_Physician.Inactive)=False))) As Example
    GROUP BY Example.LastName, Example.Specialty, Example.County, Example.Sub_Rpt_Descr;

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

Similar Threads

  1. Using Sub Queries?
    By djclntn in forum Queries
    Replies: 4
    Last Post: 04-05-2012, 08:58 AM
  2. Bit of a odd Queries
    By winterh in forum Queries
    Replies: 2
    Last Post: 03-14-2012, 03:03 PM
  3. Sub Queries
    By waqas in forum Queries
    Replies: 5
    Last Post: 01-24-2012, 11:48 AM
  4. Sum Queries
    By Lilsug in forum Access
    Replies: 4
    Last Post: 12-17-2010, 08:45 AM
  5. need help with queries
    By gromit95 in forum Queries
    Replies: 1
    Last Post: 02-06-2009, 06:50 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