| Author |
Topic |
|
marekpracz
Starting Member
7 Posts |
Posted - 2009-05-28 : 10:01:39
|
| Hi,Let say I've got table Meeing_id,ticket_id1 , 111 , 221 , 332 , 222 , 333 , 113 , 334 , 33I want to select all meetings_id's that doesn't have ticket 11 but have 33.What is best way to do that ?If I use (where ticked_id <> 11) it will exclude only single row with that Id not all meeting_id's.ThanksMarco |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-05-28 : 10:03:11
|
[code]select distinct meeting_idfrom table twhere t.ticket_id = 33and not exists ( select * from table x where x.meeting_id = t.meeting_id and x.ticket_id = 11 )[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-05-28 : 10:12:34
|
| orselect Meeting_id from tablegroup by Meeting_idhaving max(case when ticket_id=33 then 1 when ticket_id=11 then 2 else 1 end)=1MadhivananFailing to plan is Planning to fail |
 |
|
|
marekpracz
Starting Member
7 Posts |
Posted - 2009-05-28 : 10:14:32
|
| Work excellentThanks a lot ! |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-05-28 : 10:20:19
|
| My code can be simplified toselect Meeting_id from tablegroup by Meeting_idhaving max(case when ticket_id=11 then 2 else 1 end)=1MadhivananFailing to plan is Planning to fail |
 |
|
|
marekpracz
Starting Member
7 Posts |
Posted - 2009-05-28 : 10:31:10
|
| What if I would complicate thinks a bit and divide data into two tables Meeting and Meeting_detail ?So meeting table would contain meeting_id and table meeting_detail would contain both meeting_id and ticked_id and of course meeting_detail_id ? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-05-28 : 11:27:45
|
extending Madhi's logicSELECT m.*FROM meeting mINNER JOIN (SELECT meeting_id FROM meeting_detail GROUP BY meeting_id HAVING max(case when ticket_id=11 then 2 else 1 end)=1 )mdON md.meeting_id=m.meeting_id |
 |
|
|
marekpracz
Starting Member
7 Posts |
Posted - 2009-05-29 : 04:15:42
|
| visakh16 - thanks for SQl but it is not working as it shold.It does show all meetings where ticket 11 is not in it - but there is another condition that it must have ticket 33. Instead above query display all even those meetings that have only ticket 22 ! |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-05-29 : 04:44:51
|
[code]DECLARE @Sample TABLE ( MeetingID INT, TicketID INT )INSERT @SampleSELECT 1, 11 UNION ALLSELECT 1, 22 UNION ALLSELECT 1, 33 UNION ALLSELECT 2, 22 UNION ALLSELECT 2, 33 UNION ALLSELECT 3, 11 UNION ALLSELECT 3, 33 UNION ALLSELECT 5, 22 UNION ALLSELECT 4, 33SELECT MeetingIDFROM @SampleGROUP BY MeetingIDHAVING MAX(CASE TicketID WHEN 11 THEN 2 WHEN 33 THEN 1 ELSE 0 END) = 1[/code] E 12°55'05.63"N 56°04'39.26" |
 |
|
|
|