I have the following 2 tables (showing only sum of the columns here):
CUSTOMER:id, fname, lname.
SALES_ORDER: id, cust_id, sales_rep.
And the id from CUSTOMER connects with the cust_id from SALES_ORDER.
What I need to do is: Return customer details + number of orders made by that customer, for customers that have had at least one order handled by employee number '129'. This is the query I tried:
SELECT customer.id, fname, lname, count(*)
FROM customer,
sales_order
WHERE customer.id = sales_order.cust_id
GROUP BY customer.id
HAVING sales_rep = 129;
This does indeed return customers that answer the above + the number of order they had...but upon browsing the DB I see there are other clients that weren't fetched by the query. What am I doing wrong?
Thanks!
What about this:
SELECT customer.id, fname, lname, count(*)
FROM customer,
sales_order
WHERE customer.id = sales_order.cust_id
GROUP BY customer.id
HAVING MAX(IIF(sales_rep = 129, 1, 0)) = 1;
The idea is to calculated the count, but show only these records, where at least on of the sales_rep is 129. We are using IIF (you can use CASE WHEN for older SQL Server versions) to check if a particular record is for the specific sales_rep and MAX to check for at least one such record.
If you do not want to use HAVING you can filter the rows using additional filtering criteria:
SELECT customer.id, fname, lname, count(*)
FROM customer
INNER JOIN sales_order
ON customer.id = sales_order.cust_id
WHERE customer.id IN
(
SELECT DISTINCT customer.id
FROM sales_order
WHERE sales_rep = 129
)
GROUP BY customer.id
The idea is to calculated the count for each customer and then using a WHERE clause to display only users you have at least one sales_rep = 129.
Related
Given the following tables:
• Clients (ClientId, Name, Surname, Age)
• Products (ProductId, Name, Price)
• Purchases (Purchaseld, Date, ClientId, Productid)
I need to write an SQL query that shows the quantity of purchases made by clients. It must only show the clients who made more than 1 purchase. The result should contain the following fields: Full name (i.e. "john rambo"), Quantity of purchases.
I have written this query but results are not coming correct
SELECT Concat(clients.name, clients.surname)
FROM clients
JOIN products
ON clients.name = products.name
JOIN purchases
ON products.productid = purchases.productid
GROUP BY clientid
HAVING Count(clientid) > 1
SELECT Concat(clients.name, ' ', clients.surname),
count(*) as number_of_orders
FROM clients
JOIN purchases
ON products.productid = purchases.productid
GROUP BY Concat(clients.name, ' ', clients.surname)
HAVING Count(clientid) > 1
As noted in the comments, your join to products doesn't make much sense - your asking to only return records where there's a product that matches a client's first name.
CONCAT will glue the two fields together (e.g. "JohnRambo")
It must only show the clients who made more than 1 purchase.
Your question has no mention of products, so there is no need for that in the query:
SELECT CONCAT(c.name, c.surname)
FROM clients c JOIN
purchases p
ON p.ClientId = c.ClientId
GROUP BY c.ClientId, CONCAT(c.name, c.surname)
HAVING COUNT(*) > 1 ;
Note that the ClientId presumably uniquely defines the clients -- not the names. So the ClientId should be part of the aggregation.
I just started learning SQL now and trying to figure out this scenario:
We have 3 tables:
Clients (ClientID, Name, Surname, Age)
Products (ProductID, Name, Price)
Purchases (PurchaseID, Date, ClientID, ProductID)
What would be the best SQL query that will show the amount of purchases (total amount per client) made by clients?
It must only show the clients who made more than 1 purchase.
The result should contain the following fields: Full name, Quantity of purchases, Total amount.
I've got this query but it only joins two tables. How do I join the third table (Products.Price) as well and calculate the total amount per client?
SELECT CONCAT(IFNULL(Name,''),' ', IFNULL(Surname,'')) as FullName,
COUNT(purchaseId) as "Quantity of purchases"
FROM Purchases as P
INNER JOIN Clients as C
on P.ClientID = C.ClientID
GROUP BY C.ClientID,Name, Surname
HAVING COUNT(PurchaseId) > 1;
I would recommend that you use CONCAT_WS() to combine the first name. This handles NULL values more elegantly than your solution.
SELECT CONCAT_WS(c.Name, c.Surname) as FullName,
COUNT(*) as num_purchases,
SUM(pr.price) as total_price
FROM Clients c INNER JOIN
Purchases p
ON P.ClientID = C.ClientID INNER JOIN
Products pr
ON pr.ProductID = p.ProductID
GROUP BY CONCAT_WS(c.Name, c.Surname)
HAVING COUNT(DISTINCT p.PurchaseId) > 1;
Note the COUNT(DISTINCT) in the HAVING clause. This ensures that the clients have at least two purchases. If you only want clients with at least two products or purchases, then you can use COUNT(*) -- but your question is about purchases.
You can try below -
SELECT CONCAT(IFNULL(Name,''),' ', IFNULL(Surname,'')) as FullName,
COUNT(purchaseId) as "Quantity of purchases",sum(price) as totalamount
FROM Purchases as P
INNER JOIN Clients as C
on P.ClientID = C.ClientID
inner join Products p1 on p.productid=p1.productid
GROUP BY C.ClientID,Name, Surname
HAVING COUNT(PurchaseId) > 1
I have my first table, where it contains buying information about clients. So if a client buy two objects (TV and Phone), 2 rows are added as following:
Row 1:
ID = 12321, client_name = 'X', total_debts = 560, sale_type = 'TV LG 40'' ', date =..., time =...
Row 2:
ID = 34564, client_name = 'X', total_debts = 700, sale_type = 'iPhone', date =..., time =...
Those are entered in this table called client_debts
Now I have, a second table where it control the paying of each customer. So when Mr. X, came and pays from his total 1260$, just 200$ this month, it will be added to client_details like the following:
id = 8642234, client_id = 34564, client_name = 'X', payment = 200, date_now =..., time_now=...
I don't care on which item he pays his installment, so I added to client_id, the last buying id.
What I want to make is a comparison between total debts of each client, and his total payments. So i tried each query alone:
SELECT client_name, sum(total_debts) FROM alamir_store.client_debts GROUP BY client_name;
And I got this result:
The second query was:
SELECT client_name, sum(payment) FROM client_details GROUP BY client_name;
And the result is:
What I really want is to combine those 2 tables together, so I can see how much we still have from each client, and we want to see if he pays this month on any of his object or not to put him on our NOT PAID LIST
I have tried this query but didn't get me the logic result:
SELECT t1.client_name, sum(total_debts), sum(payment) FROM
client_debts t1 INNER JOIN client_details t2 GROUP BY t1.client_name;
The result is:
Any help is appreciated, to correct this error and how to make a NOT PAID YET THIS MONTH LIST
You can try the query below, but it is advisable you use the client ID for the join:
select td1.client_name,td1.TotalDebt,td2.TotalPayment from (SELECT client_name, sum(total_debts) as TotalDebt FROM client_debts GROUP BY client_name) as td1
inner JOIN
(SELECT client_name, sum(payment) as TotalPayment FROM client_details GROUP BY client_name) as td2
on td1.client_name=td2.client_name
You forgot to check the condition for matching the same client in both the tables. Use the below query
SELECT t1.client_name, sum(total_debts), sum(payment) FROM
client_debts t1 INNER JOIN client_details t2
ON t1.ID=t2.client_id GROUP BY t1.client_name;
I have two tables : Employee and Customer. Customer has customer ID, name, cust state, cust rep# and employee has employee first name, last name, employee phone number, employee number. Employee number = Cust Rep#.
I'm trying to extract employee first name, last name and employee phone number who serve customers that live in CA. This is what I had as a code but i get an error saying it returns more than one row
SELECT EMP_LNAME,
EMP_FNAME,
EMP_PHONE
FROM employee
WHERE EMP_NBR =
(SELECT CUST_REP
FROM customer
WHERE CUST_STATE='CA') ;
What's happening is that your inner query returns multiple rows, so change it to where EMP_NBR in to retrieve all matches.
The problem your query has is that it doesn't make sense to say = (a set which returns multiple rows), since it's unclear what exactly should be matched.
Use IN() instead of = when expecting a set of results to be returned in the subquery (rather than a single scalar result):
select EMP_LNAME,EMP_FNAME,EMP_PHONE
from employee
where EMP_NBR IN (
select CUST_REP from customer where CUST_STATE='CA'
);
Alternatively, you can use an INNER JOIN (or CROSS JOIN with a WHERE filter) to possibly do this more efficiently:
SELECT EMP_LNAME, EMP_FNAME, EMP_PHONE
FROM employee
INNER JOIN customer ON employee.EMP_NBR = customer.CUST_REP
WHERE customer.CUST_STATE = 'CA';
You don't need a nested query - better to write your query like this
select employee.emp_lname, employee.emp_fname, employee.emp_phone
from employee inner join customer
on employee.emp_nbr= customer.cust_rep
where customer.cust_state = 'CA'
You are getting that error message because there are several customers in California. This will not be a problem with the above query.
SELECT EMP_LNAME,
EMP_FNAME,
EMP_PHONE
FROM employee
WHERE EMP_NBR IN
(SELECT CUST_REP
FROM customer
WHERE CUST_STATE='CA') ;
I have three tables customer, customer_account, account_transaction
Table structure is as follow -
Customer
Id,
Branch,
Name
..
customer_account
Id,
Cust_id,
Loanamout,
EMI
account_transaction
ID,
account_id,
amount,
date
I need to get branch wise details in form of count of Loan given, sum of loan given, and sum of emi received for a particular branch. below is my current query -
SELECT
count(s.id) as cntloan,
SUM(s.Loanamout)
(
SELECT SUM(amount)
FROM account_transaction i
WHERE s.id = i.account_id
) AS curbal
From
customer as c,
customer_account as s
where c.branch = 1 and s.cust_id = c.id
It is giving me desired result for count of loan and sum of loan given. but not giving the right sum of EMI paid by customers
Can anyone help me on this.
Thanks you very much
This SQL has aggregative functions such as count and sum, so without a group by, this would not work.
SELECT customer.id,
COUNT(customer_account.id) as cntloadn,
SUM(customer_account.loan) as loan,
SUM(account_transaction.amount)
FROM customer
JOIN customer_account ON customer_account.cust_id = customer.id
JOIN account_transaction ON account_transaction.account_id = customer_account.id
GROUP BY customer.id