0

SO.

I have a mySql table called 'houses'. The data I pull into houses comes in a format where there is a unique identifier (MLS) and each line item comes in with 'listoffice' and 'selloffice' as fields, too.

I can group and sum by either list office or selloffice but can I do a single group by / sum by using both fields.

E.g.: select count(mls) from house group by listoffice and selloffice order by count desc?

The intent is a list of sales that combines listoffice and selloffice where they are the same.

Edit: my intent is misstated. I am looking for an aggregrate list that combines the listoffice and selloffice count where they are the same. Where it counts the listoffice and selloffice instances, adds them, then produces a list of the ranked offices.

2
  • 1
    group by listoffice, selloffice not and. Commented Jul 13, 2019 at 18:48
  • @forpas, that answers the original question as stated. Thank you! Commented Jul 13, 2019 at 18:57

1 Answer 1

1

I think you want to unpivot the data and aggregate:

select office, sum(islist) as numlists, sum(issell) as numsells,
       sum(islist + issell) as numtotal
from ((select listoffice as office, 1 as islist, 0 as issell
       from house
      ) union all
      (select selloffice, 0, 1
       from house
      )
     ) sl
group by office
order by numtotal desc;
Sign up to request clarification or add additional context in comments.

3 Comments

this is EXACTLY my intent. Thank you!
I've been experimenting with this for a bit today. Where would you put a conditional block in here? E.g., "where closed_date >= '2019-01-01'
@RobThompson . . . Add it to each of the subqueries.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.