Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Join within same table+sql server 2005

Posted on 2011-03-21
11
Medium Priority
?
203 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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 2000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

916 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