Solved

SQL Find Customers who have not paid 100

Posted on 2014-09-23
6
115 Views
Last Modified: 2014-09-23
Hi
I have a Customers table and a Payments table, shown in that order below
What joined query would I use to find the CustID of those Customers who have
not paid an AMOUNT of 100. The [LINK ID] is the same as the CustID in CUSTOMERS

Customers
Payments
0
Comment
Question by:murbro
6 Comments
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
SELECT c.*
FROM (
    SELECT [Link Id]
    FROM Payments
    GROUP BY p.[Link Id]
    HAVING MAX(CASE WHEN Amount = 100.00 THEN 1 ELSE 0 END) = 0
) AS p
INNER JOIN Customers c ON
    c.CustID = p.[Link Id]
0
 
LVL 65

Expert Comment

by:Jim Horn
Comment Utility
btw, you might have the image labels off, as the set labeled 'Customers' looks like a payments table, and vise versa

Looks like Scott answered it above.

Also, do yourself a favor and lose the spaces in the First Name, Last Name columns, as it forces T-SQL to delineate it using square brackets [ ], which is one thing to potentially forget and throw an error.
0
 
LVL 24

Expert Comment

by:Phillip Burton
Comment Utility
I assume that these two tables are the wrong way round - the first one should be payments and the customers.

Select C.CustID, C.[First Name], C.[Last Name], Sum(Amount) as TotalAmount
From Customers C
LEFT JOIN Payments P
ON C.CustID = P.[LINK ID]
GROUP BY C.CustID, C.[First Name], C.[Last Name]
HAVING Sum(Amount) < 100 OR P.[LINK ID] IS NULL

Open in new window

0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:murbro
Comment Utility
Hi
Yes the two tables are the wrong way around. I was looking for Customers who have not paid the amount of 100 once(not in total).
0
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 500 total points
Comment Utility
Select C.CustID, C.[First Name], C.[Last Name], Max(Amount) as MaxAmount
From Customers C
LEFT JOIN Payments P
ON C.CustID = P.[LINK ID]
WHERE Max(Amount) < 100 OR P.[LINK ID] IS NULL
GROUP BY C.CustID, C.[First Name], C.[Last Name]

Open in new window

0
 

Author Closing Comment

by:murbro
Comment Utility
Thanks for the help
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

763 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

6 Experts available now in Live!

Get 1:1 Help Now