Sunday, February 19, 2012

How to query in same 2 table

my table look like this

ID | Name | Weight | Type
--
1 | A | 5 | out
2 | A | 4 | in
3 | B | 10 | out

i want to query data if same Name have Type = in
Show Name and weight of Type = in
if it have Type = out and don't have Type = in (same Name)
Show Name and Weight of Type = out

like this:

Name | Weight
A | 4
B | NULL

Here it is,

Create Table #data (

[ID] Varchar(100) ,

[Name] Varchar(100) ,

[Weight] int ,

[Type] Varchar(100)

);

Insert Into #data Values('1','A','5','out');

Insert Into #data Values('2','A','4','in');

Insert Into #data Values('3','B','10','out');

select [Out].[Name],[In].[Weight] from #Data [out]

Left Outer Join #Data [in] on [Out].Name = [In].Name and [In].[Type]='in'

Where

[Out].[Type]='Out'

No comments:

Post a Comment