Vincent Turner :
Wrote a query returns the most recently updated record for each item for each day, in other words, Item 444222 gets 12 records each day and the query pulls the most updated one. Sometimes the records are inserted the day before, of, and after. When the query is run in workbench, I get the correct value, but in Excel PowerQuery it returns the earliest record. Is there a coding error on my part, or is it something else? Any help with this would be appreciated. record_type: varchar, date: date, item_number: int, cycle_id: int, scheduled_cap: int, insert_date: timestamp.
Query attached below.
Select record_type, date, item_number, cycle_id, scheduled_cap, max(insert_date)
from db.all_cycles
Where date >= '2016-01-01'
AND record_tyoe = 'I'
AND cycle_id NOT IN (40, 38, 39, 41, 53, 185)
AND item_number IN ('94850', '94858', '452917')
group by item_number, date, record_type
order by date desc;
Gordon Linoff :
You should use filtering not aggregation:
select ac.*
from db.all_cycles ac
where ac.date >= '2016-01-01' and
ac.record_tyoe = 'I' and
ac.cycle_id not in (40, 38, 39, 41, 53, 185) and
ac.item_number in ('94850', '94858', '452917') and
ac.insert_date = (select max(ac2.insert_date)
from db.all_cycles ac2
where ac2.date = ac.date and
ac2.item_number = ac.item_number and
ac2.record_tyoe = ac.record_type
)
order by date desc;
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=386280&siteId=1