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 |
|
Dev@nlkss
134 Posts |
Posted - 2009-05-07 : 03:23:49
|
| Hi following is my sample dataID MID Status1 1 False2 1 False3 1 False4 2 False5 2 True6 2 FalseI have to pick only those MIDs where all Status of that MID should be False.My output should be as MID--- 1ThankQ.$atya.Love All Serve All. |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-05-07 : 03:30:57
|
[code]DECLARE @TABLE TABLE( ID int, MID int, Status varchar(10))INSERT INTO @TABLESELECT 1, 1, 'False' UNION ALLSELECT 2, 1, 'False' UNION ALLSELECT 3, 1, 'False' UNION ALLSELECT 4, 2, 'False' UNION ALLSELECT 5, 2, 'True' UNION ALLSELECT 6, 2, 'False'SELECT MIDFROM @TABLEGROUP BY MIDHAVING COUNT(MID) = SUM(CASE WHEN Status = 'False' THEN 1 ELSE 0 END)SELECT MIDFROM @TABLEGROUP BY MIDHAVING COUNT(MID) = COUNT(CASE WHEN Status = 'False' THEN Status END)SELECT MIDFROM @TABLEGROUP BY MIDHAVING MAX(Status) = 'False'[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
Dev@nlkss
134 Posts |
Posted - 2009-05-07 : 03:37:45
|
| thanks its working,but Status is bit type.where last query gives error.$atya.Love All Serve All. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-05-07 : 04:14:16
|
it is because my sample table definition for status is varchar. Just change it accordinglySELECT MIDFROM @TABLEGROUP BY MIDHAVING MAX(convert(int, Status)) = 0 KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-05-07 : 05:04:43
|
quote: Originally posted by khtan
DECLARE @TABLE TABLE( ID int, MID int, Status varchar(10))INSERT INTO @TABLESELECT 1, 1, 'False' UNION ALLSELECT 2, 1, 'False' UNION ALLSELECT 3, 1, 'False' UNION ALLSELECT 4, 2, 'False' UNION ALLSELECT 5, 2, 'True' UNION ALLSELECT 6, 2, 'False'SELECT MIDFROM @TABLEGROUP BY MIDHAVING COUNT(MID) = SUM(CASE WHEN Status = 'False' THEN 1 ELSE 0 END)SELECT MIDFROM @TABLEGROUP BY MIDHAVING COUNT(MID) = COUNT(CASE WHEN Status = 'False' THEN Status END)SELECT MIDFROM @TABLEGROUP BY MIDHAVING MAX(Status) = 'False' KH[spoiler]Time is always against us[/spoiler]
I dont prefer method 2 as it would give you Warning: Null value is eliminated by an aggregate or other SET operation.MadhivananFailing to plan is Planning to fail |
 |
|
|
mailsomani
Starting Member
4 Posts |
Posted - 2009-05-07 : 05:27:32
|
| Hi,Let the table name be TEST with the following columns like ID,MID,STATUS.If status is false then value is 0 else 1.<Based on the above assumption please try below query>select x.mid from (SELECT MID FROM TEST GROUP BY MID,STATUS )xwhere x.mid in(select mid from test where status=0)group by x.mid having count(x.mid)=1RegardsPawan SomaniWINWIRE TECHNOLOGIESPawansomani |
 |
|
|
|
|
|
|
|