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 |
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2010-11-19 : 12:18:50
|
I thought if I use a right join then the table to the right will return all records if there is a match or not. This query isn’t giving me that. What am I doing wrong?select n.doc, isnull(count(t.cossn),0) as PendingInFOandDDSfrom t16pendall t right join natdocfile n on n.doc = t.fo where mft_POSN1_CD in('b','d') or (MFT_POSN1_CD='a' and aged_alien_rsw='y')group by docorder by docThis query produces this:doc PendingInFOandDDS100 1389 1913 1A00 5A38 1B14 1 This is what I want:doc PendingInFOandDDS001 0009 000K 0 00N 0 00U 0100 1389 1913 1 A00 5A38 1B14 1 Here's the table info:CREATE TABLE [dbo].[natdocfile]( [doc] [varchar](3) NOT NULL)insert into natdocfileselect '001' union allselect 'A00' union allselect '389' union allselect 'A38' union allselect '100' union allselect 'B14' union allselect '913' union allselect '009' union allselect '00K' union allselect '00N' union allselect '00U' CREATE TABLE [dbo].[T16pendall]( [COSSN] [char](9) NOT NULL, [FLG_CDT] [datetime] NULL, [AGED_ALIEN_RSW] [char](1) NOT NULL, [MFT_POSN1_CD] [char](1) NOT NULL, [FO] [varchar](4) NOT NULL) ON [PRIMARY]insert into T16pendallselect '268763’, '10/6/2010’, ' ', 'D', '389' union allselect '337629', '10/21/2010’, ' ', 'D', 'A00' union allselect '240312', '10/28/2010’, ' ', 'D', '330' union allselect '406339', '10/4/2010’, ' ', 'D', '442' union allselect '279700', '11/1/2010’, ' ', 'D', '387' union allselect '680031', '10/29/2010’, ' ', 'D', 'A00' union allselect '298582', '10/26/2010’, ' ', 'D', '387' union allselect '074506', '9/23/2010’, ' ', 'D', '329' union allselect '610128', '10/6/2010’, ' ', 'A', '893' union allselect '427337', '6/30/2010’, ' ', 'D', 'B14' union allselect '553084', '10/15/2010’, ' ', 'D', '913' union allselect '587962', '8/30/2010’, ' ', 'D', '100' union allselect '746855', '9/10/2010’, ' ', 'D', 'A38' union allselect '429194', '9/13/2010’, ' ', 'D', 'A00' union allselect '424431', '9/14/2010’, ' ', 'D', 'A00' union allselect '150993', '9/15/2010’, ' ', 'D', 'A00' |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2010-11-19 : 12:27:18
|
| Can't rference the optional table in the where clause. The values will be null and it will turn into an inner joinselect n.doc, isnull(count(t.cossn),0) as PendingInFOandDDSfrom t16pendall t right join natdocfile n on n.doc = t.fo and (mft_POSN1_CD in('b','d') or (MFT_POSN1_CD='a' and aged_alien_rsw='y'))group by docorder by doc==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2010-11-19 : 12:38:09
|
| And I never use right join - I find it confusing and stick to left join.Not saying you're wrong just not like me (which is also wrong :) ).==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2010-11-19 : 13:29:05
|
| Thanks so much nigelrivett for the solution and visakh16 for the information! |
 |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2010-11-19 : 14:03:43
|
Back again...I want to add this to the query:and cossn is not = cossin in T16pendmvtThe below query works but want to make sure the query is correct. select n.doc, count(t.cossn) as PendingInFO1from t16pendall tright join natdocfile n on n.doc = t.fo and mft_POSN1_CD in('b','d') or (MFT_POSN1_CD='a' and aged_alien_rsw='y')and t.cossn not in(select cossn from t16pendmvt)group by docorder by docHere's the table info for table t16pendmvtCREATE TABLE [dbo].[T16pendmvt]( [COSSN] [char](6) NOT NULL, [MVT_TYP] [char](1) NOT NULL, [MVT_LOC] [char](3) NOT NULL, [MVT_DEST] [varchar](3) NOT NULL, [MVT_CDT] [datetime] NULL) ON [PRIMARY]insert into T16pendmvtselect '074506', 'T', '329', 'S36', '9/23/2009' union allselect '610128', 'R', 'S15', ' ', '10/6/2010' union allselect '427337', 'R', 'S27', ' ', '7/1/2010' union allselect '553084', 'R', 'V64', ' ', '10/26/2010' union allselect '999962', 'T', 'SS2', 'R42', '2/3/2003' union allselect '452262', 'R', 'VS2', ' ', '1/22/2004' union allselect '458962', 'R', 'V36', ' ', '5/5/2004' union allselect '458962', 'R', 'V25', '858', '6/8/2004' union allselect '458962', 'T', 'S72', 'S24', '7/19/2004' union allselect '458962', 'R', '004', ' ', '4/8/2010' |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2010-11-19 : 14:45:14
|
| should be fine==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2010-11-19 : 14:52:46
|
| Thanks! I have a question... how come I don't need the Where clause:select n.doc, isnull(count(t.cossn),0) as PendingInFOandDDSfrom t16pendall t right join natdocfile n on n.doc = t.fo where mft_POSN1_CD in('b','d') or (MFT_POSN1_CD='a' and aged_alien_rsw='y')group by docorder by docinstead you put "and" I'm a bit confused. |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2010-11-19 : 15:07:08
|
| Because it's included in the join.You can put anything in the join that is in the where clause - it only makes a diference to the result with outer joins.That doesn't apply to all databases - some only allow columns in the join clause and not literals.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2010-11-19 : 15:09:24
|
| Okay thanks I understand! |
 |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2010-11-19 : 15:10:11
|
| Thanks Lamprey I will read this and what Visakh16 sent as well! |
 |
|
|
russell
Pyro-ma-ni-yak
5072 Posts |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2010-11-19 : 15:25:16
|
quote: Originally posted by nigelrivett Because it's included in the join.You can put anything in the join that is in the where clause - it only makes a diference to the result with outer joins.
Mostly True. With SQL you need to be careful because it does not honor predicates placed in the join condition that apply to the determinant table on an outer join. (edit: clarification on the join type). |
 |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2010-11-19 : 15:27:35
|
| Thanks Russell! |
 |
|
|
|
|
|
|
|