Solved

SQL Find Customers who have not paid 100

Posted on 2014-09-23
6
117 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:Scott Pletcher
ID: 40339265
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
ID: 40339269
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
ID: 40339270
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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

Author Comment

by:murbro
ID: 40339315
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
ID: 40339324
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
ID: 40339784
Thanks for the help
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SELECT query on two levels (detail and summary) 13 47
MS SQL with ODBC 5 34
SqlServer no dupes 25 34
Querying data from 3 SQL tables 2 31
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

785 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