Mysql query max value corresponding field - mysql

I wanted to get the corresponding field for a max value. So I want to show the actualOffence that has the highest crime count in that borough.
Here is the what i have tried. Im not sure if i am using case properly.
SELECT b.boroughName,
actualOffence( CASE WHEN MAX(c.crimeCount)), (c.crimeCount)
FROM FYP_Borough b
JOIN FYP_Crime c
ON b.boroughID=c.boroughID
JOIN FYP_Offence o
ON c.offenceID=o.offenceID
GROUP BY b.boroughName

You have to get the max crimeCount per boroughname in a subquery and then join accordingly. If I'm understanding your data structure correctly, this should work:
SELECT b.boroughName,
o.actualOffence,
c.crimeCount
FROM (SELECT b2.boroughID, b2.boroughname, max(c2.crimecount) maxcrimecount
FROM FYP_Borough b2
JOIN FYP_Crime c2 ON b2.boroughID=c2.boroughID
GROUP BY b2.boroughID, b2.boroughName
) b JOIN FYP_Crime c ON b.boroughID=c.boroughID AND b.maxcrimecount = c.crimecount
JOIN FYP_Offence o ON c.offenceID=o.offenceID

Related

Unable to get complete data using mysql 3 joins query

i have 1500 records in domains table but by using this query only get 1215 records. How to modify this query to give desired outcome and better performance
SELECT
d.id, d.domain_name, d.action, d.comment, d.agent_email,
d.assigned_date, d.added_date, dd.registered_on, dd.expiry_date,
dd.updated_date, dd.acquire_price, dd.acquire_date, dd.email,
dd.effective_price, dd.registrar, dd.status, dd.servers,
count(l.lead_domain), d.domainer_email, d.current_status,
d.undeveloped, d.sedo, d.afternic, d.flippa, d.uniregistry,
d.go_daddy, d.domr, d.minimum_offer, d.buy_it_now_price,
d.way_to_find_leads, dd.tlds_taken
FROM domains d
right join domains_data dd on d.domain_name=dd.domain_name
left outer join lead_domains l on d.domain_name=l.domain_name
and d.domainer_email=l.domainer_email
group by d.domain_name
having d.action='all' and d.domainer_email='abc#gmail.com'
order by d.added_date desc;
Your results depend on the domains_data table.
Since you are right joining domains to domains_data, the entries that are in domains_data are taken. If you want the entries of domains to be considered, use a left join like below.
SELECT d.id, d.domain_name, d.action, d.comment, d.agent_email, d.assigned_date, d.added_date, dd.registered_on, dd.expiry_date, dd.updated_date, dd.acquire_price, dd.acquire_date, dd.email, dd.effective_price, dd.registrar, dd.status, dd.servers, COUNT(l.lead_domain), d.domainer_email, d.current_status, d.undeveloped, d.sedo, d.afternic, d.flippa, d.uniregistry, d.go_daddy, d.domr, d.minimum_offer, d.buy_it_now_price, d.way_to_find_leads, dd.tlds_taken
FROM domains d
LEFT JOIN domains_data dd ON d.domain_name=dd.domain_name
LEFT OUTER JOIN lead_domains l ON d.domain_name=l.domain_name AND d.domainer_email=l.domainer_email
GROUP BY d.domain_name
HAVING d.action='all' AND d.domainer_email='abc#gmail.com'
ORDER BY d.added_date DESC;
If you are still not able to get the desired results, check the having conditions.
#Harshal As per my understanding, I don't think that query you have provided will work as GROUP BY rules are not followed properly.
SELECT d.id,
d.domain_name,
d.action,
d.comment,
d.agent_email,
d.assigned_date,
d.added_date,
dd.registered_on,
dd.expiry_date,
dd.updated_date,
dd.acquire_price,
dd.acquire_date,
dd.email,
dd.effective_price,
dd.registrar,
dd.status,
dd.servers,
(SELECT COUNT(lead_domain) FROM lead_domains WHERE domain_name = d.domain_name AND domainer_email = d.domainer_email) AS lead_domain,
d.domainer_email,
d.current_status,
d.undeveloped,
d.sedo,
d.afternic,
d.flippa,
d.uniregistry,
d.go_daddy,
d.domr,
d.minimum_offer,
d.buy_it_now_price,
d.way_to_find_leads,
dd.tlds_taken
FROM domains d
LEFT JOIN domains_data dd
ON d.domain_name = dd.domain_name
WHERE d.action = 'all'
AND d.domainer_email = 'abc#gmail.com'
ORDER BY
d.added_date DESC;
And as you have not provided any expected results, I am just guessing that my solution will work.
Here's my suggestion, filter first before joining other tables.
SELECT d.id
,d.domain_name
,d.action
,d.comment
,d.agent_email
,d.assigned_date
,d.added_date
,dd.registered_on
,dd.expiry_date
,dd.updated_date
,dd.acquire_price
,dd.acquire_date
,dd.email
,dd.effective_price
,dd.registrar
,dd.status
,dd.servers
,t1.cnt
,d.domainer_email
,d.current_status
,d.undeveloped
,d.sedo
,d.afternic
,d.flippa
,d.uniregistry
,d.go_daddy
,d.domr
,d.minimum_offer
,d.buy_it_now_price
,d.way_to_find_leads
,dd.tlds_taken
FROM domains d
inner join
(select domain_name, count(1) as cnt
from domain
where
action='all' and domainer_email='abc#gmail.com'
group by domain_name
) as t1 on t1.domain_name = d.domain_name
right join domains_data dd on d.domain_name=dd.domain_name
left outer join lead_domains l on d.domain_name=l.domain_name and d.domainer_email=l.domainer_email
order by d.added_date desc;

