I'm learning SQL Server 2000. I have two tables that I need to join, table
A and table B. The result set is a little tricky though. Table A has a set
of columns that are duplicated in table B. The reason is if there is no
data in these columns in table A, then that means that the data "defaults"
to the same named columns in table B. There is a many-to-one relationship
from A to B. What I would like to do is build this join query such that I
would return X number of columns using alias column names. I would like the
query to be able to populate those alias columns with the column values from
table A if there is data in those columns, but if there is no data then
populate those alias columns with the column values from table B. So in
essence, I have something like this:
Table A
ID
B_ID
A_1
A_2
Table B
ID
B_1
B_2
I'd like to build a query that joins these tables on (A.B_ID = B.ID), and
return these alias columns:
COL_1: This retuns data from A_1 if data exists in this column, otherwise
returns data from B_1.
COL_2: This retuns data from A_2 if data exists in this column, otherwise
returns data from B_2.
Any help would be much appreciated.
Thanks!do your join and use the following in your select
select coalesce(A_1,B_1) as COL_1,coalesce(A_2,B_2) as COL_2
from ......
> COL_2: This retuns data from A_2 if data exists in this column, otherwise
> returns data from B_2.
"epigram" <nospam@.spammy.com> wrote in message
news:1111783245. cf22b6774ccf116a3d34ad39ea96b967@.bubbane
ws...
> I'm learning SQL Server 2000. I have two tables that I need to join,
> table A and table B. The result set is a little tricky though. Table A
> has a set of columns that are duplicated in table B. The reason is if
> there is no data in these columns in table A, then that means that the
> data "defaults" to the same named columns in table B. There is a
> many-to-one relationship from A to B. What I would like to do is build
> this join query such that I would return X number of columns using alias
> column names. I would like the query to be able to populate those alias
> columns with the column values from table A if there is data in those
> columns, but if there is no data then populate those alias columns with
> the column values from table B. So in essence, I have something like
> this:
> Table A
> ID
> B_ID
> A_1
> A_2
> Table B
> ID
> B_1
> B_2
> I'd like to build a query that joins these tables on (A.B_ID = B.ID), and
> return these alias columns:
> COL_1: This retuns data from A_1 if data exists in this column, otherwise
> returns data from B_1.
> COL_2: This retuns data from A_2 if data exists in this column, otherwise
> returns data from B_2.
> Any help would be much appreciated.
> Thanks!
>|||but using a LEFT OUTER JOIN.
AMB
"Denis" wrote:
> do your join and use the following in your select
> select coalesce(A_1,B_1) as COL_1,coalesce(A_2,B_2) as COL_2
> from ......
>
>
> "epigram" <nospam@.spammy.com> wrote in message
> news:1111783245. cf22b6774ccf116a3d34ad39ea96b967@.bubbane
ws...
>
>
No comments:
Post a Comment