sql - How do I use window function to get max price and its id? -
i have query
select adate, factoryid, purchid, itemname, max(price) price tableb catnum = 9 group adate, factoryid, purchid, itemname order adate, factoryid, purchid, itemname
but want id row. in perfect world:
select id, adate, factoryid, purchid, itemname, max(price) price tableb catnum = 9 group adate, factoryid, purchid, itemname order adate, factoryid, purchid, itemname
but know won't work.
so tried this:
select id, adate, factoryid, purchid, itemname, max(price) over(partition adate, factoryid, purchid, itemname) price tableb catnum = 9
that doesn't work. price duplicated ids. , query result set goes 4000 rows 11000.
so obviously, got window function wrong somehow. first did wrong , secondly, of course, how fix it?
select * ( select *, row_number() on (partition adate, factoryid, purchid, itemname order price desc, id desc) rn tableb catnum = 9 ) q rn = 1
Comments
Post a Comment