tsql - SQL COUNT() / LEFT JOIN? -


i have 3 tables: calls, attachments , notes , want display that's in calls table, display whether call has attachments , whether call has notes. - determining if there attachment or note record call_id in it. there notes , attachments, or there may not need know.

tables structure:

calls:

call_id  |  title  |  description   

attachments:

attach_id  |  attach_name  |  call_id   

notes:

note_id  |  note_text  |  call_id   

if write:

select c.call_id      , title      , description      , count(attach_id)  calls c  left join attachments on c.call_id = a.call_id  group c.call_id        , title        , description 

to give me list of calls , number of attachments.

how can add in column number of notes or column indicates there notes?

any ideas?

thanks.

for count

select       c.call_id,       title,       description,       count(distinct attach_id) attachment_count ,       count(distinct note_id)  notes_count  calls c  left join attachments on c.call_id = a.call_id  left join notes n on n.call_id = c.call_id  group c.call_id,title,description 

or existence (will more efficient if need)

select       c.call_id,       title,       description,       count(attach_id) attachment_count ,       case         when exists (select * notes n n.call_id = c.call_id)             cast(1 bit)         else             cast(0 bit)     end notes_exist calls c  left join attachments on c.call_id = a.call_id  group c.call_id,title,description 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -