Solved

Group query results

Posted on 2007-03-20
3
174 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 your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now