?
Solved

Help with SELECT DISTINCT

Posted on 2010-11-19
7
Medium Priority
?
453 Views
Last Modified: 2012-05-10
Can someone help me fix this SELECT query.  

Here's what I'm TRYING to do.

I have a table that stores several columns, one of which is a JobNumber.  There could be multiple records through the table that have the same exact JobNumber.

I need to get a Count of all the records in the table that have the same JobNumber.  However I also need to show only the distinct records from that table.

Here's what I tried to write but it doesn't provide what I'm looking for.

      SELECT DISTINCT (e.JobNumber), e.ArrivalDate, e.EventName,
                        (SELECT COUNT(*)
                         FROM Events
                         WHERE JobNumber = e.JobNumber) As NoOfRecords
      FROM         Events e
      WHERE e.ArrivalDate > @TodaysDate
      ORDER BY e.ArrivalDate

I want the results to look something like this:

JobNumber             Arrival Date        Event Name       No Of Records
1                             1/1/2011              Boss Meeting         15
2                             1/15/2011            Client Meeting         8

How can I achieve this?
0
Comment
Question by:cdemott33
[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
7 Comments
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 34176963
SELECT e.JobNumber, max(e.ArrivalDate), MAX(e.EventName), COUNT(*) as NoOfRecord
FROM Events e
WHERE e.ArrivalDate > @TodaysDate
group by e.JobNumber
ORDER BY e.ArrivalDate
0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 34176972
Remove the alias

SELECT JobNumber, max(ArrivalDate), MAX(EventName), COUNT(*) as 'No Of Records'
FROM Events
WHERE ArrivalDate > @TodaysDate
group by JobNumber
ORDER BY ArrivalDate
0
 

Author Comment

by:cdemott33
ID: 34176980
I'm getting this error:

Column ArrivalDate is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause.
0
Industry Leaders: 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!

 
LVL 3

Expert Comment

by:DFW_Ed
ID: 34177009
order by occurs last and with it written as-is there technically isn't an "ArrivalDate" column as aggregated columns do not have names

http://www.bennadel.com/blog/70-SQL-Query-Order-of-Operations.htm

you can name a column with "arrivaldate" or choose a different "order by" column
0
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya earned 2000 total points
ID: 34177027
SELECT JobNumber, max(ArrivalDate)  ArrivalDate, MAX(EventName), COUNT(*) as 'No Of Records'
FROM Events
WHERE ArrivalDate > @TodaysDate
group by JobNumber
ORDER BY ArrivalDate
0
 
LVL 41

Expert Comment

by:Sharath
ID: 34177285
try this
select * from (
select *,COUNT(*) over (partition by JobNumber) cnt,
       ROW_NUMBER() over (partition by JobNumber order by ArrivalDate desc) rn
  from [Events]
 where ArrivalDate > @TodaysDate) t1
where rn = 1
order by ArrivalDate

Open in new window

0
 

Author Closing Comment

by:cdemott33
ID: 34197496
thanks
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

762 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