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 |
|
vital
Starting Member
12 Posts |
Posted - 2008-05-16 : 08:16:47
|
| hi there i use the delphi and as database firebird.but i have a problem with query.i have 3 tables they are master and details,table1->table2->table3table1 idprnametable2 table2idtable1idprptable3table3idtable2idprpiecesthere are their fields i use this query but this doesnt give me which result i wantselect table2.id ,table2.prp-sum(table3.prpieces) from table2 left outer join table3 on table2.id=table3.idthis query give me this resulttable2id sum-------- ----1 42 90 and now i wantto join in this query the table1 too i want get this result table1id table2id sum-------- -------- ---- x 1 4 c 2 90 this rule is a problem for me the table3 must not has the recordsif i use this query it wont give me true resultbecause the table3 maybe doesnt has the records thats why it wont give the true result.how i need to use or which query i need to use ?select table1.prpname,table1.id table2.prp-sum(table3.prpieces) from table1 left outer join table2 on table1.id=table2.table1id and table2.id=table3.table2.idthanks |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-16 : 11:13:20
|
| What will be relationship b/w table1 and table2? Will it be always one to one? |
 |
|
|
vital
Starting Member
12 Posts |
Posted - 2008-05-17 : 02:30:39
|
| table1 table2 table3------ ------- -------id table1id table2idurnad sipadet id id geladetyes they are one to one .these tables in relationship with master detail.table1 table2 table3 there will tobe a table id = table1id table2there are foreignkeys .it will to be one to one .maybe on the table 3 wont to be record but it must give to me as result if there is record on the table2i mean if there wont to be record on the table3 then that query wont give me the true result because of this table2id=table3id.it must give to me all record on the table1 but if there wont to be a result on the table2 then it cant to be onthe table3 either. how i can join all tablesthanks. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-17 : 03:09:42
|
quote: Originally posted by vital table1 table2 table3------ ------- -------id table1id table2idurnad sipadet id id geladetyes they are one to one .these tables in relationship with master detail.table1 table2 table3 there will tobe a table id = table1id table2there are foreignkeys .it will to be one to one .maybe on the table 3 wont to be record but it must give to me as result if there is record on the table2i mean if there wont to be record on the table3 then that query wont give me the true result because of this table2id=table3id.it must give to me all record on the table1 but if there wont to be a result on the table2 then it cant to be onthe table3 either. how i can join all tablesthanks.
SELECT t1.id,t2.table2id,t2.prp-isnull(sum(t3.prppieces),0)FROM table1 t1LEFT JOIN table2 t2ON t2.table1id=t1.idLEFT JOIN table3 t3ON t3.table2id=t2.table2idGROUP BY t1.id,t2.table2id,t2.prp |
 |
|
|
vital
Starting Member
12 Posts |
Posted - 2008-05-17 : 16:09:44
|
| thank you very much visakh16but if the table3 wont tobe one to one also i meantable1 is in relation ship with table2 one to one table2 is in relationship with table3 but the table2 and table3 is not one to one also like thistable1 ------ 3(table1.id) table2 ------- 3(table1.id) 1(table2.id)table3------1(table2.id)1(table2.id)1(table2.id)10(table3.id)if the table1 and table2 are one to one and table3isnot one to one with table2 then how wil tobe query?thanks a lot. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-17 : 16:15:40
|
quote: Originally posted by vital thank you very much visakh16but if the table3 wont tobe one to one also i meantable1 is in relation ship with table2 one to one table2 is in relationship with table3 but the table2 and table3 is not one to one also like thistable1 ------ 3(table1.id) table2 ------- 3(table1.id) 1(table2.id)table3------1(table2.id)1(table2.id)1(table2.id)10(table3.id)if the table1 and table2 are one to one and table3isnot one to one with table2 then how wil tobe query?thanks a lot.
No problem if table2 table3 relation is not one to one as we are grouping on table1 and table2 fields so that you will get a single result and we are also using left join which returns the records even if not in table3. |
 |
|
|
vital
Starting Member
12 Posts |
Posted - 2008-05-20 : 04:16:25
|
| yes visakh16it gives me which result i want .thanks |
 |
|
|
|
|
|