Results 1 to 7 of 7
  1. #1
    Huddle is offline Competent Performer
    Windows XP Access 2010 64bit
    Join Date
    Jun 2010
    Posts
    318

    Union Query - Removing Duplicates from Results or a Better way to do this.

    The below Union Query comes from two basically identical queries. One lists a group of buildings with a count of inspection done within a certain range. The other lists a group of building that did not have inspections done during that range.

    My purpose is this. I need to do a report on all the buildings that are due inspections for the quarter. It was easy enough to see the status on the ones that had at least one inspection done during the quarter but for those who did not have an inspection done the initial query did not pull up the building. I ended up making 5 total queries (including the union) to get this to work and of course I have duplicates due to the union of the final two queries. There has to be a simlar way to do this.

    Any suggestions or help would be appreciated.



    SELECT Q_PI_Qrtly_Insp_Ct_Range.District, Q_PI_Qrtly_Insp_Ct_Range.Bldg_Number, Q_PI_Qrtly_Insp_Ct_Range.Bldg_Name, Q_PI_Qrtly_Insp_Ct_Range.Address, Q_PI_Qrtly_Insp_Ct_Range.City, Q_PI_Qrtly_Insp_Ct_Range.State, Q_PI_Qrtly_Insp_Ct_Range.FSL, " " AS InspDateBlank, Sum(Q_PI_Qrtly_Insp_Ct_Range.InspDateCt) AS SumOfInspDateCt, Q_PI_Qrtly_Insp_Ct_Range.Due
    FROM Q_PI_Qrtly_Insp_Ct_Range
    GROUP BY Q_PI_Qrtly_Insp_Ct_Range.District, Q_PI_Qrtly_Insp_Ct_Range.Bldg_Number, Q_PI_Qrtly_Insp_Ct_Range.Bldg_Name, Q_PI_Qrtly_Insp_Ct_Range.Address, Q_PI_Qrtly_Insp_Ct_Range.City, Q_PI_Qrtly_Insp_Ct_Range.State, Q_PI_Qrtly_Insp_Ct_Range.FSL, Q_PI_Qrtly_Insp_Ct_Range.Due;
    UNION SELECT Q_PI_Qrtly_Insp_Ct_Range_Null.District, Q_PI_Qrtly_Insp_Ct_Range_Null.Bldg_Number, Q_PI_Qrtly_Insp_Ct_Range_Null.Bldg_Name, Q_PI_Qrtly_Insp_Ct_Range_Null.Address, Q_PI_Qrtly_Insp_Ct_Range_Null.City, Q_PI_Qrtly_Insp_Ct_Range_Null.State, Q_PI_Qrtly_Insp_Ct_Range_Null.FSL, " " AS InspDateSpace, " " AS CountSpace, Q_PI_Qrtly_Insp_Ct_Range_Null.Due
    FROM Q_PI_Qrtly_Insp_Ct_Range_Null
    GROUP BY Q_PI_Qrtly_Insp_Ct_Range_Null.District, Q_PI_Qrtly_Insp_Ct_Range_Null.Bldg_Number, Q_PI_Qrtly_Insp_Ct_Range_Null.Bldg_Name, Q_PI_Qrtly_Insp_Ct_Range_Null.Address, Q_PI_Qrtly_Insp_Ct_Range_Null.City, Q_PI_Qrtly_Insp_Ct_Range_Null.State, Q_PI_Qrtly_Insp_Ct_Range_Null.FSL, Q_PI_Qrtly_Insp_Ct_Range_Null.Due;

  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
    UNION does not allow duplicate records unless use the ALL parameter. Some value is different which causes records to be unique.

    Could you join the query that counts the inspections to the table of buildings - jointype 'Include all records from Buildings ...". The records where the count field is null did not have inspections in that period
    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
    Huddle is offline Competent Performer
    Windows XP Access 2010 64bit
    Join Date
    Jun 2010
    Posts
    318
    The buildings that have had an inspection during the range show up twice as shows below. I couldn't tie it to the building table because some buildings don't have posts so I have to query those out.

    District Bldg_Number FSL SumOfInspDateCt Due
    D2 WY0030 4 50
    D2 WY0030 4 26 50
    D2 WY1150 1 2
    D2 WY1150 1 1 2
    D2 WY1219 2 2
    D2 WY1228 3 25
    D2 WY1228 3 3 25
    D2 WY1235 1 2
    D2 WY1307 1 2
    D2 WY1324 1 2
    D2 WY1336 1 2

  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 buildings show twice but the records are different because of the SumOfInspDateCt value. If you don't want the building duplicated why use the UNION?

    Don't understand why you can't join the count query to the table of all buildings. With the jointype I described, all buildings will show. Isn't that what you want - to see data in report on all buildings?
    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
    Huddle is offline Competent Performer
    Windows XP Access 2010 64bit
    Join Date
    Jun 2010
    Posts
    318
    There's two problems with joining the count query with the building table. 1) not all buildings have posts so it will list buildings that don't apply, 2) it count inspections outside the date range if I change the relationships to only list buildings with posts.

    I have it where it works with the union if I can figure out how to do a count within the union. where would I add "Count(Tbl_PI_Post_Insp.Insp_Date) AS CountOfInsp_Date" into the SQL to accomplish this? Or can it be done?

    SELECT Tbl_Building_Information.District, Tbl_Building_Information.Bldg_Number, Tbl_Building_Information.Bldg_Name, Tbl_Building_Information.Address, Tbl_Building_Information.City, Tbl_Building_Information.State, Tbl_Building_Information.FSL, Tbl_PI_Post_Insp.Insp_Date, IIf([FSL]=4,DateDiff("ww",[Forms]![Fm_Date_Parameter]![StartDate],[Forms]![Fm_Date_Parameter]![EndDate])*2,IIf([FSL]=3,DateDiff("ww",[Forms]![Fm_Date_Parameter]![StartDate],[Forms]![Fm_Date_Parameter]![EndDate]),2)) AS Due
    FROM (Tbl_Building_Information RIGHT JOIN Tbl_PI_Post ON Tbl_Building_Information.Bldg_Number = Tbl_PI_Post.PI_Bldg_Number) RIGHT JOIN Tbl_PI_Post_Insp ON Tbl_PI_Post.PostID = Tbl_PI_Post_Insp.PostID_Number
    WHERE (((Tbl_PI_Post_Insp.Insp_Date) Between [Forms]![Fm_Date_Parameter]![StartDate] And [Forms]![Fm_Date_Parameter]![EndDate]));
    UNION SELECT Tbl_Building_Information.District, Tbl_PI_Post.PI_Bldg_Number, Tbl_Building_Information.Bldg_Name, Tbl_Building_Information.Address, Tbl_Building_Information.City, Tbl_Building_Information.State, Tbl_Building_Information.FSL, " " AS InspBlank, IIf([FSL]=4,DateDiff("ww",[Forms]![Fm_Date_Parameter]![StartDate],[Forms]![Fm_Date_Parameter]![EndDate])*2,IIf([FSL]=3,DateDiff("ww",[Forms]![Fm_Date_Parameter]![StartDate],[Forms]![Fm_Date_Parameter]![EndDate]),2)) AS Due
    FROM (Tbl_Building_Information INNER JOIN Tbl_PI_Post ON Tbl_Building_Information.Bldg_Number = Tbl_PI_Post.PI_Bldg_Number) LEFT JOIN Tbl_PI_Post_Insp ON Tbl_PI_Post.PostID = Tbl_PI_Post_Insp.PostID_Number
    GROUP BY Tbl_Building_Information.District, Tbl_PI_Post.PI_Bldg_Number, Tbl_Building_Information.Bldg_Name, Tbl_Building_Information.Address, Tbl_Building_Information.City, Tbl_Building_Information.State, Tbl_Building_Information.FSL, IIf([FSL]=4,DateDiff("ww",[Forms]![Fm_Date_Parameter]![StartDate],[Forms]![Fm_Date_Parameter]![EndDate])*2,IIf([FSL]=3,DateDiff("ww",[Forms]![Fm_Date_Parameter]![StartDate],[Forms]![Fm_Date_Parameter]![EndDate]),2));

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    I would have to work with data to analyse. If you want to provide, follow instructions at bottom of my post.
    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.

  7. #7
    Huddle is offline Competent Performer
    Windows XP Access 2010 64bit
    Join Date
    Jun 2010
    Posts
    318
    Actually playing around with it that isn't going to work either. Let me think it through some more and I'll get back with you when I get to a point I can be clear in what I need and the direction I'm going.

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

Similar Threads

  1. Union Query Duplicates data
    By Ray67 in forum Queries
    Replies: 2
    Last Post: 10-19-2012, 07:12 AM
  2. SQL - removing zero results from query
    By kagoodwin13 in forum Programming
    Replies: 2
    Last Post: 05-11-2012, 03:21 PM
  3. Removing duplicates
    By DAVID W in forum Access
    Replies: 5
    Last Post: 12-21-2011, 03:15 PM
  4. Removing duplicates & printing uniques
    By sp3cialed in forum Access
    Replies: 1
    Last Post: 08-06-2011, 12:31 AM
  5. Need help removing duplicates
    By warlock in forum Queries
    Replies: 1
    Last Post: 04-14-2011, 03:44 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