Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a problem to select these Data
SQL
table1

id amount businessVolume Dueno advisor_id
1   500     50            1     1
2   1000    100           2     1
3   500     50            15    1
4   1000    100           3     1
5   500     50            2     2
6   1000    100           3     2
7   500     50            13    2

this is my Table Format,
i have to sum the amount,and bv but there is a condition to sum ( amount) for whole dueno and to sum (business volume) where dueno <=12 group by advisor_id.

im expecting

SQL
amount businessvolume advisor_id
3000    250             1
2000    150             2


any suggestions ?
im going to implement this into my Recursive Query
Posted
Updated 4-Mar-14 1:25am
v2

SQL
SELECT SUM(t1.amount) amount,
(SELECT SUM(t2.businessvolume) FROM table1 t2 WHERE t2.dueno <=12
   AND t1.advisor_id=t2.advisor_id
   GROUP BY t2.advisor_id) businessvolumn,
advisor_id FROM table1 t1 GROUP BY t1.advisor_id
 
Share this answer
 
Comments
King Fisher 4-Mar-14 7:39am    
thanks mr.peter ,i accept it .but is there any other way to do this,is it Possible?
just to know.no intention.
Peter Leow 4-Mar-14 8:10am    
As much as possible, I always go for solution that is more generic, straight forward, and cross-platform. This solution will also work for MySQL. I am sure there are other ways of achieving this such as self join, but then why make thing so complicated.
King Fisher 4-Mar-14 8:16am    
i think its much complicated to merge with recursive query ,so i just look for any new things by selfish.let me try with self join and thanks for your suggestion
Maciej Los 4-Mar-14 17:48pm    
Looks OK, +5!
Please, see my answer ;)
King Fisher 4-Mar-14 23:43pm    
my 5+,thank you so much
Check this:
SQL
DECLARE @tmp TABLE (id INT IDENTITY(1,1), amount INT, businessVolume INT, Dueno INT, advisor_id INT)

INSERT INTO @tmp (amount, businessVolume, Dueno, advisor_id)
VALUES(500, 50, 1, 1), (1000, 100, 2, 1), 
(500, 50, 15, 1), (1000, 100, 3, 1), 
(500, 50, 2, 2), (1000, 100, 3, 2), 
(500, 50, 13, 2)

SELECT t1.advisor_id, (SELECT SUM(amount) FROM @tmp  WHERE advisor_id = t1.advisor_id) AS SumOfAmount, SUM(t1.businessVolume) AS SumOfBissVol
FROM @tmp AS t1
WHERE Dueno <13
GROUP BY advisor_id 


Result:
adv._id Amount  Buss.Vol
1	3000	250
2	2000	150
 
Share this answer
 
Comments
Peter Leow 4-Mar-14 21:09pm    
very comprehensive, +5!
Maciej Los 5-Mar-14 1:54am    
Thank you, Peter ;)
King Fisher 4-Mar-14 23:42pm    
my 5+ ,thanks
Maciej Los 5-Mar-14 1:54am    
Thank you ;)
[Updated]

select t1.advisor_id, t1.businessvolume, t2.amount from 
(select sum(businessvolume) as 'businessvolume', advisor_id
from table1
where Dueno<12
group by advisor_id) t1
inner join 
(select SUM(amount) as 'amount', advisor_id
from table1
group by advisor_id) t2 on t1.advisor_id=t2.advisor_id



I advice you to see here: GROUP BY[^]

and I used inner join here, but you may need to use outer join if t1 returns no rows. see here: Outer Join[^]
 
Share this answer
 
v3
Comments
King Fisher 4-Mar-14 7:23am    
sorry sir,i am not expecting the Business volume for whole dueno.
sum(businessvolume) where dueno <=12 but sum(amount) where whole dueno
Vedat Ozan Oner 4-Mar-14 7:50am    
updated.
King Fisher 4-Mar-14 23:48pm    
my 4.bcoz, its usual one.this was i tried when before i post here and i was just looked for the new ideas.thank you so much for your timing.never mind :)
Maciej Los 4-Mar-14 17:48pm    
Updated answer deserves for 5!
Vedat Ozan Oner 5-Mar-14 1:39am    
thank you, all. trying to help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900