Skip to content

Instantly share code, notes, and snippets.

@DriesS
Created June 6, 2011 10:35
Show Gist options
  • Save DriesS/1010053 to your computer and use it in GitHub Desktop.
Save DriesS/1010053 to your computer and use it in GitHub Desktop.
Problem with group by on interval
## Output Stock transactions
id source_id inventory_item_id quantity comment author_id created_at updated_at source_type cause
549133 189953 149193 1 55291 "2010-12-16 12:59:23" "2010-12-16 12:59:23" PurchaseItem PURCHASE
586203 203443 149193 11 55291 "2010-12-24 10:15:30" "2010-12-24 10:15:30" PurchaseItem PURCHASE
586413 195123 149193 10 55291 "2010-12-24 10:15:31" "2010-12-24 10:15:31" PurchaseItem PURCHASE
669023 1159453 149193 -1 2943951 "2011-01-13 10:41:42" "2011-01-13 10:41:42" LineItem SALE
765663 1286113 149193 -1 1951411 "2011-02-02 15:21:14" "2011-02-02 15:21:14" LineItem SALE
850583 1388293 149193 -1 4869271 "2011-03-07 08:08:11" "2011-03-07 08:08:11" LineItem SALE
## QUERY TO GET DOUBLES
select st.author_id, st.inventory_item_id, st.created_at, count(*) as total
FROM stock_transactions st
WHERE st.cause = 'PURCHASE'
group by st.author_id, st.inventory_item_id, st.created_at
HAVING total > 1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment