Solved

Help with SELECT DISTINCT

Posted on 2010-11-19
7
444 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
7 Comments
 
LVL 32

Expert Comment

by:ewangoya
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:ewangoya
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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:
ewangoya earned 500 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 40

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
In this article I will describe the Detach & Attach 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.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

786 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