I have an users table and amount table.
The users table has following columns.
name id
A 1
B 2
C 3
D 4
The amount table has following columns.
userId amount id
1 10 1
1 20 2
1 10 3
2 12 4
I need a sql which sums all the users amount from the amount table
Final output would be
name id totalAmount
A 1 30
B 2 12
C 3 0
D 4 0
I have tried using but does not work. Kindly help
let searchQuery = `SELECT u.id, u.name, (SELECT IFNULL(SUM(amount), 0) from amount WHERE amount.userId = u.id) as totalAmount, FROM users u LEFT JOIN amount amt on u.id = amt.userId WHERE`;
Correct syntax for correlated subquery version
SELECT u.id, u.name, (SELECT coalesce(SUM(a.amount), 0)
FROM amount a
WHERE a.userId = u.id) as totalAmount
FROM users u
You can do it using standard sql aggregation
select
u.name,
u.id,
sum(a.amount) as totalAmount
from users u
left join amount a
on a.userId = u.id
group by u.name, u.id
The left join is just to include those users whitout amounts, which will have a 0 as totalAmount
Related
I have three tables in a database, as follows:
users:
user_id name pbal
1 m1 100
2 m2 200
3 m3 300
4 m4 400
5 m5 500
payouts:
id user_id amount
1 1 100
2 1 200
3 2 300
4 1 400
blocked:
id user_id status
------ -------- ------
1 2 block
2 3 block
Now I want to make a list that excludes users whose user_id is in the blocked table, and that calculates a new total amount based on the pbal from users and the amount from the related rows in payouts:
name total_amount
----- ------------
m1 800
m5 500
m4 400
I have been trying for half an hour, but failed. Can someone help me?
Here's an example:
SELECT u.name, u.user_id, MAX(u.pbal) + SUM(p.amount) AS total_amount
FROM dbo.Payouts p
INNER JOIN dbo.Users u ON u.user_id = p.user_id
LEFT OUTER JOIN dbo.Blocked b ON u.user_id = b.user_id
WHERE b.user_id IS NULL
GROUP BY u.user_id, u.name
ORDER BY MAX(u.pbal) + SUM(p.amount) DESC;
Here is a possible answer:
SELECT u.name, u.user_id,
u.pbal + (
-- this takes amounts from the payouts table
SELECT SUM(p.amount)
FROM dbo.Payouts p
WHERE u.user_id = p.user_id
) AS total
FROM dbo.Users u
WHERE NOT EXISTS (
-- this excludes blocked users
SELECT *
FROM dbo.Blocked b
WHERE u.user_id = b.user_id
)
ORDER BY total;
I prefer subqueries in this case because they make it a bit more explicit
Take the following data:
db.users
id name
1 David
2 James
3 Mary
db.payments
amount user_id paid
5 1 0
55 1 1
94 1 1
5 2 0
55 2 1
94 2 1
5 2 0
55 2 1
94 2 1
I'd like to LEFT JOIN the payments table to the users table and SUM() the amount where paid = 0:
SELECT users.*, SUM(payments.amount)
FROM users
LEFT JOIN payments
ON users.id = payments.user_id
WHERE payments.paid = 0
GROUP BY users.id
ORDER BY users.id DESC
The problem arises in that 'mary' (user 3) doesn't get returned because she doesn't have any records in the payments table. This is down to the WHERE clause checking for paid = 0.
I'd like it to return 0 as the sum if no records.
Any ideas how to do that?
Move predicate payments.paid = 0 from WHERE to ON clause, like:
SELECT users.*, SUM(payments.amount)
FROM users
LEFT JOIN payments
ON users.id = payments.user_id AND payments.paid = 0
GROUP BY users.id
ORDER BY users.id DESC
If you place the predicate in the WHERE clause LEFT JOIN becomes an INNER JOIN.
You have to put-> sum(isnull(payment.amount,0)) as Payment count in your query
SELECT users.*, SUM(isnull(payments.amount,0))
FROM users
LEFT JOIN payments
ON users.id = payments.user_id
WHERE payments.paid = 0
GROUP BY users.id
ORDER BY users.id DESC
Try this:-
SELECT users.Id, users.name, SUM(case when paid=0 then amount else 0 end) as
req_amt
FROM users
LEFT JOIN payments
ON users.id = payments.user_id
GROUP BY users.id,users.name
ORDER BY users.id DESC
Thanks:-)
There are two tables
users
+--+----+
|id|name|
+--+----+
1 A
2 B
orders
+--+--------+-------+-------+
|id|subtotal|created|user_id|
+--+--------+-------+-------+
1 10 1000001 1
2 20 1000002 1
3 10 1000003 2
4 10 1000005 1
The idea is to get AVG, SUM and the last created order from the users.
SELECT
users.name,
users.phone,
SUM(a.subtotal),
COALESCE(a.created, NULL)
FROM users
LEFT JOIN
(
SELECT
orders.id,
orders.subtotal,
orders.user_id,
orders.created
FROM
orders
JOIN(
SELECT MAX(i.created) created, i.user_id
FROM orders i
GROUP BY i.user_id
)AS j ON(j.user_id = orders.user_id AND orders.created = j.created) GROUP BY orders.user_id
) AS a ON users.id = a.user_id
GROUP BY users.id
For example the SQL request should return this:
+--+----+---+--------+
|id|name|sum|date |
+--+----+---+--------+
1 A 40 1000005
2 B 10 1000003
But the SQL above failed to calculate sum. What did i miss?
Your query seems way too complicated. How about this?
SELECT u.id, u.name, SUM(o.subtotal), MAX(o.created)
FROM users u LEFT JOIN
orders o
ON u.id = o.user_id
GROUP BY u.id, u.name;
In MySQL it is particularly important to avoid unnecessary subqueries in the FROM clause. These are actually materialized and that can impede the use of indexes for performance.
I have two tables: users and works
I need write select query for count different names from users table where work_status = 1 from works table
The total is: 3 John, 1 Tom
I need get result:
John 2 (2 because one John work_status = 0 ant this not counting)
Tom 1
I have write select that can count different names, just need compared work_status..
SELECT name,COUNT(*) as num FROM users GROUP BY name
My query return:
There is a problem in your question. So here you have two solutions.
If there are three different John working on the company, this is your query
SELECT u.name, COUNT(*) as num
FROM users u INNER JOIN works w ON w.user_id = u.id
WHERE w.work_status = 1
GROUP BY u.name, u.id
If there are only one John working in the company, your query is this one:
SELECT u.name, COUNT(*) as num
FROM users u INNER JOIN works w ON w.user_id = u.id
WHERE w.work_status = 1
GROUP BY u.name
Note: If three John are the same person, you should delete the 2 last and on the works table change user_id = 3 and user_id = 4 for user_id = 1
This is a simple JOIN query:
SELECT u.name, COUNT(*) num
FROM users u
JOIN works w
ON w.user_id = u.id
AND w.work_status = 1
GROUP BY u.name
This one should do the job:
SELECT users.name,SUM(works.work_status) as num
FROM users,works
WHERE users.id=works.id
GROUP BY name
SELECT
users.`name`,
COUNT(*) num
FROM
users,
works
WHERE users.`id` = works.`user_id`
AND works.`work_status` = 1
GROUP BY users.`name` ;
There are three table, I mainly would like to query the transaction
User
-----
id
Debit
-----
id
amount
* user_id
create_date
Credit
------
id
amount
* user_id
create_date
So, I would like to get the transaction for particular user that order by the date, like the following
$150 1 2014-06-15 <== debit
$200 1 2014-06-16 <===credit
The problem is how to query? I need to join debit and credit together? Thanks for helping.
Use UNION ALL not JOIN since you want credit and debit rows separate. An extra join to table User if you want to get columns from it.
SELECT d.amount, d.create_date, 'debit' transactionType,
u.user_name
FROM Debit d
INNER JOIN User u
ON d.id = u.user_id
WHERE u.user_id = 1
UNION ALL
SELECT c.amount, c.create_date, 'credit' transactionType,
u.user_name
FROM Credit c
INNER JOIN User u
ON c.id = u.user_id
WHERE u.user_id = 1
ORDER BY create_date