Consider the DB :
Account table

With the records :

Clients table

with the records :

I want to get all the clients' names & id , that are in Overdraft organized by client ID , so :
SELECT c.Client_id_number , a.Account_number , c.firstName_client , c.lastName_Client , a.Account_balance
FROM Clients AS c
INNER JOIN Accounts AS a
ON a.Client_id_number = c.Client_id_number
WHERE a.Account_balance < 0
GROUP BY c.Client_id_number;
But this returned

Meaning the client
1234
with account 84
, and balance -4000
doesn't appear ... where did I go wrong ?
UPDATE: Firstly, I should make clear the following, you dont need to use the "group by" because you are not summarizing, so your query should be:
SELECT c.Client_id_number , a.Account_number , c.firstName_client , c.lastName_Client , a.Account_balance
FROM Clients AS c
INNER JOIN Accounts AS a
ON a.Client_id_number = c.Client_id_number
WHERE a.Account_balance < 0
I noticed that you have problems with the "group by" concept, so it is important that you know that you need to group by each field within your "SELECT" excepting by those where you are summarizing (c.Client_id_number , a.Account_number)
so, i.e.
SELECT c.Client_id_number , a.Account_number, sum(a.Account_balance) as balance
FROM Clients AS c
INNER JOIN Accounts AS a
ON a.Client_id_number = c.Client_id_number
WHERE a.Account_balance < 0
GROUP BY c.Client_id_number, a.Account_number;
0 comments:
Post a Comment