Solved

multiple column subquery

Posted on 2002-05-06
3
4,113 Views
Last Modified: 2010-04-27
Greetings,

Can someone point me to an equivalent statement that will run on both
ms sql server 7 and 2000?

I am trying to select some rows from a table where two column entries
match some other criteria. The Oracle equivalent is at the bottom of
this message... In MS SQL Server Query Analyzer I am getting errors
trying this:

        SELECT table1.col1, table1.col2, table1.col3
        FROM table1
        WHERE (table1.num, table1.code) in
          (SELECT table2.num, table2.code from table2
          where table2.col4 = value


The error is: "Incorrect Syntax near ',' ", so it seems to not like
the comma in "where (table1,num, table2.code) in"

The important thing to remember is that each row in one table will have the same two values as a row in the other table.

.. I did not want to concatenate the rows as it seems to deliver different results.

Thanks for any assistance,
Jeff

select (col1, col2, col3
from table1
where (num, code) in
(select num, code from table2 where col4 = value);

0
Comment
Question by:kishj
3 Comments
 
LVL 3

Accepted Solution

by:
trouta earned 50 total points
Comment Utility
The where exists clause will need to be used if you have multiple values to check for

SELECT table1.col1, table1.col2, table1.col3
FROM table1
WHERE EXISTS (SELECT * FROM TABLE2 WHERE table1.num = table2.num and table1.code = table2.code and table2.col4 = value)

0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
You can also use a join to resolve this, like so:

SELECT table1.col1, table1.col2, table1.col3
    FROM table1
    INNER JOIN table2 ON table1.num = table2.num AND table1.code = table2.code
    WHERE table2.col4 = value

0
 
LVL 1

Expert Comment

by:usmanr
Comment Utility
extending trouta's solution, you can also write it this way,,
select * from Address where exists (select 1 where City = 'Bothell' and AddressLine1 = '1970 Napa Ct.')
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to shrink a transaction log file down to a reasonable size.

743 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

16 Experts available now in Live!

Get 1:1 Help Now