Author Topic: SQL Query Combining Data  (Read 517 times)

0 Members and 1 Guest are viewing this topic.

Offline theKit

  • Devotee
  • **
  • Posts: 178
    • View Profile
SQL Query Combining Data
« on: October 11, 2014, 03:44:17 am »
I have a table that looks like this:

Code: [Select]
Table Name: upAssoc

k  p  f
1  3  80
2  3  50
3  1  2
4  2  8
5  1  22

What I want to do is:
Combine all the columns with the same number in P
SUM the F column
Order them by highest to lowest in descending order based on the sum of F.

So basically I want this output:

Code: [Select]
3  130 //80 + 50 (F column)
1  22  // next largest number
2  8   // next..
1  2  // smallest


How do I write this query in a single line?

Offline theKit

  • Devotee
  • **
  • Posts: 178
    • View Profile
Re: SQL Query Combining Data
« Reply #1 on: October 11, 2014, 03:40:24 pm »
I need to stop trying to code late at night, thanks for all the help you retards this one was simple

Quote
SELECT p, SUM(f) FROM upAssoc GROUP BY f DESC;


Offline komokazi

  • Disciple
  • ***
  • Posts: 280
  • I hate niggers
    • View Profile
Re: SQL Query Combining Data
« Reply #2 on: October 11, 2014, 04:01:37 pm »
And we're the retards...
I hate niggers

Offline SBTlauien

  • Disciple
  • ***
  • Posts: 454
  • ส็็็็็็็็็.ส็็็็็็็็็.ส็็็็็็็็็
    • View Profile
Re: SQL Query Combining Data
« Reply #3 on: October 12, 2014, 06:06:49 am »
I probably could have got it if I saw it in time.  I'm not much for SQL, but I did play around with it a little bit.

I actually want to get back to learning soon.  SQL injection especially.  Maybe I'll get fired soon... ;)