Mysql query count total from other table - mysql

Table jenis_usaha
ID JENIS_USAHA
1 Laundry
2 Restauran
Table waralaba
ID ID_JENIS_USAHA
1 1
2 2
Table perusahaan
ID NAME ID_WARALABA
1 A 1
2 B 2
3 C 1
4 D 1
Table outlet
ID ID_PERUSAHAAN
1 1
2 1
3 2
4 2
5 1
6 1
7 1
8 1
9 1
The result that I want is like this
ID JENIS_USAHA TOTAL_PERUSAHAAN TOTAL_OUTLET
1 Laundry 3 7
2 Restauran 1 2
I have this mysql query
SELECT ju.ID,
ju.JENIS_USAHA,
COUNT(p.ID) AS TOTAL_PEMBERI,
(SELECT count(o.ID)
FROM outlet o
WHERE p.ID = o.ID_PERUSAHAAN
AND w.ID = p.ID_WARALABA
AND ju.ID = w.ID_JENIS_USAHA
) AS TOTAL_OUTLET
FROM jenis_usaha ju
LEFT JOIN waralaba w ON w.ID_JENIS_USAHA = ju.ID
LEFT JOIN perusahaan p ON p.ID_WARALABA = w.ID
GROUP BY ju.ID
I've got no error BUT a wrong result for TOTAL_OUTLET. Could you please show me the right query for this? thanks

you did the wrong join that's why you got wrong output
below query should work as your way
select j.id ,j.name,TOTAL_PERUSAHAAN,outlet as TOTAL_OUTLET
from jenis_usaha j inner join
(select p.id,count(o.ID_PERUSAHAAN) as outlet from outlet o
inner join perusahaan p
on o.ID_PERUSAHAAN=p.id
group by p.id
) t1
on t1.id=j.id
inner join
(
select w.id, count(p.ID_WARALABA) as TOTAL_PERUSAHAAN
from waralaba w inner join perusahaan p
on w.id=p.ID_WARALABA
group by w.id
) t2
on t2.id=j.id
id name TOTAL_PERUSAHAAN TOTAL_OUTLET
1 Laundry 3 7
2 Restauran 1 2
here is fiddle link where you can find details
http://sqlfiddle.com/#!9/400c971/6

I was taking very long to understand your query, so I decided to do my own.
This one bring the results you wanted anyway. Please check if this works for your case. =)
Btw, st stands for subtotal
SELECT
st.id,
st.name,
count(ps_id) AS ps_amount,
sum(ol_amount) AS ol_amount
FROM
(SELECT
ju.id,
ju.name,
ps.id AS ps_id,
count(ol.id) as ol_amount
FROM
jenis_usaha ju
INNER JOIN
waralaba wl
ON wl.id_jenis_usaha = ju.id
LEFT JOIN
perusahaan ps
ON ps.id_waralaba = wl.id
LEFT JOIN
outlet AS ol
ON ol.id_perusahaan = ps.id
GROUP BY
ju.id,
ju.name,
ps.id) st
GROUP BY
st.id,
st.name

Related

mysql group_concat with where in condition

I have three table product
ID name
1 A
2 B
3 C
vendor
ID Name
1 V1
2 V2
3 V3
product_vendor
ID pid vid
1 1 1
2 1 2
3 2 3
4 3 1
5 3 2
6 3 3
i want left join and create response like below table
p_id product vendors
1 A V1,V2
2 B V3
3 C V1,V2,V3
below is the my solution but it's quite complicated. Is it possible to have a short query for this
SELECT GROUP_CONCAT(v2.`name`) AS vendername,
p.*,
GROUP_CONCAT(v.`v_id`) AS vi
FROM `products` p
JOIN `product_vendor` v ON p.id = v.p_id,
`vendors` v2
WHERE v2.id IN
(SELECT GROUP_CONCAT(p.`id`) AS vi
FROM `vendors` p
JOIN `product_vendor` v ON p.id = v.p_id
GROUP BY p.id)
GROUP BY p.id
ORDER BY `p_id` ASC
Can you try this, I do not have executed the query but thinks it will help:
SELECT `P`.`id`, `P`.`name`, CONCAT(GROUP_CONCAT(CAST(`V`.`name` AS CHAR)), ", ") as vendername
FROM `product` P
JOIN `product_vendor` PV ON (`PV`.`pid` = `P`.`id`)
JOIN `vendor` V ON (`V`.`ID` = `PV`.`vid`)
GROUP BY `PV`.`pid`
For listing all records in Product table use:
SELECT `P`.`id`, `P`.`name`, CONCAT(GROUP_CONCAT(CAST(`V`.`name` AS CHAR)), ", ") as vendername
FROM `product` P
LEFT JOIN `product_vendor` PV ON (`PV`.`pid` = `P`.`id`)
LEFT JOIN `vendor` V ON (`V`.`ID` = `PV`.`vid`)
GROUP BY `PV`.`pid`
ORDER BY `P`.`id`
Just do it with joins and group_concat no need for sub queries
SELECT
p.id,
p.name,
Group_concat(v.name) AS vendername
FROM
product p
JOIN
product_vendor pv ON p.id = pv.pid
JOIN
vendor v ON pv.vid = v.id
GROUP BY
p.id, p.name
ORDER BY
p.id ASC
Demo

mysql group by and take specific record from right join table

I have database with following data structure with sample data. Each company have multiple members. The relationship is in the company_member table. Please note only required fields I have given below.
company
id title
1 company-1
2 company-2
company_member
companyid memberid
1 1
1 2
1 3
2 4
2 5
2 6
member
id firstname member_type_id
1 Name-1 2
2 Name-2 3
3 Name-3 3
4 Name-4 3
5 Name-5 2
6 Name-6 1
member_type
id user_level
1 0
2 1
3 2
I want list of unique companies with one member from each. But the member should be the lowest user_level within the company. i.e, following result should come;
result
companyid company_title memberid member_name user_level
1 company-1 1 Name-1 1
2 company-2 6 Name-6 0
I want to know how to get one member with lowest user level among the same company.
This is a bit complicated one, however this is one way of doing it using not exists, for bigger tables its wise to use not exits since using pivot tables it will not use index.
select
c.id,
c.title,
m.id as member_id,
m.firstname,
mt.user_level
from company_member_map cmp
join company c on c.id = cmp.companyid
join member m on m.id = cmp.memberid
join member_type mt on mt.id = m.member_type_id
where not exists
(
select 1 from company_member_map t1
join member t2 on t2.id = t1.memberid
join member_type t3 on t3.id = t2.member_type_id
where
t1.companyid = cmp.companyid
and t3.user_level < mt.user_level
)
DEMO
Finally I found solution:
select c.id companyid, c.title company_title, m.firstname member_name, mt.user_level
from company c
inner join company_member cm on cm.companyid = c.id
inner join member m on m.id = cm.memberid
inner join member_type mt on mt.id = m.member_type_id
inner join
(select c1.id companyid, mt1.user_level
from company c1
join company_member cm1 on cm1.companyid = c1.id
join member m1 on m1.id = cm1.memberid
join member_type mt1 on mt1.id = m1.member_type_id
group by c1.id,m1.id
order by user_level asc
) sq on c.id = sq.companyid and sq.user_level = mt.user_level
group by c.id;
Correct this, if anyone have better solution or simplified solution.
Check this SQL Fiddle

Select only the latest version of the data with SQL join

I have two tables one containg offer information and the other containg products
something like this:
OFFER PRODUCTS
ID Number Version poID offer_id Product how_many
========================== ========================================
1 123 1 1 1 Apple 1
2 123 2 2 1 Banana 2
3 124 1 3 1 Orange 1
4 2 Apple 1
5 2 Banana 2
6 2 Orange 2
7 2 Kiwi 1
8 3 Apple 2
9 3 Banana 3
I would like a list of how many products that are currently offered.
Since OFFER(id = 2) is an update of (id = 1) only (id = 2) should be counted.
How should I best query this?
First you need to get all the latests offers:
select o.id
from offer o
where version = (select max(version)
from offer o2
where o2.number = o.number);
Based on the above you can then get all the products:
select p.*
from products p
where offer_id in (select o.id
from offer o
where version = (select max(version)
from offer o2
where o2.number = o.number));
If id and version correlate:
select sum(how_many) from products p
join offer on p.offer_id=offer.id
join (
select number, max(version) version from offer group by number
) x
on offer.id=x.id and offer.version = x.version
SELECT *
FROM products
WHERE offer_id = (SELECT MAX(id) FROM offer)
or, if you prefer the join syntax
SELECT p.*
FROM products p
INNER JOIN (SELECT MAX(id) id FROM offer) o ON p.offer_id = o.id
Edit (still not completely sure this is what you want without seeing your desired results)
SELECT p.*
FROM products p
INNER JOIN offer o on p.offer_id = o.id
INNER JOIN
(SELECT number, max(version)
FROM offer
GROUP BY number
) oMax ON o.number = oMax.number AND o.version = oMax.version
Try this:
select [list columns here]
from products p
join (select offernumber, max(id) as ID from offer group by offernumber) a
on a.id = p.offer_id
If you need addtional columns from offer other than the offernumber and the id:
select [list columns here]
from products p
join (select offernumber, max(id) as ID from offer group by offernumber) a
on a.id = p.offer_id
join offer o on o.id = a.id

