MySql duplicate SUM, one to many relation ship - mysql

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;

Related

mysql query not working as expected complex

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

Identical values in one column and unique value pairs

I have 2 columns
A B
1 2
2 2
1 2
3 2
5 2
0 2
4 2
11 4
12 4
11 4
I want the SQL query to return the pairs (A,B) where:
B has appeared 3 or more times
AND (A,B) is unique
The resulting table would be:
A B
1 2
2 2
3 2
5 2
0 2
4 2
You could use a join with a selected table grouped by B having count = 3
select distinct A, B
from my_table as t1
inner join (
select b
from my_table
group by b
having count(*)= 3
) t2 on t2.b = t1.b
and for 3 or more
select distinct A, B
from my_table as t1
inner join (
select b
from my_table
group by b
having count(*) >= 3
) t2 on t2.b = t1.b

How to select counts from different tables with foreign keys?

city
----------------------
id city_name
1 Pune
2 Mumbai
3 Banglore
Branches
----------------------
id branch_name city_id
1 Magarpatta 1
2 Wagholi 1
3 Kurla 2
4 CST 2
5 Thane 2
6 Anekal 3
Employees
----------------------
id employee_name city_id
1 Arun 1
2 Varun 1
3 Mahesh 2
4 Umesh 2
5 Prakash 1
6 Kedar 3
Expected result
-----------------------------
id(city) city_name No_of_Branch no_of_employee
1 Pune 2 3
2 Mumbai 3 2
3 Banglore 1 1
Can I get above result in single Query?
Can I Use Crosstab in this scenario? How?
SELECT c.*, count(DISTINCT b.id) AS No_of_Branch, count(DISTINCT e.id) AS no_of_employee
FROM city c
LEFT JOIN branches b ON (b.city_id=c.id)
LEFT JOIN employees e ON (e.city_id=c.id)
GROUP BY c.id
Or
SELECT c.*,
(SELECT count(b.id) FROM branches b WHERE b.city_id=c.id) AS No_of_Branch,
(SELECT count(e.id) FROM employees e WHERE e.city_id=c.id) AS no_of_employee
FROM city c
Try this:
SELECT c.id, c.city_name,
COUNT(b.id) AS No_of_Branch,
no_of_employee
FROM city c
LEFT JOIN branches b ON b.city_id = c.id
LEFT JOIN (
SELECT city_id, COUNT(e.id) AS no_of_employee
FROM employees AS e
GROUP BY e.city_id
) AS e_grp ON e_grp.city_id = c.id
GROUP BY c.id, c.city_name
The trick here is to use a derived table containing the count of employees per city_id. I you just join all tables together you will end up with duplicate counts of employees.
Demo here

mysql select 2 row from each group by

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

Unioning and Joining in 1 Query?

I have the following tables:
TABLE1:
A B C
1 2 3
2 4 6
3 6 9
TABLE2:
A B C
4 8 12
5 10 15
6 12 18
TABLE3:
A D
2 X
4 Y
6 Z
I need one query that gives:
A B C D
1 2 3
2 4 6 X
3 6 9
4 8 12 Y
5 10 15
6 12 18 Z
Is that possible?
I can do it in 2 queries, but the person I'm doing it for wants it in 1.
Thanks!
Try this (example on sqlfiddle):
SELECT x.a, x.b, x.c, d
FROM (
SELECT a, b, c FROM table1
UNION ALL
SELECT a, b, c FROM table2
) x
LEFT JOIN table3 ON ( table3.a = x.a )
Sure:
select v1.*, table3.d
from
(select table1.a, table1.b, table1.c
from table1
union all
select table2.a, table2.b, table2.c
from table2
) v1
left join table3 on v1.a = table3.a