Solved

SQL query to get highest category

Posted on 2014-09-03
3
128 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

758 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

18 Experts available now in Live!

Get 1:1 Help Now