Solved

SQL query to get highest category

Posted on 2014-09-03
3
130 Views
Last Modified: 2014-09-20
Hi,
I have table that contains invoices . The invoice has a supplier,category and amount. I want to create a MS SQL server query to get the list of each supplier with the category that has the highest Total amount .

Data is like below :
Invoice#      suppliercode      category      amount
500      40000      A      856
501      40000      A      996
502      40000      B      1585
503      40000      C      1293
504      41001      A      1146
505      41001      A      1141
506      41001      D      599
507      41001      B      1825
508      41001      B      1328
509      42000      A      1870
510      42000      B      1964
511      42000      C      943
512      42000      A      1426
513      42000      A      1523
514      42000      B      1903


The desired result of the query should be like below :
suppliercode      category      TotalAmount
40000      A      1852
41001      B      4294
42000      C      3892
Raw-data.jpg
Desired-Output.jpg
0
Comment
Question by:TetraSA
3 Comments
 
LVL 14

Expert Comment

by:Vikas Garg
ID: 40300757
Hi,

Try this for Maximun

SELECT suppliercode, category , max(AMOUNT) MaxAmount
FROM Table 
Group By suppliercode, category

Open in new window


and this is for summation

SELECT suppliercode, category , SUM(AMOUNT) MaxAmount
FROM Table 
Group By suppliercode, category

Open in new window

0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40300783
SELECT
      suppliercode
    , category
    , supcatsum
FROM (
      SELECT
            suppliercode
          , category
          , supcatsum
          , ROW_NUMBER() OVER (PARTITION BY suppliercode ORDER BY supcatsum DESC) AS rn
      FROM (
            SELECT
                  suppliercode
                , category
                , SUM(amount) OVER (PARTITION BY suppliercode, category) supcatsum
            FROM table1
            ) sq1
      ) sq2
WHERE rn = 1
;


-- more info
**MS SQL Server 2008 Schema Setup**:

    CREATE TABLE Table1
    	([Invoice#] int, [suppliercode] int, [category] varchar(1), [amount] int)
    ;
    	
    INSERT INTO Table1
    	([Invoice#], [suppliercode], [category], [amount])
    VALUES
    	(500, 40000, 'A', 856),
    	(501, 40000, 'A', 996),
    	(502, 40000, 'B', 1585),
    	(503, 40000, 'C', 1293),
    	(504, 41001, 'A', 1146),
    	(505, 41001, 'A', 1141),
    	(506, 41001, 'D', 599),
    	(507, 41001, 'B', 1825),
    	(508, 41001, 'B', 1328),
    	(509, 42000, 'A', 1870),
    	(510, 42000, 'B', 1964),
    	(511, 42000, 'C', 943),
    	(512, 42000, 'A', 1426),
    	(513, 42000, 'A', 1523),
    	(514, 42000, 'B', 1903)
    ;

**Query 1**:

    SELECT
          suppliercode
        , category
        , supcatsum
    FROM (
          SELECT
                *
              , ROW_NUMBER() OVER (PARTITION BY suppliercode ORDER BY supcatsum DESC) AS rn
          FROM (
                SELECT
                      *
                    , SUM(amount) OVER (PARTITION BY suppliercode, category) supcatsum
                FROM table1
                ) sq1
          ) sq2
    WHERE rn = 1
    

**[Results][2]**:
    
    | SUPPLIERCODE | CATEGORY | SUPCATSUM |
    |--------------|----------|-----------|
    |        40000 |        A |      1852 |
    |        41001 |        B |      3153 |
    |        42000 |        A |      4819 |



  [1]: http://sqlfiddle.com/#!3/4545c/8

Open in new window

0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 40300790
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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

867 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

24 Experts available now in Live!

Get 1:1 Help Now