?
Solved

How can I group column values as per sample?

Posted on 2014-07-22
5
Medium Priority
?
152 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
[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
5 Comments
 
LVL 15

Accepted Solution

by:
Vikas Garg earned 750 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 93

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 750 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

762 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