Solved

How can I group column values as per sample?

Posted on 2014-07-22
5
136 Views
Last Modified: 2014-08-08
These are the sample data that I want to group:
Description      Amount
Dues      200
Dues      200
Dues      200
Dues      200
Dues      200
Dues      200
Dues      200
Dues      200
Donation      50
Donation      100
Donation      150
Donation      200
Donation      250
Donation      300
Others      100
Others      100
Others      100
Others      100
Interest      50
Tax      8

I want  to have them summarized as follows:

SUMMARY      
      
      
Dues             1600
Donation      1050
Others              400
Interest              50
Less Tax       -8

Net                  3092


Thanks.
0
Comment
Question by:JimiJ13
5 Comments
 
LVL 15

Accepted Solution

by:
Vikas Garg earned 250 total points
ID: 40211006
Hi,

Please find the Query for the same
;WITH CTE AS
(
SELECT Description, SUM(CASE WHEN Description = 'Tax' THEN -Amount ELSE Amount END) AMNT
FROM T1 GROUP BY Description
)

SELECT * FROM CTE
UNION ALL
SELECT 'NET', SUM(AMNT) FROM CTE

Open in new window

0
 
LVL 3

Expert Comment

by:stevejacob68
ID: 40211016
Hi,
Try this code:
Select Description,sum(amount) from tablename group by description;
0
 
LVL 92

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 250 total points
ID: 40213408
The following seems to be working for me:

CREATE TABLE MyTable (Description varchar(50), Amount numeric(10,2))
INSERT INTO MyTable (Description, Amount) VALUES ('Dues', 200)
INSERT INTO MyTable (Description, Amount) VALUES ('Dues', 200)
INSERT INTO MyTable (Description, Amount) VALUES ('Dues', 200)
INSERT INTO MyTable (Description, Amount) VALUES ('Dues', 200)
INSERT INTO MyTable (Description, Amount) VALUES ('Dues', 200)
INSERT INTO MyTable (Description, Amount) VALUES ('Dues', 200)
INSERT INTO MyTable (Description, Amount) VALUES ('Dues', 200)
INSERT INTO MyTable (Description, Amount) VALUES ('Dues', 200)
INSERT INTO MyTable (Description, Amount) VALUES ('Donation', 50)
INSERT INTO MyTable (Description, Amount) VALUES ('Donation', 100)
INSERT INTO MyTable (Description, Amount) VALUES ('Donation', 150)
INSERT INTO MyTable (Description, Amount) VALUES ('Donation', 200)
INSERT INTO MyTable (Description, Amount) VALUES ('Donation', 250)
INSERT INTO MyTable (Description, Amount) VALUES ('Donation', 300)
INSERT INTO MyTable (Description, Amount) VALUES ('Others', 100)
INSERT INTO MyTable (Description, Amount) VALUES ('Others', 100)
INSERT INTO MyTable (Description, Amount) VALUES ('Others', 100)
INSERT INTO MyTable (Description, Amount) VALUES ('Others', 100)
INSERT INTO MyTable (Description, Amount) VALUES ('Interest', 50)
INSERT INTO MyTable (Description, Amount) VALUES ('Tax', 8)

SELECT z.Description, z.Amount
FROM 
    (SELECT 1 AS Ranking, Description, SUM(Amount) AS Amount
    FROM MyTable
    WHERE Description <> 'Tax'
    GROUP BY Description
    UNION
    SELECT 2 AS Ranking, 'Less Tax' AS Description, -SUM(Amount) AS Amount
    FROM MyTable
    WHERE Description = 'Tax'
    UNION
    SELECT 3 AS Ranking, 'Net' AS Description, 
        SUM(CASE Description WHEN 'Tax' THEN -Amount ELSE Amount END) AS Amount
    FROM MyTable) z
ORDER BY z.Ranking, z.Description

DROP TABLE MyTable

Open in new window

0
 

Author Closing Comment

by:JimiJ13
ID: 40242001
Good and helpful solutions!
0
 

Author Comment

by:JimiJ13
ID: 40250214
How can I add grouping as follows:
Group    Description     Amount

A               Donation               1050.00
A               Dues               1600.00
B               Interest               50.00
B               Others               400.00
B               Less Tax              -8.00

Thanks.

Net      3092.00
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to use odbc in vb to connect to ms sql 14 53
SQL Server - Sum Decimal Columns 11 33
sql 2014,  lock limit 5 32
MS SQL + Insert Into Table - If Doesnt Exist 9 35
Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

778 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