Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Join within same table+sql server 2005

Posted on 2011-03-21
11
Medium Priority
?
202 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
Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

 
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

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

688 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