?
Solved

VB.net SQL Joining two tables based on three criteria

Posted on 2014-09-26
4
Medium Priority
?
185 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:Murray Brown
[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 668 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 668 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 49

Assisted Solution

by:PortletPaul
PortletPaul earned 664 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:Murray Brown
ID: 40345939
Thank you all for such comprehensive answers. Pity I can't allocate full points to a each of you
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

765 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