tsql - SQL count records if only type x exists on date or other types if other than type x exists on date -


on sql server 2008 r2 have following table:

  • event_id int
  • people_id int
  • event_date date
  • event_type varchar

event_type may :

  • "encounter"
  • "legal"
  • "food"
  • "job_search"
  • "education"
  • "utilities"
  • "child_care"

i wish count events.

but wish count "encounter" event_type if event_type recorded people_id/event_date combination.

if there event_type people_id/event_date combination, want count event_type s people_id/event_date combination not event_type "encounter".

i wish return count of events, given above conditions, date range year(event_date) = 2013

i've found other solutions using sum(case when ...) x , appears way go here, i'm thrown when combining counting types other "encounter" if "encounter" , other event_type appears same date/person or other event_type appears same date/person without type "encounter".

any advice appreciated!

i think can break query 2 parts 1 'encounter' , 1 others:

select ... event_type='encounter' group ... having count(event_type)=1  union  select count(event_type),... event_type <> 'encounter' group ... having count(event_type)>1 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -