Solved

Join within same table+sql server 2005

Posted on 2011-03-21
11
195 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mysql db 3 95
What Is an Error? 2 59
php hashing methods 3 50
WordPress  Failed to Import Media 8 65
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ

739 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