SQL query to select from 3-4 tables

Please find below a link to the table-structure I have set up and to the query I am running.
Link to tables and query.
The present result is that only the firstnames, lastnames and "education_finished" are showing. But all the option_id's and their related lang_values still show "NULL".
The desired result:
Any suggestions how to solve?
Below is the query that you are using:
SELECT d.pf_firstname, d.pf_lastname, f.field_id, fl.option_id,
d.pf_education_finished, fl.lang_value
FROM phpbb_profile_fields_data d
LEFT JOIN phpbb_profile_fields f
ON d.pf_education_finished = f.field_name
LEFT JOIN phpbb_profile_fields_lang fl
ON f.field_id = fl.field_id
ORDER BY d.pf_lastname ASC
The reason why you are getting null value is because of this condition:
LEFT JOIN phpbb_profile_fields f
ON d.pf_education_finished = f.field_name
You are trying to join on pf_education_finished (int) field of one table and field_name (int) field of another table. Also, there are no matching values (e.g. pf_education_finished contains numbers whereas field_nameis 'education finished').
If you want the query to return something then you need to join on field_id and phpbb_profile_fields needs to have some records with matching field id, e.g.:
SELECT d.pf_firstname, d.pf_lastname, f.field_id, fl.option_id,
d.pf_education_finished, fl.lang_value
FROM phpbb_profile_fields_data d
LEFT JOIN phpbb_profile_fields f
ON d.pf_education_finished = f.field_id
LEFT JOIN phpbb_profile_fields_lang fl
ON f.field_id = fl.field_id
ORDER BY d.pf_lastname ASC
Here's the updated SQL Fiddle.
SELECT d.pf_firstname, d.pf_lastname, f.field_id, fl.option_id,
d.pf_education_finished, fl.lang_value
FROM phpbb_profile_fields_lang fl
inner JOIN phpbb_profile_fields f
ON f.field_id = fl.field_id
inner JOIN phpbb_profile_fields_data d
ON f.field_id = fl.field_id
ORDER BY d.pf_lastname ASC
This is the optional query if you want to display data from 3-4 tables but in this query names are repeats as per the count of field_id present in phpbb_profile_fields_lang.
The exact solution you are looking is, when you have the same primary key in all the tables from which you are retrieving the data.
Thank you.

mysql max value from field after sum

I want to know what crime is most likely to happen on each month. Right now I can get the max crime count in the data. but i want to sum all the fields that have the same offenceID field for that dateID and then get the max value.
Here is the code I have at the moment:
SELECT d.month, o.actualOffence, c.crimeCount
FROM (SELECT d2.dateID, d2.month, max(c2.crimecount) maxcrimecount
FROM FYP_Date d2 JOIN FYP_Crime c2 ON d2.dateID=c2.dateID
GROUP BY d2.dateID, d2.month)
d JOIN FYP_Crime c ON d.dateID=c.dateID AND d.maxcrimecount = c.crimecount
JOIN FYP_Offence o ON c.offenceID=o.offenceID

Need help updating table from a select query

I have the following code that produces a list of order numbers and values...
SELECT
d.`OrderNo`,
SUM(v.`UnitPrice`)
FROM tblverification v
LEFT JOIN tblorderdetailsafter d ON v.`VMainID` = d.`MainID`
GROUP BY d.`OrderNo`;
I need to update a table called matcontctsafter which has an OrderNo field and currently blank InvoiceAmount column that I need the relative SUM(v.UnitPrice) in.
Can anybody help me construct the UPDATE clause?
UPDATE matcontctsafter m
INNER JOIN (
SELECT
d.`OrderNo`,
SUM(v.`UnitPrice`) InvoiceAmount
FROM tblverification v
LEFT JOIN tblorderdetailsafter d ON v.`VMainID` = d.`MainID`
GROUP BY d.`OrderNo`
) sq ON m.OrderNo = sq.OrderNo
SET m.InvoiceAmount = sq.InvoiceAmount;
UPDATE matcontctsafter m SET m.InvoiceAmount = (SELECT
SUM(v.UnitPrice)
FROM tblverification v
LEFT JOIN tblorderdetailsafter d ON v.VMainID = d.MainID
WHERE m.OrderNo = d.OrderNo);

How to update fields that is the aggregate result of another table in MySQL?

UPDATE a JOIN b ON a.app_id=b.app_id GROUP BY a.app_id SET
remark_avg=AVG(b.score),remark_count=COUNT(b.id);
The above is basically what I want to do,but it's not a valid MySQL statement,how to write it correctly?
UPDATE a
INNER JOIN (SELECT AVG(b.score) avg_score,
COUNT(b.id) cnt_id,
b.app_id
FROM b
GROUP BY b.app_id) x ON x.app_id = a.app_id
SET remark_avg = x.avg_score,
remark_count = x.cnt_id;