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 |
|
gfaryd
Starting Member
27 Posts |
Posted - 2010-09-13 : 15:48:44
|
| Dear alli have following DDL and sample data i m using sql 2000CREATE TABLE [data_table] ( [m_id] [varchar] (15) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [group_id] [char] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [model_value] [varchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , CONSTRAINT [PK_data_table] PRIMARY KEY CLUSTERED ( [m_id], [group_id] ) ON [PRIMARY] ) ON [PRIMARY]GOCREATE TABLE [work_table] ( [m_id] [varchar] (15) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [group_id] [char] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [data_value] [varchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,) ON [PRIMARY]GOinsert into data_table values('A001','1','AB900')insert into data_table values('A001','2','AB100')insert into data_table values('A001','3','AB2000')insert into data_table values('A001','4','AB700')insert into data_table values('A002','1','CF1000')insert into data_table values('A002','3','CF4000')insert into data_table values('A002','4','CF9000')insert into data_table values('A003','3','DE4000')insert into data_table values('A003','4','DE9000')insert into data_table values('A004','2','GT4000')insert into data_table values('A004','3','GT9000')insert into work_table values('A001','1','AB900')insert into work_table values('A001','2','AB100')insert into work_table values('A001','3','AB2000')insert into work_table values('A001','4','AB700')insert into work_table values('A002','3','CF4000')insert into work_table values('A002','4','CF9000')insert into work_table values('A003','2','DE4000')insert into work_table values('A003','3','DE9000')insert into work_table values('A004','3','GT4000')insert into work_table values('A004','4','GT9000')i m running following queryselect a.*,b.* from data_table a left join work_table bon a.m_id = b.m_id and a.group_id = b.group_idwhere a.group_id = 1 and b.m_id is not nullunionselect a.*,b.* from data_table a left join work_table bon a.m_id = b.m_id and a.group_id = b.group_idwhere a.group_id = 2 and b.m_id is not nullunionselect a.*,b.* from data_table a left join work_table bon a.m_id = b.m_id and a.group_id = b.group_idwhere a.group_id = 3 and b.m_id is not nullunionselect a.*,b.* from data_table a left join work_table bon a.m_id = b.m_id and a.group_id = b.group_idwhere a.group_id = 4 and b.m_id is not nullthe query is giving me resultA001 1 AB900 A001 1 AB900A001 2 AB100 A001 2 AB100A001 3 AB2000 A001 3 AB2000A001 4 AB700 A001 4 AB700A002 3 CF4000 A002 3 CF4000A002 4 CF9000 A002 4 CF9000A003 3 DE4000 A003 3 DE9000A004 3 GT9000 A004 3 GT4000but i want out put likeA001 1 AB900 A001 1 AB900A002 3 CF4000 A002 3 CF4000A003 3 DE4000 A003 3 DE9000A004 3 GT9000 A004 3 GT4000i only want one group id in a m_id like in original resultset if group_id 1 is avaialable for m_id A001 in both tables then group_id's 2,3,4 should not be returned in result set and same applies for all data. Regards, |
|
|
Devart
Posting Yak Master
102 Posts |
Posted - 2010-09-14 : 02:42:55
|
| Hello,For example:create view [dbo].[data_work_view]asselect a.m_id,a.group_id,a.model_value,b.data_value from data_table a left join work_table bon a.m_id = b.m_id and a.group_id = b.group_idwhere a.group_id = 1 and b.m_id is not nullunionselect a.m_id,a.group_id,a.model_value,b.data_value from data_table a left join work_table bon a.m_id = b.m_id and a.group_id = b.group_idwhere a.group_id = 2 and b.m_id is not nullunionselect a.m_id,a.group_id,a.model_value,b.data_value from data_table a left join work_table bon a.m_id = b.m_id and a.group_id = b.group_idwhere a.group_id = 3 and b.m_id is not nullunionselect a.m_id,a.group_id,a.model_value,b.data_value from data_table a left join work_table bon a.m_id = b.m_id and a.group_id = b.group_idwhere a.group_id = 4 and b.m_id is not nullGOselect m_id, group_id, model_value, data_valuefrom dbo.data_work_view dwvwhere (select count(*) from data_work_view where m_id=dwv.m_id and group_id<=dwv.group_id)=1Best regards,Devart,SQL Server Tools:dbForge Schema ComparedbForge Data ComparedbForge Query Builder |
 |
|
|
PavanKK
Starting Member
32 Posts |
Posted - 2010-09-14 : 03:11:46
|
hi gfaryd,try the following queryselect a.*,b.*from data_table ajoin work_table b on a.m_id = b.m_id and a.group_id = b.group_idWHERE a.group_id =(SELECT top 1 c.group_idFROM work_table c JOIN data_table d on c.m_id = d.m_id and c.group_id = d.group_id and c.m_id = a.m_idorder by c.group_id)KK |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-09-16 : 12:55:59
|
| [code]select * into #Tempfrom(select a.*,b.* from data_table a left join work_table bon a.m_id = b.m_id and a.group_id = b.group_idwhere a.group_id = 1 and b.m_id is not nullunionselect a.*,b.* from data_table a left join work_table bon a.m_id = b.m_id and a.group_id = b.group_idwhere a.group_id = 2 and b.m_id is not nullunionselect a.*,b.* from data_table a left join work_table bon a.m_id = b.m_id and a.group_id = b.group_idwhere a.group_id = 3 and b.m_id is not nullunionselect a.*,b.* from data_table a left join work_table bon a.m_id = b.m_id and a.group_id = b.group_idwhere a.group_id = 4 and b.m_id is not null)tselect t1.*from #Temp t1inner join (select m_id,MIN(group_id) AS MinGroup from #Temp group by m_id) t2on t2.m_id = t1.m_idand t2.MinGroup = t1.group_id[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|