Solved

VB.net SQL Joining two tables based on three criteria

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that undeā€¦
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

830 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