Solved

Group query results

Posted on 2007-03-20
3
178 Views
Last Modified: 2010-03-20
here is a query that i have set up:

SELECT dbo_EmailHistory.EmailID, dbo_EmailHistory.EDate, dbo_EmailHistory.Removed, dbo_EmailProsDetail.FClickDate
FROM dbo_EmailHistory LEFT JOIN dbo_EmailProsDetail ON dbo_EmailHistory.EmailID = dbo_EmailProsDetail.EmailID
WHERE (((dbo_EmailHistory.EDate) Between [Start Date] And [Stop Date]) AND ((dbo_EmailHistory.Removed) Is Null) AND ((dbo_EmailHistory.UserID) Not In (1,3,80))) OR (((dbo_EmailHistory.Removed)="isblank"));

i only need to see one record for each dbo_EmailHistory.EmailID, but because I am pulling data from dbo_EmailProsDetail there are some instances that I get multiple results for the same EmailID.  For example here are some of the results that i get when i run the query above:

EmailID      EDate                      Removed      FClickDate
396      5/1/2006 10:49:25 AM            5/1/2006 11:05:17 AM
396      5/1/2006 10:49:25 AM            5/1/2006 11:05:17 AM
397      5/2/2006 2:10:05 PM            5/2/2006 2:11:24 PM
397      5/2/2006 2:10:05 PM            5/2/2006 2:11:24 PM
399      5/2/2006 2:41:48 PM            5/2/2006 2:42:35 PM
399      5/2/2006 2:41:48 PM            5/2/2006 2:42:35 PM
399      5/2/2006 2:41:48 PM            5/2/2006 2:42:35 PM
400      5/2/2006 2:51:21 PM            5/2/2006 2:51:34 PM
400      5/2/2006 2:51:21 PM            5/2/2006 2:51:34 PM
400      5/2/2006 2:51:21 PM            5/2/2006 2:51:34 PM
401      5/2/2006 3:51:43 PM            5/2/2006 3:51:55 PM
402      5/2/2006 9:08:29 PM            5/3/2006 10:31:43 AM

but the results i need are:

EmailID      EDate                      Removed      FClickDate
396      5/1/2006 10:49:25 AM            5/1/2006 11:05:17 AM
397      5/2/2006 2:10:05 PM            5/2/2006 2:11:24 PM
399      5/2/2006 2:41:48 PM            5/2/2006 2:42:35 PM
400      5/2/2006 2:51:21 PM            5/2/2006 2:51:34 PM
401      5/2/2006 3:51:43 PM            5/2/2006 3:51:55 PM
402      5/2/2006 9:08:29 PM            5/3/2006 10:31:43 AM

by the way my db operates if there is an FClick for an EmailID it will be the same every time so i only need to see it one time.  i have tried everything i can think of to get the results to display like above but i can not figure it out.  does anyone know what i can do to get this to work?  i know it will work i just don't know how to get there.
0
Comment
Question by:scottspivey
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
Steve Dubyo earned 500 total points
ID: 18757639
You could replace SELECT with SELECT DISTINCT to get rid of duplicates.

To group them properly you would need EmailId under GROUP BY then max() on the other fields, looking at your data though you should be fine with ..

SELECT DISTINCT dbo_EmailHistory.EmailID, dbo_EmailHistory.EDate, dbo_EmailHistory.Removed, dbo_EmailProsDetail.FClickDate
FROM dbo_EmailHistory LEFT JOIN dbo_EmailProsDetail ON dbo_EmailHistory.EmailID = dbo_EmailProsDetail.EmailID
WHERE (((dbo_EmailHistory.EDate) Between [Start Date] And [Stop Date]) AND ((dbo_EmailHistory.Removed) Is Null) AND ((dbo_EmailHistory.UserID) Not In (1,3,80))) OR (((dbo_EmailHistory.Removed)="isblank"));
0
 
LVL 1

Author Comment

by:scottspivey
ID: 18757706
influenz,

thx for the quick response.  i must be brain dead casue i have used SELECT DISTINCT in a lot of queries that i have currently set up but i never though of it.  maybe i have been looking at this data too long or something.

obviously your solution worked perfectly.  thx again for the fast response.

scott
0
 
LVL 5

Expert Comment

by:Steve Dubyo
ID: 18757746
"maybe i have been looking at this data too long"
I know that feeling !

No problem!
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question