In MYSQL DB, I have people_table :
------------------------------------------
people_table
------------------------------------------
person_ID(INT) name(VARCHAR(45))
1 A
2 B
3 C
4 D
------------------------------------------
and lend_borrow_money_table
------------------------------------------
lend_borrow_money_table
------------------------------------------
bill(DATE) lender_ID money(INT) borrower_ID
2018-11-1 1 100 2
2018-11-2 2 200 3
2018-11-3 3 300 4
2018-11-30 2 400 3
------------------------------------------
Now I Want select result like this
------------------------------------------
name lend borrow total
A 500 0 500
B 400 500 -100
C 0 600 -600
D 0 300 -300
------------------------------------------
In my way, I use join select to group by twice
but I think this is not best solution
SELECT Lender.name,Lend.lend,SUM(money) AS borrow
FROM lend_borrow_money_table
INNER JOIN people_table AS Borrower ON people_table.ID = lend_borrow_money_table.borrower_ID
INNER JOIN
(
SELECT SUM(money) as lend
FROM lend_borrow_money_table
WHERE bill<'2018/11/31' AND bill>'2018/11/1'
GROUP BY lender_ID
)AS Lend ON Lend.lender_ID
INNER JOIN people_table AS Lender ON people_table.ID = lend_borrow_money_table.lender_ID
WHERE bill<'2018/12/1' AND bill>'2018/11/1'
GROUP BY borrower_ID
My Question is that how to using GROUP BY twice but not with JOIN itself?
You can try below
select a.name,sum(b.money) as lend, sum(c.money) as borrow, sum(b.money)-sum(c.money) as total
from people_table a
left join lend_borrow_money_table b on a.id=b.lender_ID
left join lend_borrow_money_table c on a.id=c.borrower_ID
where bill>'20181101' and bill<'20181201'
group by a.name
Related
i have 3 table that will join into 1 table.
table1
id|prdct_name|qty
001 A 5
002 B 5
003 C 5
table2
id|prdct_id|price
1 001 100
2 001 200
3 002 150
4 002 250
table3
id|prdct_id|stock_in|stock_out
1 001 5 0
2 001 10 0
3 002 15 0
4 002 25 0
I have tried this sql (updated due to typo)
select a.prdct_name as Name, a.qty as QTY,b.price as PRICE, c.stock_in as SIN,c.stock_out as SOUT
from table1 a
left join table2 b on a.id=b.prdct_id
left join table3 c on a.id=c.prdct_id
where 1=1 group by b.id,c.id
but result return duplicate like this table
NAME|QTY|PRICE|SIN|SOUT
A 5 100 5 0
A 5 100 10 0
A 5 200 5 0
A 5 200 10 0
B 5 150 15 0
B 5 150 25 0
B 5 250 15 0
B 5 250 25 0
the result should be
NAME|QTY|PRICE|SIN|SOUT
A 5 100 5 0
A 5 200 10 0
B 5 150 15 0
B 5 250 25 0
is there a way to remove the duplicate issue? trying with distinct also not help.
Thanks
It looks like your second join should be on id -- and your join conditions don't look right anyway.
select a.prdct_name as Name, a.qty as QTY, b.price as PRICE, c.stock_in as SIN,c.stock_out as SOUT
from table1 a left join
table2 b
on a.id = b.prdct_id left join
table3 c
on a.id = c.id
where 1=1
group by b.id, c.id
change your join key it should id =product_id but you are trying with name & product_id
select a.prdct_name as Name, a.qty as QTY,b.price as PRICE, c.stock_in as SIN,c.stock_out as SOUT
from table1 a
left join table2 b on a.id=b.prdct_id
left join table3 c on a.id=c.prdct_id
where 1=1 group by b.id,c.id
You can make use of the SQL keyword DISTINCT to select non-duplicate rows.
Example:
SELECT DISTINCT
prdct_name as NAME,
qty as QTY,
price as PRICE,
stock_in as SIN,
stock_out as SOUT
FROM
table1 a
INNER JOIN
table2 b ON a.id=b.prdct_id
LEFT JOIN
table3 c ON b.id=c.id
GROUP BY
a.id, c.id
Working SQL fiddle
More about DISTINCT here.
I have 3 tables like:
owner_details:-
owner_id owner_name
---------------------
1 A
2 B
3 C
-------------------
vehicle_owner:-
v_id vehicle_id owner_id
-------------------------
1 1 1
2 2 2
3 4 1
4 3 1
5 5 3
transaction:-
id v_id amount transaction_type
--------------------------------
1 1 100 0
2 2 250 1
3 1 150 1
4 3 450 1
5 1 200 0
6 4 300 1
7 5 150 0
8 5 200 1
transaction_type= 0 then (-) transaction_type=1 then (+)
Owner A (1) have 3 vehicles with v_id (1,3,4) in table vehicle_owner.
v_id (1,3,4) have 5 entries in table transaction (1,3,4,5,6) with sum of amount 600 (-100+150+450-200+300)
Now I want listing like this:-.
owner_id owner_name amount
---------------------
1 A 600
2 B 250
3 C 50
-------------------
You can use the following query:
SELECT od.owner_id, od.owner_name, SUM(t.amount) AS amount
FROM owner_details od INNER JOIN vehicle_owner vo ON od.owner_id = vo.owner_id
INNER JOIN `transaction` t ON vo.v_id = t.v_id
GROUP BY od.owner_id
If you want to use the additional transaction_type you can use the following:
SELECT od.owner_id, od.owner_name, SUM(CASE WHEN t.transaction_type = 0 THEN t.amount * -1 ELSE t.amount END) AS amount
FROM owner_details od INNER JOIN vehicle_owner vo ON od.owner_id = vo.owner_id
INNER JOIN `transaction` t ON vo.v_id = t.v_id
GROUP BY od.owner_id
demo: http://sqlfiddle.com/#!9/c5f8d/1/1
Try this:
SELECT A.owner_id, A.owner_name, SUM(IFNULL(amount,0)) AMOUNT
FROM owner_details A LEFT JOIN
vehicle_owner B
ON A.owner_id=B.owner_id
LEFT JOIN `transaction` C
ON C.v_id=B.v_id
GROUP BY A.owner_id, A.owner_name;
It works for me
SELECT od.owner_id,
od.owner_name,
Sum(t.amount) AS amount
FROM owner_details od
INNER JOIN vehicle_owner vo
ON od.owner_id = vo.owner_id
INNER JOIN (SELECT v_id,
Coalesce(Sum(CASE
WHEN type = 0 THEN -amount
ELSE +amount
end), 0.0) AS amount
FROM `transaction`
GROUP BY v_id) t
ON vo.v_id = t.v_id
GROUP BY od.owner_id
Thanks Sebastian Brosch for quick response !!!
I have 3 tables like following:
branch
id name
---------
1 abc
2 xyz
users
id branch_id name
-----------------
1 1 aa
2 1 bb
3 2 cc
4 1 dd
5 2 ee
sales
id user_id product price
1 1 xxxx 10
2 1 yyyy 20
3 2 zzzz 18
4 3 aaaa 12
5 2 bbbb 10
6 4 cccc 20
Now I want to get the total selling amount branch wise like:
branch_id total_price
---------------------
1 78
2 12
For that i write a sql query like:
SELECT SUM(s.price) , b.id
FROM sales s
JOIN branch b
GROUP BY id
HAVING s.user_id
IN (
SELECT id
FROM users
WHERE branch_id = b.id
)
But this does not provide the answer that I want. Please help me.
I think this should do the trick:
SELECT branch.id AS branch_id, SUM(s.price) AS total_price
FROM branch
JOIN users ON branch.id = users.branch_id
JOIN sales ON users.id = sales.user_id
GROUP BY branch.id;
Also you could use INNER JOIN instead of JOIN(Both are doing the same thing). With INNER JOIN it is possibly easier to read, especially your query contains other types of JOIN's like LEFT JOIN or RIGHT JOIN
Hope that helps!
You could use something like this:
SELECT u.branch_id, SUM(s.price) AS total_price
FROM sales AS s INNER JOIN users u ON s.user_id = user.id
GROUP BY u.branch_id
ORDER BY u.branch_id
I have three tables as following and I try to group by over three elements to display all possible combinations
Play
--------------------------------
id typeId periodId
--------------------------------
1 a 1
2 b 1
3 b 1
4 b 1
5 a 2
6 b 1
7 a 1
8 b 2
Period
-------------
periodId
-------------
1
2
3
Type
-------------
typeId
-------------
a
b
c
I tried this but it doesn't work, I see some NULL values but the group by doesn't work.
SELECT type, p, count(*) as superNiceCount
FROM Play
RIGHT JOIN Period pp ON Play.periodId = Period.periodId
RIGHT JOIN Type tt ON Play.typeId = Type.typeId
GROUP BY tt.typeId, pp.periodId
The expected result would be
-------------------------
type p superNiceCount
-------------------------
a 1 2
a 2 1
a 3 0
b 1 4
b 2 1
b 3 0
c 1 0
c 2 0
c 3 0
How may I achieve that ?
see if this works
SELECT ty.typeId as type, pe.periodId as p, count(pl.id) as superNiceCount
FROM Period pe
CROSS JOIN Type ty
LEFT JOIN Play pl ON (pl.periodId = pe.periodId AND pl.typeId = ty.typeId)
GROUP BY ty.typeId, pe.periodId
if not try
SELECT ty.typeId as type, pe.periodId as p, count(pl.id) as superNiceCount
FROM (
SELECT * FROM
Period pe
CROSS JOIN Type ty
) as t1
LEFT JOIN Play pl ON (pl.periodId = t1.periodId AND pl.typeId = t1.typeId)
GROUP BY ty.typeId, pe.periodId
I've got a budget table:
user_id product_id budget created
-----------------------------------------------------------------
1 1 300 2011-12-01
2 1 400 2011-12-01
1 1 500 2011-12-03
2 2 400 2011-12-04
I've also got a manager_user table, joining a manager with the user
user_id manager_id product_id
------------------------------------
1 5 1
1 9 2
2 5 1
2 5 2
3 5 1
What I'd like to do is grab each of the user that's assigned to Manager #5, and also get their 'budgets'... but only the most recent one.
Right now my statement looks like this:
SELECT * FROM manager_user mu
LEFT JOIN budget b
ON b.user_id = mu.user_id AND b.product_id = mu.product_id
WHERE mu.manager_id = 5
GROUP BY mu.user_id, mu.product_id
ORDER BY b.created DESC;
The problem is it doesn't pull the most recent budget. Any suggestions? Thanks!
To accomplish your task you can do as follows:
select b1.user_id,
b1.budget
from budget b1 inner join (
select b.user_id,
b.product_id,
max(created) lastdate
from budget b
group by b.user_id, b.product_id ) q
on b1.user_id=q.user_id and
b1.product_id=q.product_id and
b1.created=q.lastdate
where b1.user_id in
(select user_id from manager_user where manager_id = 5);
I'm assuming here that your (user_id, product_id, created) combination is unique.
For what it's worth, here's the code that returned what I was looking for:
SELECT DISTINCT(b1.id),mu.user_id,mu.product_id,b1.budget,b1.created
FROM budget b1
INNER JOIN (
SELECT b.user_id, b.product_id, MAX(created) lastdate
FROM budget b
GROUP BY b.user_id, b.product_id) q
ON b1.user_id=q.user_id AND
b1.product_id=q.product_id AND
b1.created=q.lastdate
RIGHT JOIN manager_user mu
ON mu.user_id = b1.user_id AND
mu.product_id = b1.product_id
WHERE mu.manager_id = 5;
Thanks for the help Andrea!