Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Need help to find solution

Author  Topic 

rdev.theo
Starting Member

5 Posts

Posted - 2011-07-12 : 15:01:39
I am trying to get an out put where I would be comparing 2 fields across 2 tables based on a common field value, I need help to build a query where the result displays if 2 rows with same field value as true and if the comparison fails then I would need to to print as false
eg

Table 1 Table 2
Pimekey Sample1 Pimekey Sample2
ADK12 Read2 ADK12 Read2
ADK12 Read4 ADK12 Read3
ADK14 Read1 ADK14 Read1
ADK14 Read3 ADK14 Read2

I need to display
PrimeKey Sample1 Sample2 Result
ADK12 Read2 Read2 TRUE
ADK12 Read4 Read3 FALSE
ADK14 Read1 Read1 TRUE
ADK14 Read3 Read2 FALSE

Please suggest

Thanks


jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2011-07-12 : 15:21:17
SELECT
t1.PrimeKey as t1PrimeKey
,t1.Sample1 as t1Sample1
,t2.Sample2 as t2Sample2
,case when t2.Sample2 = t1.Sample1 then 'true' else 'false' end as Result

from table1 t1 inner join table2 t2 on t1.primekey = t2.primekey

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

rdev.theo
Starting Member

5 Posts

Posted - 2011-07-12 : 17:50:06
Thanks Jim


Ram Dev
Go to Top of Page

rdev.theo
Starting Member

5 Posts

Posted - 2011-07-12 : 17:54:02
I just had one more question, what if table 1 was to be derived from joining another 2 different tables with a common field between them?

Ram Dev
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2011-07-12 : 18:18:26
replace table1 with your derived query and all should be fine

from (derived table)t1 inner join table2 t2 on t1.primekey = t2.primekey

Jim



Everyday I learn something that somebody else already knew
Go to Top of Page
   

- Advertisement -