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
 General SQL Server Forums
 New to SQL Server Programming
 how to use sum from three tables

Author  Topic 

boyzs
Starting Member

8 Posts

Posted - 2008-09-09 : 04:14:53
Dear Friends,
I am new to to sql server. I have three different tables
1.TransationMaster
code pk,
TerritoryName,
TerritoryId,
2.SchemeTransationMaster
schemeTransationCode pk
TransationCode fk(Code TransationMaster table)
QtySecoundarySales
QtySchemeSales
AmoutforSecVolume
3.CashDiscountTransation
CashDiscCode pk
TransationCode fk(code TrasationMaster table)
SecQtyCashDisc
CashDiscQty
AmountOfCashDisc

i need sum from SchemeTransationMaster and CashDiscountTransation

my query is as follow


select ttm.TerritoryName as TerritoryName,
Sum(sttm.QtySecSales) as QtySecSales,
sum(sttm.QtySchemeSales) as QtySchemeSales,
sum(sttm.AmountOfSchemeVolume) as AmountOfSchemeVolume,
sum(cdtt.SecQtyOfCashDisc) as SecQtyOfCashDisc ,
sum(cdtt.QtyOfCashDisc) as QtyOfCashDisc,
sum(cdtt.TotalAmtOfCashDisc) as QtyOfCashDisc
from(select tm.TerritoryName as TerritoryName,
tm.Code as Code
from TransationMaster tm
where tm.IsDeleted = 0
)as ttm
Left Outer join
(select
stm.TransationCode as TransationCode,
stm.QtySecSales as QtySecSales,
stm.QtySchemeSales as QtySchemeSales,
stm.AmountOfSchemeVolume as AmountOfSchemeVolume
from SchemeTransationMaster stm
where stm.IsDeleted = 0

)as sttm
on ttm.Code=sttm.TransationCode
Left Outer Join
(
select
cdt.TransationCode as TransationCode,
cdt.SecQtyOfCashDisc as SecQtyOfCashDisc,
cdt.QtyOfCashDisc as QtyOfCashDisc,
cdt.TotalAmtOfCashDisc as TotalAmtOfCashDisc
from CashDiscountTransation cdt
where cdt.IsDeleted = 0
)as cdtt
on ttm.Code = cdtt.TransationCode
group by ttm.TerritoryName


but gives me wrong data.. can any one please help me regards this.

Thank You

boyzs
Starting Member

8 Posts

Posted - 2008-09-09 : 05:00:34
the above query gives me result like this



Ahmedabad 34860 20516 80720.063931 619 329 6300.63
Central Gujarat 2200 1200 815.8853765 24 24 2064
South Gujarat 100 80 99.16666 NULL NULL NULL


but right answer is

Ahmedabad 34860 20516 80720.063931 619 329 6300.63
Central Gujarat 2200 1200 815.8853765 12 12 1032
South Gujarat 100 80 99.16666 NULL NULL NULL


Boyzs
Go to Top of Page

sunil
Constraint Violating Yak Guru

282 Posts

Posted - 2008-09-09 : 05:24:44
quote:
Originally posted by boyzs

Dear Friends,
I am new to to sql server. I have three different tables
1.TransationMaster
code pk,
TerritoryName,
TerritoryId,
2.SchemeTransationMaster
schemeTransationCode pk
TransationCode fk(Code TransationMaster table)
QtySecoundarySales
QtySchemeSales
AmoutforSecVolume
3.CashDiscountTransation
CashDiscCode pk
TransationCode fk(code TrasationMaster table)
SecQtyCashDisc
CashDiscQty
AmountOfCashDisc

i need sum from SchemeTransationMaster and CashDiscountTransation

my query is as follow


select ttm.TerritoryName as TerritoryName,
Sum(sttm.QtySecSales) as QtySecSales,
sum(sttm.QtySchemeSales) as QtySchemeSales,
sum(sttm.AmountOfSchemeVolume) as AmountOfSchemeVolume,
sum(cdtt.SecQtyOfCashDisc) as SecQtyOfCashDisc ,
sum(cdtt.QtyOfCashDisc) as QtyOfCashDisc,
sum(cdtt.TotalAmtOfCashDisc) as QtyOfCashDisc
from(select tm.TerritoryName as TerritoryName,
tm.Code as Code
from TransationMaster tm
where tm.IsDeleted = 0
)as ttm
Left Outer join
(select
stm.TransationCode as TransationCode,
stm.QtySecSales as QtySecSales,
stm.QtySchemeSales as QtySchemeSales,
stm.AmountOfSchemeVolume as AmountOfSchemeVolume
from SchemeTransationMaster stm
where stm.IsDeleted = 0

)as sttm
on ttm.Code=sttm.TransationCode
Left Outer Join
(
select DISTINCT cdt.TransationCode as TransationCode,
cdt.SecQtyOfCashDisc as SecQtyOfCashDisc,
cdt.QtyOfCashDisc as QtyOfCashDisc,
cdt.TotalAmtOfCashDisc as TotalAmtOfCashDisc
from CashDiscountTransation cdt
where cdt.IsDeleted = 0
)as cdtt
on ttm.Code = cdtt.TransationCode
group by ttm.TerritoryName


but gives me wrong data.. can any one please help me regards this.

Thank You



May be above change is required.
Go to Top of Page

boyzs
Starting Member

