MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 20th, 2012, 05:02 PM
oach oach is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2001
Location: knee deep in sh*t
Posts: 251 oach User rank is Second Lieutenant (5000 - 10000 Reputation Level)oach User rank is Second Lieutenant (5000 - 10000 Reputation Level)oach User rank is Second Lieutenant (5000 - 10000 Reputation Level)oach User rank is Second Lieutenant (5000 - 10000 Reputation Level)oach User rank is Second Lieutenant (5000 - 10000 Reputation Level)oach User rank is Second Lieutenant (5000 - 10000 Reputation Level)oach User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Days 17 h 56 m 46 sec
Reputation Power: 64
Send a message via AIM to oach Send a message via MSN to oach Send a message via Yahoo to oach
SUM first three rows per ID

Below is my data set, I wanted to be able to take the sum of the first three instances of the ppl per id. So ID 1020891 = 40, but 1020883=103 not 221 and so on and so forth for each unique ID. Is there a way to do this in mysql?

Thanks in advance.

Code:
ID	inquiry_date		par_id	ppl	rank	dgr_id	transfer
1020891	2012-11-20 16:15:19	10528	40	4	115204	n
1020887	2012-11-20 16:09:41	28181	40	1	428256	n
1020883	2012-11-20 16:05:36	26197	36	3	389592	n
1020883	2012-11-20 16:05:36	5641	37	5	49544	n
1020883	2012-11-20 16:05:36	19063	30	6	201640	n
1020883	2012-11-20 16:05:36	26297	30	8	391300	n
1020883	2012-11-20 16:05:36	26300	30	9	391328	n
1020883	2012-11-20 16:05:36	26298	30	10	409867	n
1020883	2012-11-20 16:05:36	26350	28	11	392192	n
1020878	2012-11-20 16:02:12	5659	37	2	49649	n
1020875	2012-11-20 15:59:08	5650	37	1	49600	n
1020871	2012-11-20 15:57:07	26042	28	4	385618	n

Reply With Quote
  #2  
Old November 21st, 2012, 02:29 AM
cafelatte cafelatte is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Mar 2008
Posts: 1,923 cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 5 Days 16 h 21 m 8 sec
Reputation Power: 377
Which rows are 'first', and why?
What's your PRIMARY KEY?

Last edited by cafelatte : November 21st, 2012 at 02:31 AM.

Reply With Quote
  #3  
Old November 21st, 2012, 09:36 AM
oach oach is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2001
Location: knee deep in sh*t
Posts: 251 oach User rank is Second Lieutenant (5000 - 10000 Reputation Level)oach User rank is Second Lieutenant (5000 - 10000 Reputation Level)oach User rank is Second Lieutenant (5000 - 10000 Reputation Level)oach User rank is Second Lieutenant (5000 - 10000 Reputation Level)oach User rank is Second Lieutenant (5000 - 10000 Reputation Level)oach User rank is Second Lieutenant (5000 - 10000 Reputation Level)oach User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Days 17 h 56 m 46 sec
Reputation Power: 64
Send a message via AIM to oach Send a message via MSN to oach Send a message via Yahoo to oach
In this case the primary key is id, and what's considered the "first" row is rank, so the 3 highest (lowest number) ranked rows for each id is to be summed.

-Neal

Reply With Quote
  #4  
Old November 21st, 2012, 11:06 AM
cafelatte cafelatte is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Mar 2008
Posts: 1,923 cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 5 Days 16 h 21 m 8 sec
Reputation Power: 377
id is not unique, so it cannot be PRIMARY, but based upon what you have said, we'll assume that the there is instead a compound PK formed on (id,rank)...
Code:
SELECT id
     , SUM(ppl) ttl
  FROM
     ( SELECT x.*
            , COUNT(*)rnk 
         FROM my_table x 
         JOIN my_table y 
           ON y.id = x.id 
          AND y.rank <= x.rank 
        GROUP 
           BY id,rank
     ) a 
 WHERE rnk <=3 
 GROUP 
    BY id;


...or something like that
Comments on this post
oach agrees: Thank you!

Reply With Quote
  #5  
Old November 26th, 2012, 01:58 PM
oach oach is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2001
Location: knee deep in sh*t
Posts: 251 oach User rank is Second Lieutenant (5000 - 10000 Reputation Level)oach User rank is Second Lieutenant (5000 - 10000 Reputation Level)oach User rank is Second Lieutenant (5000 - 10000 Reputation Level)oach User rank is Second Lieutenant (5000 - 10000 Reputation Level)oach User rank is Second Lieutenant (5000 - 10000 Reputation Level)oach User rank is Second Lieutenant (5000 - 10000 Reputation Level)oach User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Days 17 h 56 m 46 sec
Reputation Power: 64
Send a message via AIM to oach Send a message via MSN to oach Send a message via Yahoo to oach
That worked great! Thank you.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > SUM first three rows per ID

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap