I have the following schemas:
sailor: ssn, fname, lname, address
reservation: sailor_ssn, date_reserved, boat_reg#
boat: reg#, bname, color, model#
I want to list the names of all maximal reservers. A sailor is a maximal reserver if the set of boat(s) he has reserved is not a proper subset of any other sailors.
Thanks in advance
I came up with the following query:
SELECT p.fname, p.lname
FROM sail.person p
JOIN sail.sailor s ON p.ssn = s.ssn
JOIN
(SELECT DISTINCT r4.sailor_ssn
FROM sail.reservation r4
WHERE r4.sailor_ssn NOT IN
(SELECT T3.sailor_ssn
FROM
(SELECT *
FROM
(SELECT r1.sailor_ssn, r2.sailor_ssn sssn, count(*) ctr1
FROM sail.reservation r1
JOIN sail.reservation r2 ON r1.boat_reg# = r2.boat_reg#
AND r1.sailor_ssn != r2.sailor_ssn
GROUP BY r1.sailor_ssn, r2.sailor_ssn) T1
JOIN
(SELECT r3.sailor_ssn rssn, count(*) ctr2
FROM sail.reservation r3
GROUP BY r3.sailor_ssn) T2
ON T1.sailor_ssn = T2.rssn) T3
where T3.ctr1 = T3.ctr2)) T4
ON T4.sailor_ssn = s.ssn;
Is there any better solution?
Related
I have 3 table
tmp (pid,title,price)
tmp_studyarea(areaid, tittle, tm_pid)
tmp_module(mid,title, duration, areaid)
I am trying to write a query where I can obtain (tmp.pid, tmp.title, tmp.price, SUM(tmp_module.duration Where tmp_module.areaid = tmpstudyarea.areaid and tmp_studyarea.tmp_pid = tmp.pid) Group by tmp.pid
Here is a query I wrote and i'm unable to get expected results. Help please
SELECT s.title, s.pid, SUM(duration) IN (SELECT a.tmpid, a.areaid, a.title, SUM(m.duration) as duration FROM tmp_studyarea AS a, tmp, tmp_module as m WHERE m.areaid = a.areaid AND a.tmpid = s.pid GROUP BY a.areaid) FROM tmp as s;
Here is my expected resultst
title
pid
duration
tmp Title 1
1
3000
tmp Title 3
4
1000
EDIT
Found a solution
SELECT DISTINCT s.title,s.pid, (SELECT SUM(m.duration) as duration FROM tmp_studyarea AS a, tmp_module as m WHERE m.areaid = a.areaid AND a.tmpid = s.pid GROUP BY a.tmpid) as duration FROM tmp as s, tmp_studyarea, tmp_module GROUP by pid
You must join the 3 tables properly and aggregate:
SELECT t.pid, t.title, t.price,
SUM(m.duration) total_duration
FROM tmp t
INNER JOIN tmp_studyarea s ON s.tmp_pid = t.pid
INNER JOIN tmp_module m ON m.areaid = s.areaid
GROUP BY t.pid, t.title, t.price
Someone please explain the difference between parent query and sub query? How do I understand?
e.g:
SELECT
ra.ID id,
ra.ACCOUNT_ID accountId,
ef.REAL_NAME realName,
a.MOBILE mobile,
sps.`NAME` siteName,
ef.CREATE_TIME createTime,
a.`STATUS` status,
ra.`STATUS` siteStatus,
(
SELECT
aif.APPROVER_NAME
FROM
audit_info aif
WHERE
aif.TARGET_ID = a.ID AND af.AUDIT_TYPE=2
AND aif.CREATE_TIME = (
SELECT
MAX(af.CREATE_TIME)
FROM
audit_info af
WHERE
af.TARGET_ID = a.ID AND af.AUDIT_TYPE=2
)
) AS approverName
FROM
account a
INNER JOIN site_relation_account ra ON ra.ACCOUNT_ID = a.ID
INNER JOIN account_ext ef ON ra.ACCOUNT_ID = ef.ACCOUNT_ID
INNER JOIN service_provider_site sps ON sps.ID = ra.SITE_ID
LEFT JOIN audit_info af ON af.TARGET_ID = ef.ACCOUNT_ID
WHERE ra.ACCOUNT_TYPE = 2
SELECT SUM(commission) as regularincome FROM `tbl_member_commission` where mem_id=2 AND MONTH(cdate) = MONTH(CURRENT_DATE())
SELECT SUM(commission) as crowdfund FROM `tbl_member_comm_month` where mem_id=2 AND MONTH(cdate) = MONTH(CURRENT_DATE())
note:- both of tables have these same column names : commission, mem_id, cdate
If this is the only record in each subquery, you can use CROSS JOIN:
select a.regularincome, b.crowdfund
FROM
(SELECT SUM(commission) as regularincome FROM `tbl_member_commission` where mem_id=2 AND MONTH(cdate) = MONTH(CURRENT_DATE())) as a
cross join
(SELECT SUM(commission) as crowdfund FROM `tbl_member_comm_month` where mem_id=2 AND MONTH(cdate) = MONTH(CURRENT_DATE())) as b
SELECT e.qty * e.unit_cost * e.unit_no * (e.factor/100) AS Ext_price, c.cost_type, c.cost_dt, c.internal_cost_amt, c.client_cost_amt
FROM equipment e
LEFT JOIN CostAllocation c ON (c.object_row_id = e.row_id)
I now want to divide the Ext_price by the number of CostAllocation rows that match the equipment row. How would I do that?
SELECT e.qty*e.unit_cost*e.unit_no*e.factor/(100*x.cnt) AS Ext_price,
c.cost_type,
c.cost_dt,
c.internal_cost_amt,
c.client_cost_amt
FROM equipment e
LEFT JOIN CostAllocation c ON c.object_row_id = e.row_id
LEFT JOIN
(SELECT row_id,
count(*) cnt
FROM CostAllocation
GROUP BY row_id) x ON c.object_row_id = x.row_id
try below:
select g.*,g.Ext_price/(select count(*) from CostAllocation k where k.object_row_id=g.row_id) as youranswer
from
(
SELECT e.row_id,e.qty*e.unit_cost*e.unit_no*e.factor/100 AS Ext_price, c.cost_type, c.cost_dt, c.internal_cost_amt, c.client_cost_amt
FROM equipment e
LEFT JOIN CostAllocation c ON c.object_row_id = e.row_id)g
Since mysql does not support analytical functions it is probably easiest to add a subquery (untested):
SELECT Ext_price, cost_type, cost_dt, internal_cost_amt
, client_cost_amt
, Ext_price / ( nullif( (select count(1)
from CostAllocation c2
where c2.object_row_id = e.row_id), 0) )
FROM (
SELECT e.qty*e.unit_cost*e.unit_no*e.factor/100 AS Ext_price
, c.cost_type, c.cost_dt, c.internal_cost_amt, c.client_cost_amt
FROM equipment e
LEFT JOIN CostAllocation c
ON c.object_row_id = e.row_id
) as T
I added nullif to avoid division by zero.
I have query like this ::
SELECT account.AccountNumber, account.NAME, Sum(agro.price * agro.qty) AS Expr1
FROM ((account
INNER JOIN data ON account.AccountNumber = data.acno)
INNER JOIN agro ON agro.BillNo = data.BillNo)
WHERE data.db='true'
GROUP BY account.AccountNumber, account.NAME;
I want to deduct another groupby query output in to Sum(agro.price * agro.qty) this
the another group by query is SELECT Sum(rs),acno
FROM jma group by acno;
i want to deduct Sum(agro.price * agro.qty)-Sum(rs) how its work please help me solve this
If I am understanding you correctly the following query may work for you:
SELECT subQ.AccountNumber, subQ.NAME, (subQ.subSum - jmaSum.jSum) AS FinalSum
FROM
(
SELECT a.AccountNumber, a.NAME, Sum(ag.price * ag.qty) AS subSum
FROM (account AS a
INNER JOIN data AS d ON a.AccountNumber = d.acno)
INNER JOIN agro AS ag ON ag.BillNo = d.BillNo
WHERE d.db = 'true'
GROUP BY a.AccountNumber, a.NAME
) AS subQ
LEFT JOIN
(
SELECT Sum(j.rs) AS jSum, j.acno
FROM jma AS j
GROUP BY j.acno
) AS jmaSum ON subQ.AccountNumber = jmaSum.acno