1

I have 2 table invm and invt

invm has

i_b_no , i_b_dt , freight , tot_amt
1001 , 04/01/2015, 805 , 5000
1002 , 04/01/2015, 205 , 300

invt has

i_b_no , item_code, item_desc , stax_rate , amount
 1001 , xyz     , adlasjdlja,  12.5, 1000
 1001  , abc     , dfjsdffsdf,  12.5, 1000
 1001  , lkm     , sfkgsfksdn,  10.5, 1000
 1001  , rmn     , yeryoueoqe,  5.00, 1000
 1001  , the     , qwelskjfsdf,  12.5, 1000
1002   , dad     , sdfkdsffsdf , 10.0, 100
1002   , dekw    , kdasdahdafa , 3.2 , 100
1002    , dasdna , kdfnasldakdf, 6.8 , 100

now I want sum ( (freight) * max(staxrate) /100 )

which is (805* 12.5)/100 = 1006.25 (205 * 10)/100 = 20.5

ans should be 1026.75

I used

select sum (( sum (INVM.FREIGHT ) *  MAX (INVT.STAX_RATE) /100 ))
from invm inner join INVT on INVM.I_B_NO = INVT.I_B_NO 
where 
invm.i_b_dt between '04/01/2015 00:00:00' and '04/01/2015 00:00:00' 
group by INVM.FREIGHT 

gives me error

Msg 130, Level 15, State 1, Line 1 Cannot perform an aggregate function on an expression containing an aggregate or a subquery

.

i am stucked in this please help

0

1 Answer 1

2

Do the JOIN in a derived table with the GROUP BY and MAX:

select sum (FREIGHT * STAX_RATE / 100)
from (select INVM.FREIGHT as FREIGHT , MAX(INVT.STAX_RATE) as STAX_RATE
      from invm inner join INVT on INVM.I_B_NO = INVT.I_B_NO
      where invm.i_b_dt between '04/01/2015 00:00:00' and '04/01/2015 00:00:00'
      group by INVM.FREIGHT)
1
  • hey thanks buddy the secound querry worked for me sorry i can't give + point to u as i am new bee :( May 15, 2015 at 7:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.