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 |
|
artieman1
Starting Member
1 Post |
Posted - 2007-03-08 : 00:25:04
|
| I am attempting to find some way to select multiple rows in a table that will filter out some of the contents. Here goes my explanation.Using the following as the table schema.Resource|GrpNameMultiple Resources that can repeat and Multiple Grp Names that can repeat. The report requestor will select weather or not we are going to select all GrpNames or just a few.What I would like to do is make sure that if 2 or 3 or more group names are selected, we would only list the resources that belong to all selected groups.I can not think of how in the world I would filter on this.select resource from <tablename> where <Selected Group Names ALL exist>If there is a Resource that is selected that is not part of ALL the Groups selected, I do not want to see it. |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-08 : 00:31:43
|
maybe you can post your table structure, some sample data and the expected result. KH |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-03-08 : 00:40:53
|
Is this what you want?declare @t table( resource varchar(50), grpname varchar(50))insert @tselect 'aaa', 'grp1' union allselect 'bbb', 'grp1' union allselect 'ccc', 'grp1' union allselect 'aaa', 'grp2' union allselect 'bbb', 'grp2' union allselect 'ddd', 'grp2'select grpname from @t t1 join (select 'aaa' as x union all select 'bbb' as x union all select 'ccc' as x) t2on t1.resource = t2.xgroup by grpnamehaving count(resource) = 3 Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
jen
Master Smack Fu Yak Hacker
4110 Posts |
Posted - 2007-03-08 : 15:54:54
|
| [code]CREATE FUNCTION dbo.fnSplitText( @RowData varchar(4000), @SplitOn varchar(5)) RETURNS @RtnValue table ( Row int identity(1,1), Data varchar(100)) AS BEGIN Declare @Cnt int Set @Cnt = 1 While (Charindex(@SplitOn,@RowData)>0) Begin Insert Into @RtnValue (data) Select Data = ltrim(rtrim(Substring(@RowData,1,Charindex(@SplitOn,@RowData)-1))) Set @RowData = Substring(@RowData,Charindex(@SplitOn,@RowData)+1,len(@RowData)) Set @Cnt = @Cnt + 1 End Insert Into @RtnValue (data) Select Data = ltrim(rtrim(@RowData)) ReturnENDgodeclare @t table( resource varchar(50), grpname varchar(50))declare @grpname varchar(100)set @grpname='grp1,grp2'insert @tselect 'aaa', 'grp1' union allselect 'bbb', 'grp2' union allselect 'ccc', 'grp3' union allselect 'aaa', 'grp2' union allselect 'bbb', 'grp1' union allselect 'ddd', 'grp2'select * from @tselect resource from(select resource from @twhere grpname in (select data from dbo.fnsplitText(@grpname,',')))sgroup by resourcehaving count(*)=(select count(*) from dbo.fnsplitText(@grpname,','))godrop function dbo.fnSplitTextgo[/code]--------------------keeping it simple... |
 |
|
|
|
|
|
|
|