Solved

VB.net SQL Joining two tables based on three criteria

Posted on 2014-09-26
4
172 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
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

863 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

24 Experts available now in Live!

Get 1:1 Help Now