Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 155
  • Last Modified:

SQL query to get highest category

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
TetraSA
Asked:
TetraSA
1 Solution
 
Vikas GargBusiness Intelligence DeveloperCommented:
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
 
PortletPaulCommented:
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
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now