tags:

views:

21

answers:

1

I have a table with separate columns for months, days and a varchar column for 6 hour increments for each day ('12AM', '6AM', '12PM', '6PM'). There's also a column that's supposed to have calculated numeric values for each of those 6 hour increments. These calculated values come from some reference table. This reference table contains values for each day for several months broken down by hour where each hour has its own column. So, basically, I have to add the values for each 6 hour increment. I have no idea how to associate the correct values in the reference table to those 6 hour increments.

I will really appreciate any help on this.

A: 

You could try something like:

UPDATE main_table, reference_table
  SET main_table.calc_column = 
    (CASE WHEN main_table.incr = "6AM" THEN reference_table.col1+reference_table.col2+...
          WHEN main_table.incr = "12AM" THEN reference_table.col7+reference_table.col8+...
          WHEN main_table.incr = "6PM" THEN reference_table.col13+reference_table.col14+...
          ELSE reference_table.col19+reference_table.col20+...
     END)
  WHERE main_table.month = reference_table.month
    AND main_table.day = reference_table.day
VeeArr
Thank you so much for this suggestion. It seems like it should work. However, I get an error in the WHERE part of the query. The thing is that my reference table doesn't have a month and day columns. It just has a date column called thedate. So, I tried the following syntaxWHERE main_table.month = extract(month from reference_table.thedate)AND main_table.day = extract(day from reference_table.thedate)Just to save you the trouble, I know that this is the correct syntax for the extract() function. I've used it on reference_table many times before. So, what's the problem here?
UkraineTrain
I know that this problem could get fixed if I add month and day columns to the reference_table, but is there a way of making it work without adding the columns?
UkraineTrain
What error do you get?
VeeArr
Error 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax near ') where tb.month_number = extract(month from Electrical_Usage.thedate) and tb.theday = extract(day from Electrical_Usage.thedate)
UkraineTrain
I missed the END for the CASE expression. Updated.
VeeArr
Thanks a lot, man. You are the best. It works like a charm.
UkraineTrain
If you found my answer useful, please accept it as the answer to your question. Thanks.
VeeArr