Solved

How can I group column values as per sample?

Posted on 2014-07-22
5
130 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 14

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

744 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

11 Experts available now in Live!

Get 1:1 Help Now