I have 3 tables in which all 3 tables are joined to get the result.
Below is the table,
//tbl_order
order_id order_no_first order_no order_no_last order_date
---------------------------------------------------------------------
1 C 1000 a 2017-05-16
2 C 1001 a 2017-05-16
3 C 1001 b 2017-05-16
4 A 1002 a 2017-05-16
5 A 1002 b 2017-05-16
//tbl_assign
assign_id order_id order_no_first central_status central_assign_unit
----------------------------------------------------------------------------
1 1 C 1 1
2 2 C 1 1
3 3 C 1 1
4 4 A 1 1
//tbl_unit_status
status_id assign_id status_status
---------------------------------------
1 1 Cutter
2 1 Stitch
3 1 Delivery
4 2 Cutter
5 2 Stitch
6 3 Cutter
7 4 Cutter
I want the result as below,
//Required output
order_id assign_id order_no_first order_no order_no_last status_status
---------------------------------------------------------------------------
2 2 C 1001 a Stitch
3 3 C 1001 b Cutter
4 4 A 1002 a Cutter
5 A 1002 b
from the table tbl_unit_status the status below status_status field, if it is Despatch then do not display that result.
I have tried to get the above result. But no success below is my code.
`SELECT *
FROM tbl_order o
LEFT JOIN tbl_assign a
ON a.order_id = o.order_id AND o.order_no_first = a.order_no_first
LEFT JOIN (
SELECT u.assign_id, max(u.status_id) AS maxid
FROM tbl_unit_status u GROUP BY u.assign_id) uu
ON uu.assign_id = a.assign_id
LEFT JOIN tbl_unit_status u2 on u2.status_id = uu.maxid
WHERE a.central_status = 1 AND a.central_assign_unit = 1
OR (u2.status_status != "Delivery" AND u2.status_status != "Despatch")
GROUP BY o.order_id
From the above code, the result is
//wrong output
order_id assign_id order_no_first order_no order_no_last status_status
---------------------------------------------------------------------------
1 1 C 1000 a Delivery
2 2 C 1001 a Stitch
3 3 C 1001 b Cutter
4 4 A 1002 a Cutter
5 A 1002 b
Is there any way to get the Required output as soon in the first output. I have tried and am stuck in here.
Thank you.
Use parenthesis correctly arround AND OR clauses and I recommend using NOT LIKE instead of != when dealing with strings.
SELECT *
FROM tbl_order o
LEFT JOIN tbl_assign a
ON a.order_id = o.order_id AND o.order_no_first = a.order_no_first
LEFT JOIN (
SELECT u.assign_id, max(u.status_id) AS maxid
FROM tbl_unit_status u GROUP BY u.assign_id) uu
ON uu.assign_id = a.assign_id
LEFT JOIN tbl_unit_status u2 on u2.status_id = uu.maxid
WHERE a.central_status = 1 AND (a.central_assign_unit = 1 OR u2.status_status NOT LIKE 'Delivery' AND u2.status_status NOT LIKE 'Despatch')
GROUP BY o.order_id
Obs: I can't comment due to my reputation, I'm sorry
You should be using AND instead of OR in your where clause. You don't need the parentheses either.
SELECT *
FROM tbl_order o
LEFT JOIN tbl_assign a
ON a.order_id = o.order_id
AND o.order_no_first = a.order_no_first
LEFT JOIN
(SELECT u.assign_id, max(u.status_id) AS maxid
FROM tbl_unit_status u
GROUP BY u.assign_id
) uu ON uu.assign_id = a.assign_id
LEFT JOIN tbl_unit_status u2
on u2.status_id = uu.maxid
WHERE a.central_status = 1
AND a.central_assign_unit = 1
AND (
u2.status_status IS NULL
OR
u2.status_status NOT IN ("Delivery", "Despatch")
)
GROUP BY o.order_id
Related
I'm trying to update a field in the database to the sum of its joined values:
I have this sample data:
ID refer_id my_reflink s1 s2 m1 m2
-----------------------------------------------
1 a 1 7
2 a b 2 8
3 a c 3 9
4 d 4 0
5 b e 5 1
6 c f 6 2
I need a query that gives this result to s1 and s2.
ID refer_id my_reflink s1 s2 m1 m2
----------------------------------------------------
1 a 5 17 1 7
2 a b 5 1 2 8
3 a c 0 0 3 9
4 d 4 0
5 b e 6 2 5 1
6 e f 0 0 6 2
The result is
s1 = sum of m1 for refer_id = my_reflink for each ID
s2 = sum of m2 for refer_id = my_reflink for each ID
So for ID1, it will be
s1 = 5 because ID 2 and ID 3 have my_reflink of ID 1 and m1 = 2 m1 = 3
s2 = 17 because ID 2 and ID 3 have my_reflink of ID 1 and m2 = 8 m2 = 9
The update/join syntax might be different depending on the RDBMS used but the below query works for MySql
UPDATE someTable t
JOIN (SELECT refer_id, SUM(m1) sum_m1, SUM(m2) sum_m2
FROM someTable
GROUP BY refer_id) s ON s.refer_id = t.my_reflink
SET s1 = COALESCE(sum_m1, 0), s2 = COALESCE(sum_m2,0)
You seem to want a join:
select t.ID, t.refer_id, t.my_reflink, tt.m1, tt.m2,
t.m1, t.m2
from t left join
(select refer_id, sum(m1) as m1, sum(m2) as m2
from t
group by refer_id
) tt
on t.my_reflink = tt.refer_id;
If you want to actually change the data (rather than return a result set), then the update syntax depends on the database you are using.
This question already has an answer here:
MySQL pivot row into dynamic number of columns
(1 answer)
Closed 4 years ago.
I have three tables e.g client, product and purchase.
Purchase:
id productId clientId amount
1 1 2 2500
2 2 3 3500
3 3 4 4500
4 6 1 5500
5 1 2 1500
6 3 3 2000
7 3 2 1000
Client:
id name
1 A
2 B
3 C
4 B
Product:
id product
1 Apple
2 Banana
3 Mango
6 Sweet
I am able to query this
SELECT client.id, client.client_name, product.product, purchase.amount from client INNER JOIN purchase ON client.id=purchase.clientId INNER JOIN product ON product.id=purchase.productId GROUP BY client.id
My output is:
id client_name product amount
1 A Sweet 5500
2 B Apple 2500
3 c Mango 3500
4 D Banana 4500
But I want output like where is the amount of each client purchased
Desired Output:
id client_name Apple Banana Mango Sweet
1 A x x x x
2 B X x x x
3 c X x x x
4 D x x x x
How can I do that with query. thanks
If you have a limited products then i would use conditional aggregation :
SELECT c.id, c.client_name,
SUM(CASE WHEN pd.id = 1 THEN p.Amount ELSE 0 END) AS Apple,
SUM(CASE WHEN pd.id = 2 THEN p.Amount ELSE 0 END) AS Banana,
SUM(CASE WHEN pd.id = 3 THEN p.Amount ELSE 0 END) AS Mango,
SUM(CASE WHEN pd.id = 6 THEN p.Amount ELSE 0 END) AS Sweet
FROM client c INNER JOIN
purchase p
ON c.id = p.clientId INNER JOIN
product pd
ON pd.id = p.productId
GROUP BY c.id, c.client_name;
The easiest way if your Purchase table is static
select
c.id,
c.name,
(select SUM(p.amount) from Purchase p where p.productId = 1 and p.clientId = c.id) as Apple,
(select SUM(p.amount) from Purchase p where p.productId = 2 and p.clientId = c.id) as Banana,
(select SUM(p.amount) from Purchase p where p.productId = 3 and p.clientId = c.id) as Mango,
(select SUM(p.amount) from Purchase p where p.productId = 6 and p.clientId = c.id) as Sweet
from Client c
i have 2 table with this structure
Products
id title
-----------------
1 sample 1
2 sample 2
3 sample 3
4 sample 4
5 sample 5
6 sample 6
gallery
id typeid name
-------------------------------
1 1 sample for 1
2 1 sample for 1
3 1 sample for 1
4 2 sample for 2
5 2 sample for 2
7 2 sample for 2
8 3 sample for 3
9 3 sample for 3
10 3 sample for 3
11 4 sample for 4
12 4 sample for 4
13 5 sample for 5
14 5 sample for 5
and iwant this for lists of id eg(1,2,3)
id typeid name
---------------------
1 1 sample for 1
1 2 sample for 1
2 4 sample for 2
2 5 sample for 2
3 8 sample for 3
3 9 sample for 3
here is my query
select p.*,g.* from products p inner join gallery g ON p.id=g.typeid where p.id in (3,4,5) group by typeid
here is real structure sqlfiddle link
SELECT p.id, g.typeid, g.id, g.title
FROM products p
INNER JOIN (SELECT * FROM gallery a
WHERE (SELECT COUNT(*) FROM gallery b WHERE b.title = a.title AND b.id >= a.id) <= 2
) g ON p.id = g.typeid
WHERE p.id in (3,4,5)
EDIT:
Try this SQL Fiddle Demo
SELECT p.id, g.typeid, g.id, g.name
FROM products p
INNER JOIN (SELECT * FROM gallery a
WHERE (SELECT COUNT(*) FROM gallery b WHERE b.typeid = a.typeid AND b.id >= a.id) <= 2
) g ON p.id = g.typeid
WHERE p.id in (3,4,5) order by g.id asc
So basically this part
WHERE (SELECT COUNT(*) FROM gallery b WHERE b.title = a.title AND b.id >= a.id) <= 2
is used to replace group by typeid
SELECT p.id,p.title, g.typeid, g.id, g.name
FROM products p
INNER JOIN (SELECT * FROM gallery a
WHERE (SELECT COUNT(*) FROM gallery b WHERE b.typeid = a.typeid AND b.id >= a.id)
please try this
I have join with a multiple tables. You can find the tables and MySQL query in this demo.
These are the tables:
users
id name
1 abc
2 xyz
3 pqr
friend
user_id friend_id
1 2
1 3
2 3
collection
id user_id friend_id amount
1 1 2 100
2 2 1 -100
3 2 3 200
4 3 2 -200
5 1 3 300
6 3 1 -300
bill
id use_id(bill_creator)
1 1
2 2
3 2
bill_person
id bill_id user_id
1 1 1
2 1 2
3 1 3
4 2 2
5 2 3
6 3 2
6 3 1
So far I have made this query:
SELECT mf.id
, mf.name
, c.amount AS amount,count(bp.user_id) AS no_common_bills
FROM (
SELECT fr.user_id AS user_id
, fr.friend_id AS friend_id
FROM friend fr
JOIN users fru
ON fru.id = fr.user_id
WHERE fru.id IN (1)
UNION
SELECT fl.friend_id AS user_id
, fl.user_id AS friend_id
FROM friend fl
JOIN users flf
ON flf.id = fl.friend_id
WHERE flf.id IN (1)
) f
JOIN users mf
ON mf.id = f.friend_id
LEFT
JOIN collection c
ON c.friend_id = mf.id
AND c.user_id = f.user_id
LEFT JOIN bill_person bp
ON bp.user_id=f.user_id AND c.friend_id = mf.id
GROUP BY mf.id
ORDER BY mf.id
and my output of this query is
id NAME AMOUNT NO_COMMON_BILLS
2 XYZ 100 2
3 PQR 300 2
but I want this result:
id NAME AMOUNT NO_COMMON_BILLS
2 XYZ 100 2
3 PQR 300 1
I am getting wrong output at NO_COMMON_BILLS. Other than that, all values are correct.
Third time's a charm...
http://sqlfiddle.com/#!2/338dd/12
SELECT u.name friend
, COUNT(bp2.user_id) no_common_bills
FROM users u
LEFT
JOIN
( SELECT user_id me, friend_id them FROM friend WHERE user_id = 1
UNION
SELECT friend_id,user_id FROM friend WHERE friend_id = 1
) x
ON x.them = u.id
LEFT
JOIN bill_person bp1
ON bp1.user_id = x.them
LEFT
JOIN bill_person bp2
ON bp2.bill_id = bp1.bill_id
AND bp2.user_id = x.me
WHERE u.id <> 1
GROUP
BY friend;
FRIEND NO_COMMON_BILLS
jkl 0
mno 0
pqr 1
xyz 2
I have following 3 tables where B, C have many to one relation ship with A, and i need to generate the following Result table with MySql
Table A Table B Table C
id name id A.id qty id A.id
1 a 1 1 5 1 1
2 b 2 1 10 2 1
3 c 3 2 4 3 2
4 3 6 4 2
5 2 7
Expected Result Table
C.id A.id sum(B.qty Group by A.id)
1 1 15
2 1 15
3 2 11
4 2 11
select c.id, a.id, sum(b.qty)
from c
inner join a on c.a_id = a.id
inner join b on b.a_id = a.id
group by c.id, a.id
or in your case, you just don't need table a
select c.id as cid, c.a_id as aid , sum(b.qty) as totalqty
from c
inner join b on b.a_id =c.a_id
group by c.id, c.a_id;