sql - Multiple column condition check with multiple rows to be found -
id b c d e(time) --------------------------- 1 j 1 b 1 2 j 1 s 2 3 m 1 b 1 4 m 1 b 2 5 m 2 s 3 6 m 2 s 4 7 t 1 b 1 8 t 2 s 2 9 t 1 b 3 10 k 1 b 1 11 k 1 b 2
i need find unique values multiple column added condition. unique value combination of col a,b , d.
if col has 2 rows (like record 1 , 2) , column b same on both data , there different value in column d , s coming after b dont want see records
if col has multiple rows (like record 3 6 ) different col b , d, whereas in coulmn d s coming after b dont want see records.
if col has multiple rows (like record 7 9 ) different col b , d, whereas in coulmn d there s before b want see records.
if col has multiple rows (like record 10 11 ) different col b , same column d dont want see records.
any input , able see first , last of using partition , using unbounded in query...
seems basic logic on see if s preceds b on column d show records using partition...
desired output row 7-9: based on logic same column , had sell before buy customer on column d when order column e time.
id b c d e(time) --------------------------------------------------- 7 t 1 b 1 8 t 2 s 2 9 t 1 b 3
i started write query this, ran out of "spare time", criteria hard follow, if comment out "where" @ bottom of query functions doesn't yet produce desired effect.
possibly can lead in direction achive goal ...
with src ( select 1 id, 'j' a, 1 b, 'a' c, 'b' d, 1 e union select 2, 'j', 1, 'a', 's', 2 union select 3, 'm', 1, 'a', 'b', 1 union select 4, 'm', 1, 'a', 'b', 2 union select 5, 'm', 2, 'a', 's', 3 union select 6, 'm', 2, 'a', 's', 4 union select 7, 't', 1, 'a', 'b', 1 union select 8, 't', 2, 'a', 's', 2 union select 9, 't', 1, 'a', 'b', 3 union select 10, 'k', 1, 'a', 'b', 1 union select 11, 'k', 1, 'a', 'b', 2 ), acnt ( select a, count(*) cnt src group ), firstd ( select a, d src e=1 ), firstsrow ( select a, min(e) e src d='s' group ), lastbrow ( select a, max(e) e src d='b' group ), mins ( select a, min(d) d, min(b) b src group ), maxs ( select a, max(d) d, max(b) b src group ) select src.* src join acnt on acnt.a=src.a join firstd on firstd.a=src.a join mins on mins.a=src.a join maxs on maxs.a=src.a left join firstsrow on firstsrow.a=src.a left join lastbrow on lastbrow.a=src.a not (acnt.cnt=2 , mins.b=maxs.b , mins.d<>maxs.d , firstsrow.e < lastbrow.e) , not (acnt.cnt>=3 , mins.b<>maxs.b , mins.d<>maxs.d , firstd.d='b') , (acnt.cnt>=3 , mins.b<>maxs.b , mins.d<>maxs.d , firstd.d='b')
Comments
Post a Comment