Solved

VB.net SQL Joining two tables based on three criteria

Posted on 2014-09-26
4
182 Views
Last Modified: 2014-09-26
Hi

I have two tables [Bank] and [Payments]
They both have three matching columns: [DATE], [LINK ID] and [AMOUNT]
I need to pull a query where all three of these columns have the same data
and then a query where this is not the case

What SQL code would I use to achieve these two scenarios?
0
Comment
Question by:murbro
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 167 total points
ID: 40345684
Select [DATE], [LINK ID], [AMOUNT]
FROM [Bank]

INTERSECT

Select [DATE], [LINK ID], [AMOUNT]
FROM [Payments]

Open in new window


and

Select [DATE], [LINK ID], [AMOUNT]
FROM [Bank]

EXCEPT

Select [DATE], [LINK ID], [AMOUNT]
FROM [Payments]

Open in new window

0
 
LVL 1

Assisted Solution

by:nebb-tgr
nebb-tgr earned 167 total points
ID: 40345693
SELECT * FROM Bank AS A 
WHERE
EXISTS (
SELECT * FROM Payments WHERE Date = A.Date AND [LINK ID] = A.[LINK ID] AND Amount = A.Amount
)

Open in new window


SELECT * FROM Bank AS A 
WHERE
NOT EXISTS (
SELECT * FROM Payments WHERE Date = A.Date AND [LINK ID] = A.[LINK ID] AND Amount = A.Amount
)

Open in new window

0
 
LVL 48

Assisted Solution

by:PortletPaul
PortletPaul earned 166 total points
ID: 40345733
Mmmm, I'd normally expect a one (Bank) to many (Payments)

So, for something completely different:

SELECT
      Bank.DATE
    , Bank.[LINK ID]
    , Bank.AMOUNT
    , SUM(Payments.AMOUNT)
FROM Bank
INNER JOIN Payments ON Bank.DATE = Payments.DATE and Bank.[LINK ID] = Payments.[LINK ID]
GROUP BY
      Bank.DATE
    , Bank.[LINK ID]
    , Bank.AMOUNT
;

Open in new window

SELECT
      Bank.DATE
    , Bank.[LINK ID]
    , Bank.AMOUNT
FROM Bank
LEFT JOIN Payments ON Bank.DATE = Payments.DATE and Bank.[LINK ID] = Payments.[LINK ID]
WHERE
      Payments.DATE IS NULL
;

Open in new window

0
 

Author Closing Comment

by:murbro
ID: 40345939
Thank you all for such comprehensive answers. Pity I can't allocate full points to a each of you
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

737 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