Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 sum and total

Author  Topic 

inbs
Aged Yak Warrior

860 Posts

Posted - 2009-09-29 : 10:02:12
i have table:

Month Year Amount Customer
1 2009 100 X
1 2009 200 X
2 2009 100 X
2 2009 200 X
3 2009 100 X
3 2009 200 X

1 2009 200 Y
2 2009 200 Y
3 2009 200 Y

how can i write query and see this result:
              Month
1 2 3
Customer X 100 100 100
Customer X 200 200 200
TOTAL x 300 300 300
Customer Y 200 200 200
TOTAL Y 300 300 300
TOTATL ALL 400 400 400

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-09-29 : 10:19:22
It should be done in front end application
Otherwise read about ROLLUP or CUBE operator in sql server help file

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

inbs
Aged Yak Warrior

860 Posts

Posted - 2009-09-29 : 10:40:45
we couldnt success do it in end application
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-09-30 : 02:03:51
quote:
Originally posted by inbs

we couldnt success do it in end application



What do you mena by this?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-09-30 : 02:09:36
eeeeeeee how come month and year separated...


Hope can help...but advise to wait pros with confirmation...
Go to Top of Page

inbs
Aged Yak Warrior

860 Posts

Posted - 2009-09-30 : 02:24:50
i change my question

i have this table

Year  Month  Customer    Amount
2009 1 A1 1
2009 1 A2 2
2009 1 A3 3
2009 1 B1 4
2009 1 B2 5
2009 1 B3 6


i need this result (by month and year)
SubTotal By A
SubTotal By B
TOTAl A+B

Year  Month  Customer    Amount SUBTOTAL  TOTAL
2009 1 A1 1 6 21
2009 1 A2 2 6 21
2009 1 A3 3 6 21

2009 1 B1 4 15 21
2009 1 B2 5 15 21
2009 1 B3 6 15 21
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-09-30 : 02:25:43
quote:
Originally posted by inbs

my question is how i make subtotal for x and for y


Did you read my first reply?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-09-30 : 02:35:59
madhi...how u do
1 2 3
Customer X 100 100 100
Customer X 200 200 200



Hope can help...but advise to wait pros with confirmation...
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-09-30 : 02:54:03
to your original question

DECLARE @sample TABLE
(
[MONTH] int,
[YEAR] int,
Amount int,
Customer varchar(10)
)
INSERT INTO @sample
SELECT 1, 2009, 100, 'X' UNION ALL
SELECT 1, 2009, 200, 'X' UNION ALL
SELECT 2, 2009, 100, 'X' UNION ALL
SELECT 2, 2009, 200, 'X' UNION ALL
SELECT 3, 2009, 100, 'X' UNION ALL
SELECT 3, 2009, 200, 'X' UNION ALL
SELECT 1, 2009, 200, 'Y' UNION ALL
SELECT 2, 2009, 200, 'Y' UNION ALL
SELECT 3, 2009, 200, 'Y'

SELECT Customer, [YEAR], [1], [2], [3]
FROM (
SELECT Customer, [YEAR], [MONTH], Amount,
dense_rank = row_number() OVER (PARTITION BY Customer, [MONTH] ORDER BY [YEAR])
FROM @sample

UNION ALL

SELECT Customer + ' TOTAL', [YEAR], [MONTH], Amount = SUM(Amount),
dense_rank = 1
FROM @sample
GROUP BY Customer, [YEAR], [MONTH]


UNION ALL

SELECT 'TOTAL ALL', [YEAR], [MONTH], Amount = SUM(Amount),
dense_rank = 1
FROM @sample
GROUP BY Customer, [YEAR], [MONTH]
) d
pivot
(
SUM(Amount)
FOR [MONTH] IN ([1], [2], [3])
) p
ORDER BY CASE WHEN Customer = 'TOTAL ALL' THEN 2 ELSE 1 END, Customer

/*
Customer YEAR 1 2 3
---------------- ----------- ----------- ----------- -----------
X 2009 100 100 100
X 2009 200 200 200
X TOTAL 2009 300 300 300
Y 2009 200 200 200
Y TOTAL 2009 200 200 200
TOTAL ALL 2009 500 500 500

(6 row(s) affected)
*/




KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-09-30 : 02:54:18
to your revised question


DECLARE @sample TABLE
(
[YEAR] int,
[MONTH] int,
Customer varchar(10),
Amount int
)
INSERT INTO @sample
SELECT 2009, 1, 'A', 1 UNION ALL
SELECT 2009, 1, 'A', 2 UNION ALL
SELECT 2009, 1, 'A', 3 UNION ALL
SELECT 2009, 1, 'B', 4 UNION ALL
SELECT 2009, 1, 'B', 5 UNION ALL
SELECT 2009, 1, 'B', 6

SELECT s.[YEAR], s.[MONTH], s.Customer, s.Amount,
SubTotal = SUM(Amount) OVER (PARTITION BY s.Customer),
Total = SUM(Amount) OVER ()
FROM @sample s

/*
YEAR MONTH Customer Amount SubTotal Total
----------- ----------- ---------- ----------- ----------- -----------
2009 1 A 1 6 21
2009 1 A 2 6 21
2009 1 A 3 6 21
2009 1 B 4 15 21
2009 1 B 5 15 21
2009 1 B 6 15 21

(6 row(s) affected)
*/




KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-09-30 : 02:55:14
[code]SELECT yearly, monthly, customer, amount, (SELECT SUM(amount) FROM @fun f1 WHERE f1.customer=f.customer)subtotal, total
FROM @fun f
CROSS APPLY
(SELECT SUM(amount) partition by customer, SUM(amount) total
FROM @fun)f2
ORDER BY CUSTOMER[/code]


Hope can help...but advise to wait pros with confirmation...
Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-09-30 : 02:56:11
.... sifu =.=


Hope can help...but advise to wait pros with confirmation...
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-09-30 : 02:56:11
i assumed your customer should be A and B and not A1, A2 etc else the SUBTOTAL and TOTAL does not make sense


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-09-30 : 02:58:44
quote:
Originally posted by waterduck

.... sifu =.=


Hope can help...but advise to wait pros with confirmation...




sorry beat you to it




KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-09-30 : 03:01:28
you can also use 2 CROSS APPLY to do that

SELECT s.[YEAR], s.[MONTH], s.Customer, s.Amount,
SubTotal, Total
FROM @sample s
CROSS APPLY
(
SELECT SubTotal = SUM(Amount)
FROM @sample x
WHERE x.Customer = s.Customer
) st
CROSS APPLY
(
SELECT Total = SUM(Amount)
FROM @sample x
) t




KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-09-30 : 03:04:04
sifu..where to find the cute icon!!!


Hope can help...but advise to wait pros with confirmation...
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-09-30 : 03:06:09
click on "Reply with Quote" and you will be able to see that


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

inbs
Aged Yak Warrior

860 Posts

Posted - 2009-09-30 : 03:49:27
i success with my way,but it run a long time
how can i optimize it?

SELECT v.Customer,v.Month,v.Year,v.OAmount,v.TarAmount,
--SubTotalOAmount
CASE WHEN v.Customer LIKE '%A%'
THEN (SELECT SUM(Amount) FROM TABLE_V WHERE Month=v.Month AND Year=v.Year AND Customer LIKE '%A%')
WHEN v.Customer LIKE '%B%'
THEN (SELECT SUM(OAmount) FROM TABLE_V WHERE Month=v.Month AND Year=v.Year AND Customer LIKE '%B%')
ELSE OAmount END AS SubTotalOAmount,
--SubTotalTarAmount
CASE WHEN v.Customer LIKE '%A%'
THEN (SELECT SUM(TarAmount) FROM TABLE_V WHERE Month=v.Month AND Year=v.Year AND Customer LIKE '%A%')
WHEN v.Customer LIKE '%B%'
THEN (SELECT SUM(TarAmount) FROM TABLE_V WHERE Month=v.Month AND Year=v.Year AND Customer LIKE '%B%')
ELSE TarAmount END AS SubTotalTarAmount
FROM TABLE_V v
GROUP BY v.Customer,v.Month,v.Year,v.OAmount,v.TarAmount
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-09-30 : 09:33:57
use the CROSS APPLY method that i posted at http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=133613#522011


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -