Solved

SQL Query with count of duplicates

Posted on 2011-02-19
3
255 Views
Last Modified: 2012-05-11
I have trucks entereing different loading zones. Each time the truck enters a loading zone, it makes an entry into our mysql table witht the following fields:  id, truckName, date, latitude, longitude, speedInZone, zoneName. Through out the day the trucks will enter a leave the zone multiple times.  I need to pull the following data:

truckName, DateTime of last entry in this zone, zoneName, Count of how many times this truck entered this zone.

I am thinking maybe a group by truckName, zoneName may be the right direction but I need a little help.
0
Comment
Question by:dtechfish
  • 2
3 Comments
 
LVL 26

Accepted Solution

by:
tigin44 earned 500 total points
ID: 34934896
try this

SELECT truckName, MAX(date) AS lastentry, zoneName, COUNT(*)
 FROM yourTable
 GROUP BY truckName, zoneName
 
0
 
LVL 40

Expert Comment

by:Sharath
ID: 34934904
try this.
select id,zonename,count(*) cnt,max(date) last_entry
  from your_table
 group by id,zonename

Open in new window

if you did not get what you are looking for, provide some sample data with expected result.
0
 
LVL 40

Expert Comment

by:Sharath
ID: 34934908
replace id with truckname in my query. tigin got it
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Query syntax 10 40
Need help subtracting a value within my script 7 42
subtract 1 in Access 2003 query 7 39
Excel - SQL export question 3 8
As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
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, f…

896 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

11 Experts available now in Live!

Get 1:1 Help Now