Solved

How can I group column values as per sample?

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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
tempdb log keep growing 7 54
SQL - Curser to do an insert based on a select 2 28
SQL Select Query help 1 46
Please explain Equi-join 3 40
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 …
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

740 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