Solved

Group query results

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Result Set to XML 4 36
Select only the top record in a left join 13 35
SQL Server 2012 r2 - Make Temp Table Query Faster 5 43
awk and Pythagoras? 5 6
As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
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.…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

770 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