| Author |
Topic |
|
bilencekic
Posting Yak Master
121 Posts |
Posted - 2007-12-09 : 08:05:46
|
| hi,i need a simple select statementstrange it is very simple =)hourly of working on other things i coulnt think about this.Well,aid---oid1 141 152 122 15well i need the record that oaid = 14 and oaid = 15.not 14 or 15 it must be (oaid = 14 and oaid = 15)i need the resultaid1i have some methods but they may slow down server with huge records.MS BLESS US |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-12-09 : 08:17:59
|
will you have more than one record with the same aid and oid ?select aidfrom table1where oid in (14, 15)group by aidhaving count(*) = 2 KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
bilencekic
Posting Yak Master
121 Posts |
Posted - 2007-12-09 : 08:26:23
|
| yep i havebut in my scenerio i thought that in wont work yep i think it is fixed , i will test 10-20 min later with a good test conditions.and no need for count(*) MS BLESS US |
 |
|
|
bilencekic
Posting Yak Master
121 Posts |
Posted - 2007-12-09 : 08:35:07
|
| wellit didint fixed my problem.--my table---46 046 7346 046 046 7646 7946 046 8146 047 047 7547 047 047 7647 7947 047 8147 048 048 7348 048 048 7648 7948 048 8148 0left column is aid and right is oidwhen u use in for ('73','76')it gets the aid that have 73 or 76but i want the result that have oid = 73 and oid = 76and result must be4648not 47 (it has 76 but doesnt have 73)MS BLESS US |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-12-09 : 08:43:26
|
[code]select aidfrom( select aid, oid from yourtable where oid in (73, 76) group by aid, oid) agroup by aidhaving count(*) = 2[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
bilencekic
Posting Yak Master
121 Posts |
Posted - 2007-12-09 : 08:56:34
|
| if u youse in operator you select the wrong resultas you see there47 has 76 but doesnt have 73 so it shouldnt comebut if you use in operator it looks that if record has 76 OR 73and 47 comes. that is wrong.i need just the records which have 76 and 73 in ther oid.MS BLESS US |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-12-09 : 09:03:58
|
[code]DECLARE @sample TABLE( aid int, oid int)INSERT INTO @sampleSELECT 46, 0 UNION ALLSELECT 46, 73 UNION ALLSELECT 46, 0 UNION ALLSELECT 46, 0 UNION ALLSELECT 46, 76 UNION ALLSELECT 46, 79 UNION ALLSELECT 46, 0 UNION ALLSELECT 46, 81 UNION ALLSELECT 46, 0 UNION ALLSELECT 47, 0 UNION ALLSELECT 47, 75 UNION ALLSELECT 47, 0 UNION ALLSELECT 47, 0 UNION ALLSELECT 47, 76 UNION ALLSELECT 47, 79 UNION ALLSELECT 47, 0 UNION ALLSELECT 47, 81 UNION ALLSELECT 47, 0 UNION ALLSELECT 48, 0 UNION ALLSELECT 48, 73 UNION ALLSELECT 48, 0 UNION ALLSELECT 48, 0 UNION ALLSELECT 48, 76 UNION ALLSELECT 48, 79 UNION ALLSELECT 48, 0 UNION ALLSELECT 48, 81 UNION ALLSELECT 48, 0SELECT aidFROM( SELECT aid, oid FROM @sample WHERE oid IN (73, 76) GROUP BY aid, oid) aGROUP BY aidHAVING COUNT(*) = 2/*aid ----------- 46 48 (2 row(s) affected)*/[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
bilencekic
Posting Yak Master
121 Posts |
Posted - 2007-12-09 : 09:13:10
|
| ohhaving count(*) = xx is the number of values i had senti think.that is great =)thx mansorry for letting you the write whole code.MS BLESS US |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-12-10 : 02:10:39
|
or from Tan's exampleSELECT aidFROM @sampleGROUP BY aidHAVING sum(case when oid IN (73, 76) then 1 else 0 end)=2 MadhivananFailing to plan is Planning to fail |
 |
|
|
|