Hey stackoverflow community,
I have a table of Sales
, hypothetical shown below.
Customer Revenue State Date
David $100 NY 2016-01-01
David $500 NJ 2016-01-03
Fred $200 CA 2016-01-01
Fred $200 CA 2016-01-02
I'm writing a simple query of revenue generated by customer. The output returns as such:
David $600
Fred $400
What I want to do now is add the row for the latest purchase date along with the state associated with the latest purchase.
Desired result:
David $600 2016-01-03 NJ
Fred $400 2016-01-02 CA
I would like to keep the SQL code as clean as possible. I also want to avoid doing a JOIN to a new query as this query can start to get complex. Any ideas as to how to do so?