How to SUM query 3 tables?

A table
--------------------
id bId txnVolume
1 1(b table) 10.00
2 1 5.00
3 2 7.00
4 3 2.00
B table
--------------------
id cId
1 1(C table)
2 2
3 3
C table
--------------------
id cusId prodId
1 1 1
2 1 2
3 1 1
4 1 2
I want to get the sum of txnVolume on table A if C table is custId 1 and prodId 1? Thanks for the help guys! Cheers!
Select sum(coalesce(txnVolume,0))
FROM A INNER JOIN B on A.BID = B.Bid
INNER JOIN C on C.ID = B.CID
WHERE C.CustID = 1 and C.ProdID = 1
Try the following:
select sum(txnVolume) from A
join B on A.bID = B.id
join C on C.cID = C.id
where prodID = 1 and custID = 1
In sql you need to join the tables. When tables are joined they create one big table. You can then add where clauses to the columns.
SELECT sum(txnVolume)
FROM A
JOIN B on A.bId = B.id
JOIN C on B.cID = c.id
where c.custid = 1 and c.prodid = 1
SELECT SUM(a.txnVolume)
FROM c
LEFT JOIN b
ON c.id=b.cId
LEFT JOIN a
ON b.id=a.bId
WHERE c.cusId=1 AND c.prodId=1;
SELECT SUM(a.txnVolume) totals
FROM tableA a
INNER JOIN tablB b
ON a.bid = b.id
INNER JOIN tableC c
ON b.cid = c.ID
WHERE c.custID = 1 AND c.prodID = 1

SQL - Get two users' names

I'm trying to get names of users from a voting table, I can't quite figure out how to get both the voter's name and the votee's name.
Simplified Votes Table
ID voteeID voterID
1 1 2
2 1 3
3 1 4
4 2 1
5 2 3
6 2 4
Simplified User Info Table
ID username
1 Bob
2 Sam
3 Ed
4 Mark
Where I'm at:
SELECT votes.voteeID, votes.voterID, userinfo.username
FROM votes
JOIN ???
I was playing around with the join, but I'm kind of stuck. If I join on the votee's id to the userinfo id, how would I also get the voter's name or vice versa?
Database: MySQL 5
Any help or even a hint would be wonderful, thanks in advance.
try this one,
SELECT b.`username` as Votee_Name,
c.`username` as Voter_Name
FROM Votes a
INNER JOIN UserInfo b
ON a.voteeID = b.Id
INNER JOIN UserInfo c
on a.voterID = c.id
SQLFiddle Demo
How about something like
SELECT v.ID,
v.voteeID,
u1.username Votee,
v.voterID,
u2.username Voter
FROM Votes v INNER JOIN
User u1 ON v.voteeID = u1.ID INNER JOIN
User u2 ON v.voterID = u2.ID
You need to take INNER JOIN twice to get usernames for both votee and voter:
SELECT a.voteeID, a.voterID,
b.username AS votee_name, c.username AS voter_name
FROM votes a
INNER JOIN userinfo b
ON a.voteeID = b.ID
INNER JOIN userinfo c
ON a.voterID = c.ID;
SELECT V.ID as ID B.username as Votee_Name, C.username as Voter_Name
FROM Votes V
INNER JOIN UserInfo B ON V.voteeID = B.Id
INNER JOIN UserInfo C on V.voterID = C.id;