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.
| 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 tables1.TransationMaster code pk, TerritoryName, TerritoryId,2.SchemeTransationMaster schemeTransationCode pk TransationCode fk(Code TransationMaster table) QtySecoundarySales QtySchemeSales AmoutforSecVolume3.CashDiscountTransation CashDiscCode pk TransationCode fk(code TrasationMaster table) SecQtyCashDisc CashDiscQty AmountOfCashDisc i need sum from SchemeTransationMaster and CashDiscountTransation my query is as followselect 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 QtyOfCashDiscfrom(select tm.TerritoryName as TerritoryName, tm.Code as Code from TransationMaster tm where tm.IsDeleted = 0 )as ttmLeft 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.TransationCodeLeft 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 cdtton ttm.Code = cdtt.TransationCodegroup by ttm.TerritoryNamebut 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.63Central Gujarat 2200 1200 815.8853765 24 24 2064South Gujarat 100 80 99.16666 NULL NULL NULLbut right answer is Ahmedabad 34860 20516 80720.063931 619 329 6300.63Central Gujarat 2200 1200 815.8853765 12 12 1032South Gujarat 100 80 99.16666 NULL NULL NULL Boyzs |
 |
|
|
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 tables1.TransationMaster code pk, TerritoryName, TerritoryId,2.SchemeTransationMaster schemeTransationCode pk TransationCode fk(Code TransationMaster table) QtySecoundarySales QtySchemeSales AmoutforSecVolume3.CashDiscountTransation CashDiscCode pk TransationCode fk(code TrasationMaster table) SecQtyCashDisc CashDiscQty AmountOfCashDisc i need sum from SchemeTransationMaster and CashDiscountTransation my query is as followselect 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 QtyOfCashDiscfrom(select tm.TerritoryName as TerritoryName, tm.Code as Code from TransationMaster tm where tm.IsDeleted = 0 )as ttmLeft 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.TransationCodeLeft 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 cdtton ttm.Code = cdtt.TransationCodegroup by ttm.TerritoryNamebut gives me wrong data.. can any one please help me regards this.Thank You
May be above change is required. |
 |
|
|
boyzs
Starting Member
8 Posts |
Posted - 2008-09-09 : 05:50:15
|
Thanks Sunil for the help but the problems is still thereWhen I try to do inner join with two tables TransationMasterCashDiscountTransationGives me the right data..But I am trying to connect three tables which Gives me duplicate datain CashDiscountTransationI had also try to create two different views by using TransationMasterCashDiscountTransationand TransationMasterSchemeTransationMasterTablesand connect this views by left outer joinBut 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 thisThank You |
 |
|
|
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? |
 |
|
|
boyzs
Starting Member
8 Posts |
Posted - 2008-09-09 : 06:47:18
|
sure as I said the tables areTransationMaster 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 followsTerritoryName CodeAhmd 1Ahmd 2Central Guj 3Ahmd 4South guj 5 in table SchemeTransationMasterTransationCode QtySecSales QtySchemeSales AmountOfSchemeVolume 1 100 80 200 1 50 5 100 3 100 50 200 5 100 50 100in 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 asTerrName QtySecSales QSchS Amt SecQtyC QtyOfC AmtAhmd 150 85 300 200 150 300Central Guj 100 50 200 100 100 200South guj 100 50 100 Null Null NullHopes 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 QtyOfCashDiscfrom(select tm.TerritoryName as TerritoryName, tm.Code as Code from TransationMaster tm where tm.IsDeleted = 0 )as ttmLeft 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 sttmon ttm.Code=sttm.TransationCodeLeft 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 cdtton ttm.Code = cdtt.TransationCodegroup by ttm.TerritoryName Thank YouBoyzs |
 |
|
|
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.TransationCodeWHERE T.IsDeleted = 0 |
 |
|
|
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 answerThank you very much to all[/code]Boyzs |
 |
|
|
|
|
|
|
|