8 Posts

Posted - 2008-09-09 : 05:50:15
Thanks Sunil for the help but the problems is still there

When I try to do inner join with two tables

TransationMaster
CashDiscountTransation

Gives me the right data..

But I am trying to connect three tables which Gives me duplicate data
in CashDiscountTransation

I had also try to create two different views by using

TransationMaster
CashDiscountTransation

and

TransationMaster
SchemeTransationMaster

Tables

and connect this views by left outer join
But still getting same result..

from last two days I am trying to resolved this query but my luck is not to good..

Is there any other alternative to get sum from three tables??

please guy's help me regards this

Thank You
Go to Top of Page

sunil
Constraint Violating Yak Guru

282 Posts

Posted - 2008-09-09 : 06:10:29
Can you provide some sample data ,table structures and expected output?
Go to Top of Page

boyzs
Starting Member

8 Posts

Posted - 2008-09-09 : 06:47:18
sure

as I said the tables are
TransationMaster
Code (Primary Key)(guid)
TerritoryName (Varchar(max))
SchemeTransationMaster
TransationCode (Fk---> pk to TransationMaster.Code )
QtySecSales(float)
QtySchemeSales(float)
AmountOfSchemeVolume(float)
CashDiscountTransation
TransationCode (Fk---> pk to TransationMaster.Code )
SecQtyOfCashDisc (float)
QtyOfCashDisc (float)
TotalAmtOfCashDisc(float)

In table TransationMaster datas are as follows

TerritoryName Code
Ahmd 1
Ahmd 2
Central Guj 3
Ahmd 4
South guj 5

in table SchemeTransationMaster

TransationCode QtySecSales QtySchemeSales AmountOfSchemeVolume
1 100 80 200
1 50 5 100
3 100 50 200
5 100 50 100

in table CashDiscountTransation
TransationCode SecQtyOfCashDisc QtyOfCashDisc TotalAmtOfCashDisc
1 100 50 100
4 50 10 20
3 100 100 200


this are the data now i want output as
TerrName QtySecSales QSchS Amt SecQtyC QtyOfC Amt
Ahmd 150 85 300 200 150 300
Central Guj 100 50 200 100 100 200
South guj 100 50 100 Null Null Null


Hopes now you get the my problem...


Query I use is nearer to my answer but does not get the correct answer...

select ttm.TerritoryName as TerritoryName,
Sum(sttm.QtySecSales) as QtySecSales,
sum(sttm.QtySchemeSales) as QtySchemeSales,
sum(sttm.AmountOfSchemeVolume) as AmountOfSchemeVolume,
sum(cdtt.SecQtyOfCashDisc) as SecQtyOfCashDisc ,
sum(cdtt.QtyOfCashDisc) as QtyOfCashDisc,
sum(cdtt.TotalAmtOfCashDisc) as QtyOfCashDisc
from(select tm.TerritoryName as TerritoryName,
tm.Code as Code
from TransationMaster tm
where tm.IsDeleted = 0
)as ttm
Left Outer join
(select
stm.TransationCode as TransationCode,
stm.QtySecSales as QtySecSales,
stm.QtySchemeSales as QtySchemeSales,
stm.AmountOfSchemeVolume as AmountOfSchemeVolume
from SchemeTransationMaster stm
where stm.IsDeleted = 0 )as sttm
on ttm.Code=sttm.TransationCode
Left Outer Join
(
select
cdt.TransationCode as TransationCode,
cdt.SecQtyOfCashDisc as SecQtyOfCashDisc,
cdt.QtyOfCashDisc as QtyOfCashDisc,
cdt.TotalAmtOfCashDisc as TotalAmtOfCashDisc
from CashDiscountTransation cdt
where cdt.IsDeleted = 0
)as cdtt
on ttm.Code = cdtt.TransationCode
group by ttm.TerritoryName



Thank You

Boyzs
Go to Top of Page

Ifor
Aged Yak Warrior

700 Posts

Posted - 2008-09-09 : 07:30:02
Maybe something like:
SELECT T.TerritoryName
,D1.*
,D2.*
FROM TransationMaster T
LEFT JOIN
(
SELECT
S.TransationCode
,SUM(S.QtySecSales) AS QtySecSales
,SUM(S.QtySchemeSales) AS QtySchemeSales
,SUM(S.AmountOfSchemeVolume) AS AmountOfSchemeVolume
FROM SchemeTransationMaster S
WHERE S.IsDeleted = 0
GROUP BY S.TransationCode
) D1
ON T.Code = D1.TransationCode
LEFT JOIN
(
SELECT C.TransationCode
,SUM(C.SecQtyOfCashDisc) AS SecQtyOfCashDisc
,SUM(C.QtyOfCashDisc) AS QtyOfCashDisc
,SUM(C.TotalAmtOfCashDisc) AS TotalAmtOfCashDisc
FROM CashDiscountTransation C
WHERE C.IsDeleted = 0
GROUP BY C.TransationCode
) D2
ON T.Code = D2.TransationCode
WHERE T.IsDeleted = 0
Go to Top of Page

boyzs
Starting Member

8 Posts

Posted - 2008-09-09 : 08:13:16
[code]
Thanks Ifor

Problem as been resolved by yr query... I appreciate to your answer
Thank you very much to all

[/code]

Boyzs
Go to Top of Page
   

- Advertisement -