SAP HANA SQL - Return max date and related attributes, but ignore other records -


the problem facing trying query sap hana bring list of unique codes refer 1 instance of change being made database. bit of background below image, each change has relevant site id , product no. using variables, in order find out ts number recent date.

however, when use select max(datab) function, forces me use agroup by clause. but, because cannot omit ts number group by clause, returns three.

is there way max date, given combination of product no. , site id, , return ts number date? in example, fine use top 1 scaled-down example query @ many combinations of product no. , site id (with desired outcome being list of of ts numbers relate recent change product/store combination, use join query).

any appreciated. if full table design etc. required people can attempt replicate problem happily provide hoping there's simple solution have not thought of...

many thanks

as in other sql-db supports window functions, can use row_number() or rank() function desired result. 1 use depends on how want handle tie values.

if want 1 ts-number in case there more 1 ts-number same maxdate, use following sql:

select dat, ts_nr, pr_nr, site     (select *, row_number() on ( partition pr_nr, site order dat desc ) rownum        mytab   )     rownum = 1; 

be aware, result non-deterministic. however, can (should in cases!) make deterministic adding ts_nr order in window order clause. either highest or lowest ts-number same maxdate, depending on sort order.

if want ts-numbers in case there several ts-numbers same maxdate, use rank() instead of row_number(), this:

select dat, ts_nr, pr_nr, site     (select *, rank() on ( partition pr_nr, site order dat desc ) ranknum       mytab   )     ranknum = 1; 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -