Joining 3 Tables - mysql

I have 3 tables (CompanyProfile, IndustryTable, MainTable)
CompanyProfile
CompanyCode CompanyName IndustryCode
AAAA Company A 3
BBBB Company B 1
CCCC Company C 4
DDDD Company D 1
EEEE Company E 1
GGGG Company F 2
IndustryTable
IndustryCode IndustryName status
1 Manufacturing ACTIVE
2 Sales ACTIVE
3 Logistics ACTIVE
4 Energy DEACTIVATED
MainTable
CompanyCode field2
AAAAA SampleRecord1
AAAAA SampleRecord2
DDDDD SampleRecord3
CCCCC SampleRecord4
EEEEE SampleRecord5
Now I need a Query to Get all the RecordCount from MainTable Group By IndustryTable.IndustryCode (ACTIVE Only), So the above example should give the following output
IndustryCode IndustryName RecordCount (Explanation)
1 Manufacturing 2 -Record 3 and 5
2 Sales 0
3 Logistics 2 -Record 1 and 2
Record 4 (CCCCC) will not be show since IndustryTable.IndustryCode 4 is DEACTIVATED

Something like
select industrycode, industryname, count(*) as total from companyprofile t1 join industry table t2 join maintable t3 where t1.companycode=t3.companycode and t1.industrycode=t2.industrycode and t2.status="ACTIVE" group by industrycode
?

Related

count and sum function not working in left join

I have the following 3 tables and i would like to know the correct sql for the expected result as below.
my sql here is not working;
select h.pid,
h.name,
sum(r.amount1) as total1,
sum(r.amount2) as total2,
count(g.pid) as times,
sum(g.take) as totaltaken
from history h
left join rpt_revenue r on h.pid=r.pid
left join guest g on g.pid=r.pid
group by h.pid, h.name;
history
pid name
1 peter
2 may
rpt_revenue
id pid amount1 amount2
1 1 10.00 11.00
2 2 20.00 20.00
3 1 2.00 2.00
4 2 2.00 2.00
guest
gid pid id take
1 1 1 2
2 1 3 2
3 2 2 3
expected result
pid total1 total2 times totaltaken
1 12.00 13.00 2 4
2 22.00 22.00 1 3
So to be able to use aggregate function over join, you should first aggregate your data in a join subquery and then aggregate all of them at the top level
here some examples of aggregation

Putting a table result as a text column

I have two tables, the first one is a table of companies, and the second one is a table of shareholders we could say. The shareholder table can have upwards of a thousand shareholders.
I need to make a view/another table which has every shareholder name on a single text column along with the unique identifier of the company, the name and "fake" name of the company.
Table 1 Company:
ID CompanyID Name FantasyName
1 1 A Company 1
2 2 B Company 2
3 3 C Company 3
4 4 D Company 4
5 5 E Company 5
6 6 F Company 6
7 7 G Company 7
Table 2 Shareholders:
ID CompanyID Name
1 1 John 1
2 1 Peter 2
3 1 Gabriel Li
4 2 Raphael 3
5 2 Anderson 4
6 2 Michael 6
7 2 Angelina Jo
8 3 John 8
9 4 Beatrice
10 4 Scarlet
11 5 Scarlet
12 5 Logan
13 5 John 1
I tried to do this via Linq through C# but it hasn't been fast enough for my purpose, this table contains upwards of millions of companies.
The end result would look like this:
Table 3 Company and Shareholders:
ID CompanyID Name FantasyName Shareholders
1 1 A Company 1 John 1,Peter 2,Gabriel Li
2 2 B Company 2 Raphael 3, Anderson 4,Michael 6,Angelina Jo
3 3 C Company 3 John 8
4 4 D Company 4 Beatrice,Scarlet
5 5 E Company 5 Scarlet,Logan,John 1
...
All shareholders in a single TEXT field, with the company info accompanying it.
You can use aggregation and GROUP_CONCAT():
CREATE VIEW myview AS
SELECT
c.ID,
c.CompanyID,
c.Name,
c.FantasyName,
GROUP_CONCAT(s.Name) Shareholders
FROM
Company c
INNER JOIN Shareholders s ON s.CompanyID = c.CompanyID
GROUP BY
c.ID,
c.CompanyID,
c.Name,
c.FantasyName

Outer join multiple table

