Quantcast
Channel: Forum Getting started with SQL Server
Viewing all articles
Browse latest Browse all 7129

Compare field value in one table to all field values in another table

$
0
0

Hi everyone,

I have two views, both containing account numbers (field is called ACCT_NBR in both views)

VIEW_1 contains several thousand records, and VIEW_2 contains only 15.

What I would like to do is create a case in my VIEW_1 query that compares the ACCT_NBR of each of those several thousand records to the 15 ACCT_NBR records in VIEW_2, and if there is a match I would like to take a field value from the matching record in VIEW_2 (called PARENT_NBR) and put it into a new field in VIEW_1 called ACCT_PARENT_NBR.

This is what I have so far, but it doesn't seem to work.

CASE
WHEN dbo.VIEW_1.ACCT_NBR = (select ACCT_NBR from dbo.VIEW_2) THEN  (select PARENT_NBR from dbo.VIEW_2)

END AS ACCT_PARENT_NBR

Thank you


Viewing all articles
Browse latest Browse all 7129

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>