Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Query Help


Query Help

Author
Message
yogi123
yogi123
SSC-Enthusiastic
SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)

Group: General Forum Members
Points: 130 Visits: 394
Hello Everyone,

I need one query help


create table #temp1
(
StudentLocalID varchar(9),
EnrollSchoolCode Varchar(3)
)

create table #temp2
(
StudentLocalID varchar(9),
ScheduleSchoolCode varchar(3)
)
insert into #temp1 values ('003682153','305')
insert into #temp1 values('003682153','303')
insert into #temp1 values('003744930','305')
insert into #temp1 values('003744930','332')

insert into #temp2 values('003682153','305')
insert into #temp2 values('003682153','303')
insert into #temp2 values('003744930','305')
insert into #temp2 values('003744930','303')
insert into #temp2 values('003744930','332')





I need to display unmatch ScheduleSchoolCode from EnrollSchoolCode of #temp2 only
so desired output is


StudentLocalID   ScheduleSchoolCode
003744930   303

Jason-299789
Jason-299789
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1132 Visits: 3229
this should work


Select * from #temp2
EXCEPT
Select * from #temp1



_________________________________________________________________________
SSC Guide to Posting and Best Practices
yogi123
yogi123
SSC-Enthusiastic
SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)

Group: General Forum Members
Points: 130 Visits: 394
Jason-299789 (1/2/2013)
this should work


Select * from #temp2
EXCEPT
Select * from #temp1



Thanks for your help

can you please solve another way, so i can implement that in my real logic.
Thanks For Your Help
Jason-299789
Jason-299789
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1132 Visits: 3229
Ok, I didnt realise there was any other logic.

This is a way to do it with a Left Join


Select t2.*
from #temp2 t2
Left Join #temp1 t1 on t2.StudentLocalID=t1.StudentLocalID and t2.ScheduleSchoolCode=t1.EnrollSchoolCode
where
   t1.EnrollSchoolCode is Null




Heres a way with a Not Exists


Select t2.*
from #temp2 t2
Where
NOT EXISTS (Select 1 from #temp1 t1
      Where t1.EnrollSchoolCode=t2.ScheduleSchoolCode
      and t1.StudentLocalID=t2.StudentLocalID)



Either should enable you to add more logic.

_________________________________________________________________________
SSC Guide to Posting and Best Practices
manik_anu
manik_anu
Old Hand
Old Hand (374 reputation)Old Hand (374 reputation)Old Hand (374 reputation)Old Hand (374 reputation)Old Hand (374 reputation)Old Hand (374 reputation)Old Hand (374 reputation)Old Hand (374 reputation)

Group: General Forum Members
Points: 374 Visits: 300
this should work!!!!!
Select * from #temp2
EXCEPT
Select * from #temp1

Manik
You cannot get to the top by sitting on your bottom.
Jason-299789
Jason-299789
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1132 Visits: 3229
manikandanps (1/3/2013)
this should work!!!!!
Select * from #temp2
EXCEPT
Select * from #temp1


I already provided that solution but the poster needs to add more logic so I've provided the additional solutions, which should allow for more logic to be provided.

_________________________________________________________________________
SSC Guide to Posting and Best Practices
yogi123
yogi123
SSC-Enthusiastic
SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)

Group: General Forum Members
Points: 130 Visits: 394
Jason-299789 (1/3/2013)
Ok, I didnt realise there was any other logic.

This is a way to do it with a Left Join


Select t2.*
from #temp2 t2
Left Join #temp1 t1 on t2.StudentLocalID=t1.StudentLocalID and t2.ScheduleSchoolCode=t1.EnrollSchoolCode
where
   t1.EnrollSchoolCode is Null




Heres a way with a Not Exists


Select t2.*
from #temp2 t2
Where
NOT EXISTS (Select 1 from #temp1 t1
      Where t1.EnrollSchoolCode=t2.ScheduleSchoolCode
      and t1.StudentLocalID=t2.StudentLocalID)



Either should enable you to add more logic.


Thank You so Much This works
Awesome
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24157 Visits: 37929
yogi123 (1/2/2013)
Jason-299789 (1/2/2013)
this should work


Select * from #temp2
EXCEPT
Select * from #temp1



Thanks for your help

can you please solve another way, so i can implement that in my real logic.
Thanks For Your Help


Can you explain why this wouldn't work for you?

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