I have 4 table that I want to join:
tbl_transaksi_header
--------------------
kode_transaksi
kode_user
nama_penerima
email_penerima
alamat_penerima
bank
telpon
tbl_konfirmasi
--------------------
id
kode
member
namarekening
tbl_user
-------------------
kode_user
username_user
tbl_transaksi_detail
---------------------
kode_transaksi
harga
jumlah
status
I want to show all data including NULL in nama_rek.
I tried this:
SELECT t.status, t.kode_transaksi kode, u.username_user nama_user, t.nama_penerima penerima, t.telpon, t.bank, sum(d.harga*d.jumlah) total,k.namarekening nama_rek
FROM tbl_transaksi_header t
JOIN tbl_user u
ON t.kode_user=u.kode_user
JOIN tbl_transaksi_detail d
ON t.kode_transaksi=d.kode_transaksi
LEFT OUTER JOIN tbl_konfirmasi k
ON t.kode_transaksi=k.kode
Work fine but not showing null result, only showing 1 row result. I want something like this:
status kode nama_user penerima telpn bank total nama_rek
xxx xxx xxx xxx xxx xxx xxx NULL
xxx xxx xxx xxx xxx xxx xxx NULL
xxx xxx xxx xxx xxx xxx xxx yyy
xxx xxx xxx xxx xxx xxx xxx NULL
Sample data:
kode_transaksi kode_user nama_penerima email_penerima alamat_penerima bank telpon
kd1 1 john some#email.com florida Bank A 088833221
kd2 2 elsa some#email.com uk Bank B 088833222
kd3 1 roy some#email.com manhattan Bank C 088833223
id kode member namarekening
1 kd1 paul paul
kode_user username_user
1 paul
2 elena
kode_transaksi harga jumlah status
kd1 10 2 process
kd1 5 2 process
kd1 5 1 process
kd2 4 3 pending
kd2 3 4 pending
kd3 2 3 pending
I want this output:
status kode nama_user penerima telpon bank total nama_rek
process kd1 paul john 088833221 Bank A 35 paul
pending kd2 elena elsa 088833222 Bank B 24 NULL
pending kd2 elena roy 088833223 Bank C 6 NULL
Your join is fine; the problem is that you have a SUM() in your select with no GROUP BY. Try this:
SELECT t.kode_transaksi kode, u.username_user nama_user, t.nama_penerima penerima, t.telpon, t.bank, sum(d.harga*d.jumlah) total,k.namarekening nama_rek
FROM tbl_transaksi_header t
JOIN tbl_user u
ON t.kode_user=u.kode_user
JOIN tbl_transaksi_detail d
ON t.kode_transaksi=d.kode_transaksi
LEFT OUTER JOIN tbl_konfirmasi k
ON t.kode_transaksi=k.kode
GROUP BY
t.kode_transaksi, u.username_user, t.nama_penerima , t.telpon, t.bank,k.namarekening

Is it possible write this query without cursor

I am using mysql database
The schema of department is
Department Table
Deptid DeptName
1 CEO
2 HR
3 IT
4 Dev
5 QA
Employee table
Empid EmpName managerid deptid
1 E1 1
2 E2 1 2
3 E3 1 3
4 E4 3 4
5 E5 4 4
6 E6 4 4
7 E7 3 5
8 E8 7 5
9 E9 7 5
I need output as
deptid parentdept count
1 1
2 1 1
3 1 1
4 3 3
5 3 3
Parent department is department which belongs to manager of employee.
It means It department CEO is parent department of IT and HR because IT and HR managers report to CEO dept manager.
It means It department IT is parent department of Dev and QA because DEV and QA managers report to IT dept manager.
deptid parentdept count
CEO 1
HR CEO 1
IT CEO 1
DEV IT 3
QA IT 3
Tree representation of it is
CEO-(deptid 1)E1
HR-(deptid 2)E2 IT-(deptid 3)E3
DEV-(deptid 4)E4 QA(deptid 5)E7
E5-(deptid 4) E6-(deptid 4) E8-(deptid 5) E9-(deptid 5)
I thought the managerid shoudn't be equal to the any empid in the same department rows by your descriptions.
select A.deptid,B.parentdept,A.count from (select deptid,
count(*) as count from Employee group by deptid) as A
left join
(select t1.deptid, t3.deptid as parentdept from Employee t1,Employee t3 where t1.managerid not
in (select Empid from Employee t2 where t2.deptid=t1.deptid)
and t1.managerid=t3.empid) as B
on A.deptid=B.deptid;
if I missed something in the logic, apologies in advance.

table joins with multiple group_concat

I have a problem regarding joining tables with group_concat. Here are the details.
table_orders:
item_cd order_id descs quantity status seq_no
1 100 coca-cola 2 A 232
2 100 pizza 1 A 233
3 101 cheeseburger 5 A 234
4 102 pepsi 4 A 235
4
table_instructions:
item_cd instruction
3 more cheese
3 less vegetable
cancelled_item_table:
quantity seq_no
1 234
1 234
1 235
Now what I want to achieve is like this:
item_cd descs quantity instructions cancelled_item
1 coca-cola 2 - -
2 pizza 1 - -
3 cheeseburger 2 more cheese, less vegetable 1,1
4 pepsi 4 - 1
This is my current query:
SELECT
ord.item_cd,
ord.order_id,
ord.descs,
ord.quantity,
GROUP_CONCAT(x.quantity) as cancelled,
GROUP_CONCAT(i.instruction) as instruct
FROM table_orders ord
LEFT JOIN cancelled_item_table x ON ord.seq_no = x.seq_no
LEFT JOIN table_instructions i ON ord.item_cd = i.item_cd
WHERE ord.status = 'A'
GROUP BY ord.order_id
and here is the output:
item_cd descs quantity instructions cancelled_item
1 coca-cola 2 - 1
2 pizza 1 - 1
3 cheeseburger 2 more cheese, more cheese,
less vegetable, less vegetable 1,1,1,1
4 pepsi 4 - 1
If you notice, cheeseburger has 2 cancelled item and 2 instruction, but the output is 4, looks like it's multiplying.
Since the join with cancelled_item_table multiplies rows, you have to join to an already grouped subquery, like this:
SELECT
ord.item_cd,
ord.order_id,
ord.descs,
ord.quantity - coalesce(x.tot,0) as quantity,
GROUP_CONCAT(i.instruction) as instruct,
x.cancelled
FROM
table_orders ord LEFT JOIN table_instructions i
ON ord.item_cd = i.item_cd LEFT JOIN
(select seq_no, count(*) as tot, GROUP_CONCAT(quantity) as cancelled
from cancelled_item_table
group by seq_no) x ON ord.seq_no = x.seq_no
WHERE ord.status = 'A'
GROUP BY ord.item_cd, ord.order_id, ord.descs, quantity