MySQL Forums
Forum List  »  Newbie

GROUP BY and ORDER BY
Posted by: Rich Bernstein
Date: May 15, 2021 01:56PM

I hope I can explain this properly

SELECT sr.taxyear,sr.user_email, sr.item, sr. campaign, e.w2_wages
FROM (survey_results sr JOIN employees e ON sr.user_email=e.employee_email )
WHERE sr.item_type='BC' AND e.campaign=sr.campaign AND sr.campaign='Apple'
GROUP BY sr.user_email
ORDER BY sr.date_added

I have a table called survey_results which is just the raw data from surveys. I also have a table called employees which contains the w2_wages of all employees. Both these tables have a field called email; in employees it is called employee_email and in survey_results it is called user_email. They also both have a field called taxyear. What I am trying to do is for each unique combo of user_email and taxyear and campaign in survey results, I'd like to update a field in survey_results called w2_wages with the w2_wages for that employee+taxyear (from employees).

One issue is that if more than one combo of user_email+taxyear appear in survey_results, I only want to use the newest one. That is why I have ORDER BY. I have a field called date_added in survey_results that i am hoping I can use for this.

I only got two records from the select statement above.There should be a few more. I am not really sure my SQL is correct.

Any help is appreciated.

Options: ReplyQuote


Subject
Written By
Posted
GROUP BY and ORDER BY
May 15, 2021 01:56PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.