Solved

How can I group column values as per sample?

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
IN with @variable 5 25
Syntax using Declare 4 38
email the result out from a T-SQL queries 29 63
Getting same value for every field in SQL 2 29
     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 …
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

920 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

16 Experts available now in Live!

Get 1:1 Help Now