Solved

Group query results

Posted on 2007-03-20
3
181 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

628 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