![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Hi, there is a view like ID SORT FK 01 -5 N 01 -4 N 01 -3 Y 01 -2 Y 01 -1 N 01 0 Y 01 1 N 01 2 N 02 -2 N 02 -1 Y 02 0 Y 02 1 Y 03 -5 Y 03 -4 N 03 -3 Y 03 -2 N 03 -1 Y 03 0 N 03 1 Y I need the number of seperate groups of 'Y' that means in this case: ID COUNT 01 2 02 1 03 4 ID 01: SORT (-2,-3) and (0) are a group ->2 ID 02: SORT (-1 .. +1) is a group ->1 ID 03: SORT (-5), (-3), (-1), (1) are groups ->4 I sometimes use lead and lag, but in this case I do not have an idea, but I feel it should be possible, isn't it? Thank you Andreas -- wenn email, dann AndreasMosmann <bei> web <punkt> de |
#3
| |||
| |||
|
|
"Andreas Mosmann" <mosmann (AT) expires-31-01-2011 (DOT) news-group.org> a écrit dans le message de news: 1296500556.65 (AT) user (DOT) newsoffice.de... | Hi, there is a view like | | | ID SORT FK | 01 -5 N | 01 -4 N | 01 -3 Y | 01 -2 Y | 01 -1 N | 01 0 Y | 01 1 N | 01 2 N | 02 -2 N | 02 -1 Y | 02 0 Y | 02 1 Y | 03 -5 Y | 03 -4 N | 03 -3 Y | 03 -2 N | 03 -1 Y | 03 0 N | 03 1 Y | | I need the number of seperate groups of 'Y' | that means in this case: | | ID COUNT | 01 2 | 02 1 | 03 4 | | ID 01: SORT (-2,-3) and (0) are a group ->2 | ID 02: SORT (-1 .. +1) is a group ->1 | ID 03: SORT (-5), (-3), (-1), (1) are groups ->4 | | I sometimes use lead and lag, but in this case I do not have an idea, | but I feel it should be possible, isn't it? | | Thank you | Andreas Something like count(case when fk='Y' and nvl(lag(fk) over (partition by id order by sort),'N')='N' then 1 end) with group by id Regards Michel |
#4
| |||
| |||
|
|
to find out, whether this is the beginning ob a new block and after this I made a sum() and group by select ID,sum(IS_MATCH) MyCount from ( select ID,SORT, decode( nvl( lag(fk) over (partition by id order by sort) ),'N')||fk ,'NY',1,0) IS_MATCH from MyView ) group by ID |
#5
| |||
| |||
|
|
On 2011-02-01 16:46, Andreas Mosmann wrote: [...] to find out, whether this is the beginning ob a new block and after this I made a sum() and group by select ID,sum(IS_MATCH) MyCount from ( select ID,SORT, decode( nvl( lag(fk) over (partition by id order by sort) ),'N')||fk ,'NY',1,0) IS_MATCH from MyView ) group by ID For the fun of it, another approach is to enumerate each id group in two ways, per id, and per id and fk: select id, sort, fk, row_number() over (partition by id order by sort) as total_group_order, row_number() over (partition by id, fk order by sort) as partial_group_order from MyView If the difference between total_group_order and partial_group_order is the same it means that they are adjacent, i.e. belongs to the same group. Therefore the number of separate groups of 'Y' is the same as the number of distinct differences: select id, count(distinct total_group_order - partial_group_order) as MyCount from ( select id, sort, fk, row_number() over (partition by id order by sort) as total_group_order, row_number() over (partition by id, fk order by sort) as partial_group_order from MyView ) where fk = 'Y' group by id My guess is that your current solution is more efficient than this. |
|
/Lennart |
#6
| |||
| |||
|
|
Hi, there is a view like |
|
ID SORT FK 01 -5 N 01 -4 N 01 -3 Y 01 -2 Y 01 -1 N 01 0 Y 01 1 N 01 2 N 02 -2 N 02 -1 Y 02 0 Y 02 1 Y 03 -5 Y 03 -4 N 03 -3 Y 03 -2 N 03 -1 Y 03 0 N 03 1 Y I need the number of seperate groups of 'Y' that means in this case: ID COUNT 01 2 02 1 03 4 ID 01: SORT (-2,-3) and (0) are a group ->2 ID 02: SORT (-1 .. +1) is a group ->1 ID 03: SORT (-5), (-3), (-1), (1) are groups ->4 I sometimes use lead and lag, but in this case I do not have an idea, but I feel it should be possible, isn't it? Thank you Andreas |
|
ID COUNT 01 2 02 1 03 4 |
#7
| |||
| |||
|
|
Andreas Mosmann schrieb am 31.01.2011 in 1296500556.65 (AT) user (DOT) newsoffice.de>: Hi, there is a view like ID SORT FK 01 -5 N 01 -4 N 01 -3 Y 01 -2 Y 01 -1 N 01 0 Y 01 1 N 01 2 N 02 -2 N 02 -1 Y 02 0 Y 02 1 Y 03 -5 Y 03 -4 N 03 -3 Y 03 -2 N 03 -1 Y 03 0 N 03 1 Y I need the number of seperate groups of 'Y' that means in this case: ID COUNT 01 2 02 1 03 4 ID 01: SORT (-2,-3) and (0) are a group ->2 ID 02: SORT (-1 .. +1) is a group ->1 ID 03: SORT (-5), (-3), (-1), (1) are groups ->4 I sometimes use lead and lag, but in this case I do not have an idea, but I feel it should be possible, isn't it? Thank you Andreas I solved the question above with your help. Now I have another question to the same subject. Imagine the result a little more complex. Instead of ID COUNT 01 2 02 1 03 4 there is maybe: ID START STOP COUNT 01 0 100 2 01 100 150 2 01 150 375 4 01 375 500 1 01 500 700 1 01 700 900 2 01 900 1015 2 I now want to melt records with the same count, but only that, that follow each other. wished result ID START STOP COUNT 01 0 150 2 01 150 375 4 01 375 700 1 01 700 1015 2 I former got an answer to a similar question by Mr. Maxim Demenko and I tried to change some hours, but I could not resolve my new problem. It is, that f.e. COUNT=2 is twice in the result. So I can not use MAX- Function to find out end of the block. Maxims idea (that fitted for the other question very fine) was Inner query: select ID, START, --to find out the first nvl(decode(lag(START) over (partition by ID, COUNT order by VST),NULL,1,0) FIRST_LINE, --to find out "Stop"- Value max(STOP) over (partition by ID, COUNT), COUNT from MyResult Complete query (simplified): select ID,START,MAX_STOP,COUNT from ( select ID, START, --to find out the first nvl(decode(lag(START) over (partition by ID, COUNT order by VST),NULL,1,0) IS_FIRST_LINE, --to find out "Stop"- Value max(STOP) over (partition by ID, COUNT) MAX_STOP, COUNT from MyResult ) WHERE IS_FIRST_LINE=1 It works for COUNT 1 and 4, but of course not for COUNT 2. Any ideas? |
#8
| |||
| |||
|
|
select id, start, stop, cnt, row_number() over (partition by id order by start) - row_number() over (partition by id, cnt order by start) as diff from T order by start ID START STOP CNT DIFF -- ----------- ----------- ----------- -------------------- 01 0 100 2 0 01 100 150 2 0 01 150 375 4 2 01 375 500 1 3 01 500 700 1 3 01 700 900 2 3 01 900 1000 2 3 min(start) and max(stop) per id, diff gives: select id, min(start) as start, max(stop) as stop, cnt, diff from ( select id, start, stop, cnt, row_number() over (partition by id order by start) - row_number() over (partition by id, cnt order by start) as diff from T ) group by id, cnt, diff order by start ID START STOP CNT DIFF -- ----------- ----------- ----------- -------------------- 01 0 150 2 0 01 150 375 4 2 01 375 700 1 3 01 700 1000 2 3 |
|
I'll have to stop here, if I come up with some other way I might post that later. |
|
/Lennart Andreas |
![]() |
| Thread Tools | |
| Display Modes | |
| |