Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL query to get highest category

Posted on 2014-09-03
3
Medium Priority
?
151 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 2000 total points
ID: 40300790
0

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

721 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