Solved

Join within same table+sql server 2005

Posted on 2011-03-21
11
197 Views
Last Modified: 2012-05-11
Hi,
I have a table with calcrule and id
Data for example is
Table:Calc
Field'Calcrule'   Field'id'
A                       APPLE
A                       BOAT
A                       SKY

B                       SKY
B                       CLOUD
B                       CAR

The requirement is to find the differences between two 'Calrule'
I need to compare calcrule'A' with calcrule'B'  to find differences in 'id' and vice versa

Any suggestions are appreciated
Cheers

0
Comment
Question by:RIAS
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35179579
you start with this:


select a.* , b.*
  from Calc A
  join Calc B
    on A.id = B.ID
    and B.CalcRule = 'B'
  where a.CalcRule = 'A'

Open in new window

0
 

Author Comment

by:RIAS
ID: 35179975
Hi,
Thanks for the reply.But it is not returning any results.

select  a.* , b.*   from calcrules A  join  calcrules B  on A.rulename = B.rulename and B.rulename = 'pie_Val_TVEnhUncapped' where a.rulename = 'etv_Val_TVEnhUncapped'

Any suggestions...

Cheers
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35180000
you have to join on ID, not on rulename.
0
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35180005
to clarify, you tried:

select  a.* , b.*   from calcrules A  join  calcrules B  on A.rulename = B.rulename and B.rulename = 'pie_Val_TVEnhUncapped' where a.rulename = 'etv_Val_TVEnhUncapped'

which is the wrong part put in bold.
0
 

Author Comment

by:RIAS
ID: 35180372
Hi,
In the example provided  Field'id' is in database Field'rulename'.
Id was just given as example.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35180388
that is a problem when you provide "wrong" data samples.

anyhow, please clarify what you want as output from you posted data?
I could imagine at least 3 outputs ... so I don't want to guess
0
 

Author Comment

by:RIAS
ID: 35180398
select  a.* , b.*   from calcrules A  join  calcrules B  on A.calcname = B.calcname and B.calcname= 'pie_Val_TVEnhUncapped' where a.calcname = 'etv_Val_TVEnhUncapped'
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35180416
not sure if we cross-posted....
you posted now an SQL, but what "DATA" do you want as output?
0
 

Author Comment

by:RIAS
ID: 35180484
The output should be the difference in two calc.

Example

Field'Calcname'   Field'calcrule'
A                       APPLE
A                       BOAT
A                       SKY

B                       SKY
B                       CLOUD
B                       CAR

Difference displayed should be Apple and boat when calcname A is compared to calcname B
Difference should be cloud,car when B is compared to A.
What the requirement is to see the difference in the calcrules

Cheers

0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 35180650
excellent.

ok, here we go, comparing 'A' to 'B'
select c.calcrule from Calc c where c.CalcName = 'A' and not exists ( select null from Calc o where o.CalcName = 'B' AND o.calcrule = c.CalcRule ) 

Open in new window

and 'B' to 'A':
select c.calcrule from Calc c where c.CalcName = 'B' and not exists ( select null from Calc o where o.CalcName = 'A' AND o.calcrule = c.CalcRule ) 

Open in new window

0
 

Author Closing Comment

by:RIAS
ID: 35180699
Thanks a million mate!!!Works like a charm !!
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question