Solved

trying to set a different value for column using case statement

Posted on 2013-12-29
1
334 Views
Last Modified: 2013-12-29
I am trying to set the value of cooling hours on first row IF the completed date is before the startdate
 sample data

row Num      completed      next_completed      cooling Hours
9      11/25/2013 11:21:15 PM      11/27/2013 1:05:45 AM      25.7417
10      11/27/2013 1:05:45 AM      11/29/2013 11:18:34 PM      (null)
11      11/29/2013 11:18:34 PM      11/30/2013 7:37:45 PM      20.3197
12      11/30/2013 7:37:45 PM      11/30/2013 11:36:57 PM      (null)
13      11/30/2013 11:36:57 PM      12/1/2013 6:52:19 PM      19.2561

in the example dates above coolingHours is = time difference between next_Completed and completed for each row
however for ROW 9 I need to set coolingHours to time difference between next_completed and startDate - all other rows stay the same

startDate is a variable passed into the storedProcedure (for this example it would be 11///2013)

I tried the code below however it returned only the min(rowNum) and it did not effect the coolingHours

    SELECT   rowNum, 
       l.logId,l.completed,l.next_completed,l.first_completed,
        isError,charge,totalTimeRange,startingDate,endingDate,
         CASE
            WHEN completed < ''',startDate,''' AND rowNum = min(rowNum)
                THEN time_to_sec(timediff(l.next_completed, ''',startDate,''')) / 3600
                ELSE coolingHours
            END AS coolingHours,
        databaseName,i.name,l.itemId, kwDifference, cost, costT,l.details,timeInSeconds
      FROM tempTable3 l
       inner join items i ON l.logId = i.logId AND i.name LIKE ''%KW PRE%'' 
       WHERE l.itemId = ''',myItemId,''' 
              AND completed BETWEEN ''', startDate, ''' AND ''', endDate, '''

Open in new window


I am open to suggestions at this point!
0
Comment
Question by:portlight
1 Comment
 

Accepted Solution

by:
portlight earned 0 total points
ID: 39745094
Adding the below code fixed the issue

   set @newcoolingHours = (SELECT time_to_sec(timediff(next_completed, startDateOriginal)) / 3600 AS newCoolingHours 
        FROM tempTable3 WHERE completed > startDateOriginal AND completed BETWEEN startDate AND endDate LIMIT 1);
   SET xHours = @newCoolingHours;

Open in new window


full code:
 set @newcoolingHours = (SELECT time_to_sec(timediff(next_completed, startDateOriginal)) / 3600 AS newCoolingHours 
        FROM tempTable3 WHERE completed > startDateOriginal AND completed BETWEEN startDate AND endDate LIMIT 1);
   SET xHours = @newCoolingHours;

  SET @sqlTemp = NULL;
  SET @sqlTemp = 
   CONCAT(
  'SELECT   rowNum,
   IF ((DAYOFYEAR(completed) = DAYOFYEAR(''',startDate,''') ),''',xHours,''',coolingHours)AS coolingHours,
   l.completed,DAYOFYEAR(completed)AS completedDayOfYear,
    ''',startDateOriginal,''' AS OriginalStartDate,DAYOFYEAR(''',startDateOriginal,''')AS originalDayOfYear,
    ''',startDate,''' AS newStartDate,DAYOFYEAR(''',startDate,''')AS startDateDayOfYear,
    l.next_completed,DAYOFYEAR(  l.next_completed)AS nextCompletedDayOfYear,

    isError,charge,totalTimeRange,startingDate,endingDate,
    l.logId, l.first_completed,databaseName,i.name,l.itemId, kwDifference, cost, 
    costT,l.details,timeInSeconds
  FROM tempTable3 l
   inner join items i ON l.logId = i.logId AND i.name LIKE ''%KW PRE%'' 
   WHERE l.itemId = ''',myItemId,''' 
          AND completed BETWEEN ''', startDate, ''' AND ''', endDate, '''

 '); 

Open in new window

0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now