Complex select

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ville Vahtera

    #1

    Complex select

    Hi,

    This is something I need to do with one select. Here is my select:

    SELECT SUM(value1) FROM mytable WHERE rec_time BETWEEN '00:00:00' AND
    '00:30:00' AND YEAR(rec_date) = YEAR(NOW()) GROUP BY MONTH(rec_date)

    or

    SELECT SUM(value1) FROM mytable WHERE rec_time BETWEEN '00:00:00' AND
    '00:30:00' AND YEAR(rec_date) = YEAR(NOW()) GROUP BY LEFT(rec_date,7 )

    rec_time = time
    rec_date = date

    as you can see, I'm collecting summary of daily record around midnight and
    building an monthly array like: 0,0,0,6.5,0,23. 7,2,0,0,5.5,0,0
    BUT! my function call requires a constant-length list. So if any months in
    my selection range have no data, I need to fill the space with an empy
    value. How can I do this? Perhaps with an helper table, but how?

    Thank in advance!

    Best regards,
    -Ville Vahtera, Finland


  • Bill Karwin

    #2
    Re: Complex select

    Ville Vahtera wrote:[color=blue]
    > if any months in
    > my selection range have no data, I need to fill the space with an empy
    > value. How can I do this? Perhaps with an helper table, but how?[/color]

    Fill the helper table with every month value between the least rec_date
    and the greatest rec_date. Then join the helper table to your `mytable`
    using an outer join:

    SELECT SUM(...)
    FROM mytable RIGHT OUTER JOIN monthTable
    ON YEAR(mytable.re c_date) = YEAR(monthTable .rec_date)
    AND MONTH(mytable.r ec_date) = MONTH(monthTabl e.rec_date)
    WHERE ...
    GROUP BY YEAR(monthTable .rec_date), MONTH(monthTabl e.rec_date)

    Regards,
    Bill K.

    Comment

    • Ville Vahtera

      #3
      Re: Complex select


      "Bill Karwin" <bill@karwin.co m> kirjoitti
      viestissä:di6nc b02kb4@enews2.n ewsguy.com...[color=blue]
      > Ville Vahtera wrote:[color=green]
      >> if any months in my selection range have no data, I need to fill the
      >> space with an empy value. How can I do this? Perhaps with an helper
      >> table, but how?[/color]
      >
      > Fill the helper table with every month value between the least rec_date
      > and the greatest rec_date. Then join the helper table to your `mytable`
      > using an outer join:
      >
      > SELECT SUM(...)
      > FROM mytable RIGHT OUTER JOIN monthTable
      > ON YEAR(mytable.re c_date) = YEAR(monthTable .rec_date)
      > AND MONTH(mytable.r ec_date) = MONTH(monthTabl e.rec_date)
      > WHERE ...
      > GROUP BY YEAR(monthTable .rec_date), MONTH(monthTabl e.rec_date)[/color]

      thanx, I got it:

      SELECT
      IFNULL(SUM(myta ble.value1), 0)
      FROM one2twelve
      LEFT JOIN mytable ON
      MONTH(mytable.r ec_date) = one2twelve.i AND
      mytable.rec_tim e BETWEEN '00:00:00' AND '00:30:00' AND
      YEAR(rec_date) = YEAR(NOW())
      GROUP BY one2twelve.i;

      Regards,
      -Ville


      Comment

      Working...