SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Query Help Please!


Query Help Please!

Author
Message
Vertigo44
Vertigo44
Right there with Babe
Right there with Babe (736 reputation)Right there with Babe (736 reputation)Right there with Babe (736 reputation)Right there with Babe (736 reputation)Right there with Babe (736 reputation)Right there with Babe (736 reputation)Right there with Babe (736 reputation)Right there with Babe (736 reputation)

Group: General Forum Members
Points: 736 Visits: 840
2 Tables.

Table C
VISIT#, SSN, FACILITY, DOCUMENT NAME

Table H
VISIT#, SSN, DATE


Visit# | SSN | DOCNAME | DATE
1 | 555 | Doc A | 01.01.01
1 | 555 | Doc B | 01.01.01
1 | 555 | Doc C | 01.01.01
2 | 777 | Doc A | 01.01.01
2 | 777 | Doc D | 01.01.01
2 | 777 | Doc C | 01.01.01
3 | 999 | Doc A | 01.01.01
3 | 999 | Doc B | 01.01.01
3 | 999 | Doc C | 01.01.01
4 | 555 | Doc A | 01.02.01
4 | 555 | Doc D | 01.02.01
4 | 555 | Doc C | 01.02.01
5 | 777 | Doc A | 01.03.01
5 | 777 | Doc D | 01.03.01
5 | 777 | Doc C | 01.03.01

I want to figure out which visits are missing Doc B...

So Visit 5 | SSN 777 | 01.03.01 and Visit 4 | SSN 555 | 01.02.01 and Visit 2 | SSN 777 | 01.01.01

I am doing my join like so...

table C Inner Join table H on C.visit# = H.visit#

Thanks for the help!
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)

Group: General Forum Members
Points: 94839 Visits: 38958
Something like this:



select *
from
TableH h
where
exists(select 1 from TableC c where c.[visit#] = h.[visit#] and c.[document name] = 'Doc B');




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Vertigo44
Vertigo44
Right there with Babe
Right there with Babe (736 reputation)Right there with Babe (736 reputation)Right there with Babe (736 reputation)Right there with Babe (736 reputation)Right there with Babe (736 reputation)Right there with Babe (736 reputation)Right there with Babe (736 reputation)Right there with Babe (736 reputation)

Group: General Forum Members
Points: 736 Visits: 840
Lynn,

Wouldn't that give me Visits with or that have doc name B? I'm looking for the ones that do not have a specific document...that are missing a document. You could have it right, I have not run it yet. I would just think I would want it to say not exists or not in vs exists. Am I wrong?
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)

Group: General Forum Members
Points: 94839 Visits: 38958
Vertigo44 (4/3/2013)
Lynn,

Wouldn't that give me Visits with or that have doc name B? I'm looking for the ones that do not have a specific document...that are missing a document. You could have it right, I have not run it yet. I would just think I would want it to say not exists or not in vs exists. Am I wrong?


Yes, you are right. Change the EXISTS to NOT EXISTS and see if that works.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search