 |
|
 |
|
Next: Search CLOB field
|
| Author |
Message |

Joined: Jun 22, 2005 Posts: 2
|
(Msg. 1) Posted: Fri Jun 24, 2005 10:07 am
Post subject: Query table based on multiple keys
|
|
|
|
| Hey,
I am having some confusion about how to formulate this particular query.
I have 2 tables. Table A has 4 columns say a1,a2,a3,a4 with the columns a1,a2,a4 forming the primary key. Table B again has 3 columns with b1,b2,b3,b4 and like before, b1,b2 and b4 form the primary key. All columns are of the same datatype in both tables. Now I want to get rows from table A which are not present in table B. Whats the best way of doing this?
Thanks
|
>> Stay informed about: Query table based on multiple keys |
|
| Back to top |
|
 |  |
External

Since: Jun 23, 2005 Posts: 2
|
(Msg. 2) Posted: Fri Jun 24, 2005 10:07 am
Post subject: Re: Query table based on multiple keys [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
|
|
| Back to top |
|
 |  |
External

Since: Dec 07, 2004 Posts: 587
|
(Msg. 3) Posted: Fri Jun 24, 2005 12:10 pm
Post subject: Re: Query table based on multiple keys [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
|
|
| Back to top |
|
 |  |
External

Since: Jun 23, 2005 Posts: 2
|
(Msg. 4) Posted: Fri Jun 24, 2005 1:07 pm
Post subject: Re: Query table based on multiple keys [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
--CELKO-- wrote:
> SELECT a1, a2, a3, a4
> FROM A
> WHERE NOT EXISTS
> (SELECT *
> FROM B
> WHERE A.a1 = B.b1
> AND A.a2 = B.b2
> AND A.a3 = B.b3
> AND A.a4*= B.b4 );
Yeah, that works better than my version. I just tested a little more
and realized that mine doesn't actually do what I expected, but I can't
figure out why not.
--Richard<!-- ~MESSAGE_AFTER~ --> >> Stay informed about: Query table based on multiple keys |
|
| Back to top |
|
 |  |
External

Since: May 30, 2004 Posts: 1647
|
(Msg. 5) Posted: Fri Jun 24, 2005 10:05 pm
Post subject: Re: Query table based on multiple keys [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
--CELKO-- (jcelko212@earthlink.net) writes:
> SELECT a1, a2, a3, a4
> FROM A
> WHERE NOT EXISTS
> (SELECT *
> FROM B
> WHERE A.a1 = B.b1
> AND A.a2 = B.b2
> AND A.a3 = B.b3
> AND A.a4*= B.b4 );
What is that *= doing on the last row?
The requirements were somewhat ambiguous, but one of these should do:
SELECT a1, a2, a3, a4
FROM A
WHERE NOT EXISTS
(SELECT *
FROM B
WHERE A.a1 = B.b1
AND A.a2 = B.b2
AND A.a4 = B.b4 );
(Rows identified by keys, the value in the non-key column a3/b3 may
be different.)
SELECT a1, a2, a3, a4
FROM A
WHERE NOT EXISTS
(SELECT *
FROM B
WHERE A.a1 = B.b1
AND A.a2 = B.b2
AND A.a4 = B.b4
AND A.a3 = B.b3 );
(Rows may be in both tables, but may have a difference in a3/b3.)
--
Erland Sommarskog, SQL Server MVP, esquel.TakeThisOut@sommarskog.se
Books Online for SQL Server SP3 at
<a style='text-decoration: underline;' href="http://www.microsoft.com/sql/techinfo/productdoc/2000/books.asp" target="_blank">http://www.microsoft.com/sql/techinfo/productdoc/2000/books.asp</a><!-- ~MESSAGE_AFTER~ --> >> Stay informed about: Query table based on multiple keys |
|
| Back to top |
|
 |  |
External

Since: May 30, 2004 Posts: 1647
|
(Msg. 6) Posted: Fri Jun 24, 2005 10:08 pm
Post subject: Re: Query table based on multiple keys [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
( ) writes:
> I'm no expert, so this probably isn't the most efficient way to do
> this, but I think this will work:
>
> select A.* from A, B
> where A.a1 *= B.b1
> and A.a2 *= B.b2
> and A.a4 *= B.b4
> and B.b1 is null
*= is a older form of outer join which has all sorts of funny
quirkes with it. I am not going to find why this does not work.
Use the new ANSI syntax instead:
select A.*
from A
left join B ON A.a1 = B.b1
and A.a2 = B.b2
and A.a4 = B.b4
where and B.b1 is null
But I much prefer NOT EXISTS for this type of query, as it much better
expresses what you are looking for.
--
Erland Sommarskog, SQL Server MVP, esquel.DeleteThis@sommarskog.se
Books Online for SQL Server SP3 at
<a style='text-decoration: underline;' href="http://www.microsoft.com/sql/techinfo/productdoc/2000/books.asp" target="_blank">http://www.microsoft.com/sql/techinfo/productdoc/2000/books.asp</a><!-- ~MESSAGE_AFTER~ --> >> Stay informed about: Query table based on multiple keys |
|
| Back to top |
|
 |  |
External

Since: Dec 07, 2004 Posts: 587
|
(Msg. 7) Posted: Sat Jun 25, 2005 4:38 am
Post subject: Re: Query table based on multiple keys [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
|
|
| Back to top |
|
 |  |
| Related Topics: | Multiple db query call from within different context into .. - The first query returns me the results from multiple databases, the second does the same thing except it puts the result into a #temp table? Could someone please show me an example of this using the first query? The first query uses the @exec_context and...
simple ALTER Table query question...SQL 2000. - Ok I have a table with fields as follows: field1 varchar(10) field2 varchar(10) field3 varchar(10) Identity int Indenity field I want to create a ALTER table to add fields field2a and field2b right after field2 and not at the end of the table. Any..
How to do a Join Using Foreign Keys in Stored in a Databas.. - I have a database column that stores a comma delimited list of foreign keys. Would someone show me how to do a join using the values from a list stored within a record? For example, a record in tbl_cds.genre_id might have a value of "2, 5, 6"...
Parent/Child Query: return single row from child table - Hello All, I have a parent table ASSET: CREATE TABLE [dbo].[ASSET]( [ASSETID] [int] IDENTITY(1,1) NOT NULL, [ASSET_NAME] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ) ON [PRIMARY] with the following data: INSERT INTO ASSET..
Distinct Row based on description count - Hello everyone. Hope someone can help. I have a table with two columns Material Number and Material Description. Sample data below: Material# Description 10000 This is item 10000 10000 |
|
You can post new topics in this forum You can reply to topics in this forum You can edit your posts in this forum You can delete your posts in this forum You can vote in polls in this forum
|
|
|
|
 |
|
|