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

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -