SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Calculate Totals for ID numbers that have more than one entry


Calculate Totals for ID numbers that have more than one entry

Author
Message
emmettjarlath
emmettjarlath
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 281
Hi,

I have a table with about 700 rows, 100 ID numbers that have more than one entry. I need to add the amount for each ID number, who have the same date, to get the total amount.

For example:

ID Dated Amount
1 2012-02-03 00:00:00.000 100.00
1 2012-02-03 00:00:00.000 25.00
1 2012-02-03 00:00:00.000 125.00
2 2012-02-04 00:00:00.000 50.00
2 2012-02-04 00:00:00.000 15.00
3 2012-02-07 00:00:00.000 190.00
3 2012-02-07 00:00:00.000 210.00
3 2012-02-07 00:00:00.000 60.00
3 2012-02-07 00:00:00.000 40.00

The result should look like this:

ID Dated Amount
1 2012-02-03 00:00:00.000 250.00
2 2012-02-04 00:00:00.000 65.00
3 2012-02-03 00:00:00.000 500.00


Any suggestions would be a great help.

Thank you in advance,

emmettjarlath
bitbucket-25253
bitbucket-25253
SSCertifiable
SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)

Group: General Forum Members
Points: 7821 Visits: 25280
Try this:

SELECT ID,Dated,SUM(Amount) FROM #T GROUP BY ID,Dated ORDER BY ID 



Of course alter the FROM #T to FROM substitue your tables name

My resutl:
1 2012-02-03 00:00:00.000 250.00
2 2012-02-04 00:00:00.000 65.00
3 2012-02-07 00:00:00.000 500.00

If everything seems to be going well, you have obviously overlooked something.

Ron

Please help us, help you -before posting a question please read

Before posting a performance problem please read
emmettjarlath
emmettjarlath
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 281
Hi bitbucket-25253,

Thanks for your help. Your sugegstion worked very well. All I had to do was alter the code to my requirements and hey presto, SUCCESS!

Unfortunately a new challenge has arised. The table I am pulling the information from has duplicate distinct unit id numbers, for example.

unit_id total
1 155
1 399

I do not want to view both. The unit id numbers I want to view are also in another table I can pull from. I have messed around with a join but to no avail. The unit id number in the other table is actually called analysis_code1.

Is there a way I can define the unit id I want?

Thanks again,
emmettjarlath
bitbucket-25253
bitbucket-25253
SSCertifiable
SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)

Group: General Forum Members
Points: 7821 Visits: 25280
If I understand your question correctly, see if something like this will work for you.
CREATE TABLE T2000(ID Int,Dated DATETIME,Amount DECIMAL(10,2))
INSERT INTO T2000
SELECT 1, '2012-02-03 00:00:00.000', 100.00 UNION ALL
SELECT 1, '2012-02-03 00:00:00.000', 25.00 UNION ALL
SELECT 1, '2012-02-03 00:00:00.000', 125.00 UNION ALL
SELECT 2, '2012-02-04 00:00:00.000', 50.00 UNION ALL
SELECT 2, '2012-02-04 00:00:00.000', 15.00 UNION ALL
SELECT 3, '2012-02-07 00:00:00.000 ',190.00 UNION ALL
SELECT 3, '2012-02-07 00:00:00.000', 210.00 UNION ALL
SELECT 3, '2012-02-07 00:00:00.000', 60.00 UNION ALL
SELECT 3, '2012-02-07 00:00:00.000', 40.00

CREATE TABLE #X(ID Int,Dated DATETIME,Amount DECIMAL(10,2))
INSERT INTO #X
SELECT ID,Dated,SUM(Amount) FROM T2000 GROUP BY ID,Dated ORDER BY ID
SELECT ID,Dated,Amount FROM #X

CREATE TABLE Second(ID INT,Whoops VARCHAR(20))
--My mistake ID should have been analysis_code1
INSERT INTO Second
SELECT 1,'YUP' UNION ALL
SELECT 2,'Maybe' UNION ALL
SELECT 3,'Yes' UNION ALL
SELECT 4,'Nope'

SELECT c.ID,c.Dated,c.Amount,s.Whoops
FROM #X c
JOIN Second s ON
c.ID = s.ID
-- Result:
1 2012-02-03 00:00:00.000 250.00 YUP
2 2012-02-04 00:00:00.000 65.00 Maybe
3 2012-02-07 00:00:00.000 500.00 Yes



If everything seems to be going well, you have obviously overlooked something.

Ron

Please help us, help you -before posting a question please read

Before posting a performance problem please read
emmettjarlath
emmettjarlath
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 281
Hi,

I should have closed this topic off weeks ago. I solved the this issue. I am now able to total my figures from a table on one server and compare the totals to another table on a different server.

Thank you for help, it really pointed me in the right direction.

emmettjarlath
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search