Solved

Group query results

Posted on 2007-03-20
3
180 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…

734 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