database - Is there an SQL query that will count the number of occurrences of an event WITHOUT grouping the data by the event being counted? -


note i'm doing in ms access, solution using basic sql operators appreciated.

suppose have table each row represents coin flip in series of coin flips.

disclaimer: i'm using coin flips analogy don't have explain actual data set.

select * coinflips;  id    flip     time ------------------- 1     heads      1 2     tails      2 3     heads      3 4     heads      4   5     heads      5 6     tails      6 

how write query returns of rows above additional column counts number of 'head' flips occurred row's occurrence. in other words, want result like:

desired output

id    flip     time   numheads -------------------------------- 1     heads      1       1 2     tails      2       1 3     heads      3       2 4     heads      4       3 5     heads      5       4 6     tails      6       4 

to in ms access, need correlated subquery or join/aggregation. other databases have direct support functionality, not ms access.

select cf.*,        (select count(*)         coinflips cf2         cf2.flip = 'heads' , cf2.id <= cf.id        ) numheads coinflips cf; 

Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -