Created
October 2, 2009 21:30
-
-
Save jc00ke/200163 to your computer and use it in GitHub Desktop.
member select with max/group by
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| members = db[:customer]. | |
| select( :customer__id, | |
| :customer__firstname, | |
| :customer__lastname, | |
| :customer__emailaddress, | |
| :customer__address, | |
| :customer__city, | |
| :customer__state, | |
| :customer__zip, | |
| :customer__customdate1___expirationdate, | |
| :transaction__time___purchasedate, | |
| [[153, 'child'], [154, 'senior'], [588, 'fitness'], [589, 'fitness']].case('annual', :item__id).as(:membershiptype) | |
| ). | |
| join(:transaction, :customer__id => :transaction__customerid). | |
| join(:transactionentry, :transaction__transactionnumber => :transactionentry__transactionnumber). | |
| join(:item, :transactionentry__itemid => :item__id). | |
| filter(:transactionentry__itemid => [157, 152, 153, 668, 261, 156, 692, 231, 348, 154, 544, 545, 588, 589, 329, 158, 627, 161, 628, 232, 315, 160, 263, 330]). | |
| filter(:transaction__time => (Date.today - 365)..(Date.today - 335)). | |
| exclude(:customer__id => logged_members). | |
| group( :customer__id, | |
| :customer__firstname, | |
| :customer__lastname, | |
| :customer__emailaddress, | |
| :customer__address, | |
| :customer__city, | |
| :customer__state, | |
| :customer__zip, | |
| :customer__customdate1___expirationdate, | |
| :transaction__time___purchasedate, | |
| :item__id | |
| ). | |
| max(:transaction__time___purchasedate). | |
| distinct(:customer__id) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment