Question

i have a pretty complicated query to do, i searched for hours and i have not progress in resolving it so i am asking for your help.

Let's say I have the following table:

id      action_id
 1       4
 2       null
 3       6
 4       4
 5       4
 6       null
 7       null
 8       6

I want to get the number of action_id ignoring the repetition in each group surrounded by null values. So the result expected is not :

action_id     count
 4            3
 6            2

but:

action_id     count
 4            2
 6            2

Is there a way to do this task ? Thanks for your help.

Was it helpful?

Solution

Try this query:

SELECT action_id,COUNT(action_id) FROM
table1 WHERE 
ID > (SELECT MIN(ID) FROM table1 WHERE action_id IS NULL) AND
ID < (SELECT MAX(ID) FROM table1 WHERE action_id IS NULL) AND
action_id IS NOT NULL GROUP BY action_id;

SQL Fiddle

OTHER TIPS

select action_id, count(1)
from Table1
where (id<>action_id)
and action_id is not null
group by action_id

SQL Fiddle

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top