some tricky query, is it possible to do this in sql?

Discussion in 'MySQL' started by falcondriver, Oct 14, 2006.

  1. #1
    i have a row with some simple events in it, in my case it could be "red" or "green", yeah i could also call it 1 and 0 but i think its a bit easier to explain this way.
    they are sorted by a timestamp, so i get something out like this in my table:

    green
    green
    green
    red
    green
    red
    red
    green
    green
    red
    green
    red
    ...

    what i want now is a list how much green and red phases i have and how long they are, and it dosnt even matters if this phases are red or green, so for this example i need something like this as result:

    phase length, count

    3, 1 (3x green)
    2, 2 (1x red, 1x green)
    1, 5 (i dont really need this 1-phase results at the end...)

    anyone with a smart idea how to solve this in mysql?
     
    falcondriver, Oct 14, 2006 IP
  2. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #2
    what i want now is a list how much green and red phases i have and how long they are, and it dosnt even matters if this phases are red or green, so for this example i need something like this as result:

    phase length, count

    3, 1 (3x green)
    2, 2 (1x red, 1x green)
    1, 5 (i dont really need this 1-phase results at the end...)



    please can you re-explain what you want in your result...it's not clear to me :)
     
    php-lover, Oct 24, 2006 IP
  3. sdlifecycle

    sdlifecycle Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    It looks like you trying to track repeating trends (red or green), correct? You'll have to use a CURSOR or loop of some sort. This will allow you to increment the 1x count by one each time the current value equals the previous. Make sense? If not, I can try to put something together in SQL. Are you using mySql or Sql Server?
     
    sdlifecycle, Oct 24, 2006 IP
  4. falcondriver

    falcondriver Well-Known Member

    Messages:
    963
    Likes Received:
    47
    Best Answers:
    0
    Trophy Points:
    145
    #4
    yeah, its a kind of trend statistic, in mysql.
     
    falcondriver, Oct 25, 2006 IP
  5. smallbuzz

    smallbuzz Peon

    Messages:
    125
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    There is no single query that can do this for you. You can do this in php. Just loop thru your resultset and store your phase/count in an array.
     
    smallbuzz, Nov 8, 2006 IP
  6. varunkrish

    varunkrish Peon

    Messages:
    107
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    its not possible to do this in atleast mysql4 alone.

    mysql5 its possible with stored procs.

    but if ur using mysql4 alone u must loop thro the result set in php
     
    varunkrish, Nov 9, 2006 IP