Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 14th, 2008, 10:22 PM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default SQL Getting duplicates

I am workng with an Access app and have run into some SQL behaviour I normally sort out using VBS in my ASP page.......I can not seem to get this issue sorted:

TBL_Support_Call is the main tble. For each record in this tble they can be multiple records in the TBL_Call_Staus_History table. EG if a call gets opened a records gets written containing the Status and the date. Then the records status may change four times therefore four records will be written the history table. How can I ensure the query only returns one TBL_Support_Call record no matter how many entries it has in the TBL_Call_Staus_History table?

Here is the working query which insists giving me duplicates according to the number of records in the history table


SELECT DISTINCT Format([DateTime],"dd/mm/yyyy") AS [Date Time], TBL_Support_Call.Ref, TBL_Support_Call.[Issue Summary],
TBL_Support_Call.Comments, TBL_FollowUp.Comments AS [Follow up Comments], TBL_Support_Call.[User Name],
TBL_Support_Call.BU, TBL_Support_Call.Issue, TBL_Support_Call.Link, TBL_Support_Call.Category,
TBL_Support_Call.[Functional Area], TBL_Support_Call.[Contact Source], TBL_Support_Call.Status,
Format([TBL_Call_Staus_History.CloseDate],"dd/mm/yyyy") AS [Closed Date], TBL_Support_Call.[Logged BY],
TBL_Support_Call.Owner FROM (TBL_Support_Call
LEFT JOIN TBL_FollowUp ON TBL_Support_Call.Ref = TBL_FollowUp.[Original Call Ref])
LEFT JOIN TBL_Call_Staus_History ON TBL_Support_Call.Ref = TBL_Call_Staus_History.refID
ORDER BY TBL_Support_Call.Ref DESC;


Further to my post if I remove:

Format([TBL_Call_Staus_History.CloseDate],"dd/mm/yyyy") AS [Closed Date], TBL_Support_Call.[Logged BY],

from the query my duplicate issue goes away. Can a better join or better SQL overcome this?

TYIA
__________________
Wind is your friend
Matt

Last edited by mat41; December 15th, 2008 at 12:18 AM..
 
Old December 15th, 2008, 02:03 AM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

Seems to work......

SELECT Format([DateTime],"dd/mm/yyyy") AS [Date Time]
, TBL_Support_Call.Ref
, TBL_Support_Call.[Issue Summary]
, TBL_Support_Call.Comments
, TBL_FollowUp.Comments AS [Follow up Comments]
, TBL_Support_Call.[User Name]
, TBL_Support_Call.BU
, TBL_Support_Call.Issue
, TBL_Support_Call.Link
, TBL_Support_Call.Category
, TBL_Support_Call.[Functional Area]
, TBL_Support_Call.[Contact Source]
, TBL_Support_Call.Status
, Format([mmmm.maxdate],"dd/mm/yyyy") AS [Closed Date]
, TBL_Support_Call.[Logged BY]
, TBL_Support_Call.Owner
, datediff("d",[DateTime],[mmmm.maxdate]) AS [Days Opened]
FROM (
TBL_Support_Call
LEFT OUTER
JOIN TBL_FollowUp
ON TBL_FollowUp.[Original Call Ref] = TBL_Support_Call.Ref
)
LEFT OUTER
JOIN ( SELECT refID
, MAX(CloseDate) AS maxdate
FROM TBL_Call_Staus_History
GROUP
BY refID ) AS mmmm
ON mmmm.refID = TBL_Support_Call.Ref
ORDER
BY TBL_Support_Call.Ref DESC;
__________________
Wind is your friend
Matt





Similar Threads
Thread Thread Starter Forum Replies Last Post
Yes to duplicates MMJiggy62 Beginning VB 6 3 July 14th, 2006 11:51 AM
Delete duplicates? boson SQL Language 2 August 11th, 2004 04:47 PM
Avoiding Duplicates fixitman Access 1 April 27th, 2004 10:43 AM
eliminating all but 1 of duplicates nlicata SQL Server 2000 3 November 21st, 2003 04:52 PM
Duplicates ashley_y Access 11 August 14th, 2003 03:41 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.