Solved

SQL query to get highest category

Posted on 2014-09-03
3
144 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 15

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 49

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 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 40300790
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

631 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