sql - Summing and ordering at once -


I have a table of commands where I need to know which 3 partners_ID has the largest sum of the sum , And make the three of them the largest from the largest.

  testdb = # SELECT amount_total, sale_order to partner_id; Zodiac_tools | Partner_id -------------- + ------------ 1244.00 | 93065. 90. 12 3600.00 3 2263.00 | 25,000.00 10 3263.00 | 3 123.00 | 25 5400.00 | 12 (8 lines)   

Just starting SQL, I am confused ...

consolidated amount

If you want to list the cumulative amount, it can be as simple as:

  SELECT partner_id, sum ( Amount_sun) AS amout_supertotal FROM 1 order by sales order group 2 DESC limit 3;   

1 in Group B1 is a numerical parameter, which refers to the position in the SELECT list in this case Only a formal shortcut for the partner_id by the group .

This ignores a special case where more than three partners will qualify and (arguably for lack of definition) will arbitrarily raise. / P>

Personal Quantity
  SELECT partner_id, amount_exe sales_order JOIN (SELECT partner_id, on rank) (amount (amount) by DESC) ORDER 2 by Sirius 3) Top 3 Top 3 by Using (partner_id) ORDER RNK;   

On the other hand, this includes another partner, if more than 3 partners qualify for the top 3. it gives you.

Function execute) after the ranks. The ORDER BY window is applied after the work and the LIMIT is finalized all in one subquery.

Then to join the sub-table on this subquiry, so that only the top dogs should remain in the result and in the order by rnk .

> Window tasks require PostgreSQL 8.4 or later.

It's rather advanced stuff that lets you learn SQL in a few simple ways.

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -