Solved

Join within same table+sql server 2005

Posted on 2011-03-21
11
188 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 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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
Comment Utility
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 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
you have to join on ID, not on rulename.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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
Comment Utility
Hi,
In the example provided  Field'id' is in database Field'rulename'.
Id was just given as example.
0
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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
Comment Utility
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 142

Expert Comment

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

Author Comment

by:RIAS
Comment Utility
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 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
Comment Utility
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
Comment Utility
Thanks a million mate!!!Works like a charm !!
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now