sql server - Multiple Inner Join - SQL -
i have sql request doesn't work...i don't know why because i'm beginning sql...
select cdr.*, p1.*, p2.*, p3.* dbo.t_cdr_appel cdr inner join dbo.pos p1 on cdr.cdr_loc_code = p1.loc_code , cdr.cdr_loc_code 'sc%vi%' , cdr.cdr_type 'i%' inner join dbo.pos p2 on cdr.cdr_last_redir_no = p2.post_type , cdr.cdr_loc_code 'ccn%' , cdr.cdr_type 'i' inner join dbo.pos p3 on cdr.cdr_last_redir_no = p3.backup_number , cdr.cdr_loc_code 'voicemail%' , cdr.cdr_type 'if'
thanks help
your select statement
select cdr.*, p1.*, p2.*, p3.* dbo.t_cdr_appel cdr inner join dbo.pos p1 on cdr.cdr_loc_code = p1.loc_code , cdr.cdr_loc_code 'sc%vi%' , cdr.cdr_type 'i%' inner join dbo.pos p2 on cdr.cdr_last_redir_no = p2.post_type , cdr.cdr_loc_code 'ccn%' , cdr.cdr_type 'i' inner join dbo.pos p3 on cdr.cdr_last_redir_no = p3.backup_number , cdr.cdr_loc_code 'voicemail%' , cdr.cdr_type 'if'
is same as
select cdr.*, p1.*, p2.*, p3.* dbo.t_cdr_appel cdr inner join dbo.pos p1 on cdr.cdr_loc_code = p1.loc_code inner join dbo.pos p2 on cdr.cdr_last_redir_no = p2.post_type inner join dbo.pos p3 on cdr.cdr_last_redir_no = p3.backup_number cdr.cdr_loc_code 'sc%vi%' , cdr.cdr_loc_code 'ccn%' , cdr.cdr_loc_code 'voicemail%' , cdr.cdr_type 'i%' , cdr.cdr_type 'i' , cdr.cdr_type 'if'
so, problem where
clause many combination of and
conditions same columns cdr.cdr_loc_code
, cdr.cdr_type
. try replacing and
or
instead.
update: since p1,p2,p3
of same table, avoid duplicate columns, try this
select cdr.*, p.* ( select *, (case when cdr_type 'i%' cdr_loc_code when cdr_type 'i' cdr_last_redir_no when cdr_type 'if' cdr_last_redir_no else '' end) tmp_col dbo.t_cdr_appel ) cdr inner join dbo.pos p on (case when cdr.cdr_type 'i%' p.loc_code when cdr.cdr_type 'i' p.post_type when cdr.cdr_type 'if' p.backup_number else '' end) = cdr.tmp_col (cdr.cdr_loc_code 'sc%vi%' , cdr.cdr_type 'i%') or (cdr.cdr_loc_code 'ccn%' , cdr.cdr_type 'i') or (cdr.cdr_loc_code 'voicemail%' , cdr.cdr_type 'if')
note: running query above might take while large tables. may want add conditions where
clause further tuning. should put where
clause sub query since cdr_loc_code
, cdr_type
of same table dbo.t_cdr_appel
. check null or empty dynamic column tmp_col
in order avoid joining tables on '' = ''
or on null = null
.
select cdr.*, p.* ( select *, (case when cdr_type 'i%' cdr_loc_code when cdr_type 'i' cdr_last_redir_no when cdr_type 'if' cdr_last_redir_no else '' end) tmp_col dbo.t_cdr_appel (cdr_loc_code 'sc%vi%' , cdr_type 'i%') or (cdr_loc_code 'ccn%' , cdr_type 'i') or (cdr_loc_code 'voicemail%' , cdr_type 'if') ) cdr inner join dbo.pos p on (case when cdr.cdr_type 'i%' p.loc_code when cdr.cdr_type 'i' p.post_type when cdr.cdr_type 'if' p.backup_number else '' end) = cdr.tmp_col , nullif(cdr.tmp_col, '') not null
Comments
Post a Comment