Solved

need to add a 3rd join in from statement - giving error

Posted on 2013-12-26
2
307 Views
Last Modified: 2013-12-27
I am trying to add a 3rd left join however once I added that second select statement I am getting errors.

(SELECT T2.completed,
       (SELECT MAX(completed)
         FROM log',mylogID, '
         WHERE completed < T2.completed) AS 1stcompleted
      FROM log',mylogID, ' T2
      ORDER BY T2.completed
     )TimeOrder2

Open in new window


There error is

1 Unknown column 'TimeOrder.completed' in 'on clause'

So I am thinking I do not have them nested correctly.

The full code I am using is

SELECT
    @row := @row + 1 AS rownum,
    a.logId,
    L1.recordId,
    L2.recordId AS next_recordId,L3.completed AS 1stCompleted,
    L1.completed,
    L2.completed AS next_completed,
    L1.activityId AS activityJoin,
    L2.activityId AS next_activityId,
    IF(L1.activityId = L2.activityId, 1, NULL) AS isError,
     CASE
        when a.name LIKE ''%Enable%'' THEN time_to_sec(timediff(L2.completed, L1.completed)) / 3600
        ELSE NULL
        END AS coolingHours,
     TO_SECONDS(L2.completed) - TO_SECONDS(L1.completed) AS timeInSeconds,
    ((SUBSTR(l.details, INSTR(l.details, '':'') + 1))) AS charge,
    time_to_sec(timediff(''', endDate, ''', ''', startDate, ''')) / 3600 AS totalTimeRange,
    CONVERT_TZ(''', startDate, ''', ''UTC'', ''US/Pacific'') AS startingDate, 
    CONVERT_TZ(''', endDate, ''', ''UTC'', ''US/Pacific'') AS endingDate,
    ((L1.item', myItemID, ' - L1.item', myItemID2, ') * 
        (TIME_TO_SEC(TIMEDIFF(L2.completed, L1.completed)))) / 3600 AS kwDifference,
    ((L1.item', myItemID, ' - L1.item', myItemID2, ') * (SUBSTR(l.details, INSTR(l.details, '':'') + 1))) AS cost,
    ((((L1.item', myItemID, ' - L1.item', myItemID2, ') * (SUBSTR(l.details, INSTR(l.details,'':'') + 1)))
    * (TIME_TO_SEC(TIMEDIFF(L2.completed, L1.completed)) / 3600))) AS costT,
    DATABASE() AS databaseName, i.itemId ,l.details
FROM
    (SELECT @row:=0)R,
    (SELECT T1.completed,
       (SELECT MIN(completed)
         FROM log',mylogID, '
         WHERE completed > T1.completed) AS next_completed
      FROM log',mylogID, ' T1
      ORDER BY T1.completed
     )TimeOrder,(SELECT T2.completed,
       (SELECT MAX(completed)
         FROM log',mylogID, '
         WHERE completed < T2.completed) AS 1stcompleted
      FROM log',mylogID, ' T2
      ORDER BY T2.completed
     )TimeOrder2
        LEFT JOIN log', mylogID, ' L1 ON (L1.completed = TimeOrder.completed)
        LEFT JOIN log', mylogID, ' L2 ON (L2.completed = TimeOrder.next_completed)
        LEFT JOIN log', mylogID, ' L3 ON (L3.completed = TimeOrder2.1stcompleted)
        LEFT JOIN activities a ON L1.activityId = a.activityId
        LEFT JOIN logs l ON a.logId = l.logId
        inner join items i ON l.logId = i.logId AND i.name LIKE ''%KW PRE%'' 
    )ORDER BY L1.completed')

Open in new window

0
Comment
Question by:portlight
2 Comments
 
LVL 13

Expert Comment

by:AielloJ
Comment Utility
portlight,

Try changing the alias name.  Generally, columns and aliases begin with letters.  It's also listed as a general best practice.  You might also want to fully qualify the 'completed' column in the WHERE clause.

Best regards,

AielloJ
0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
Comment Utility
I agree.  In addition, it appears this is dynamic SQL (maybe in PHP? or other front end), so you can print out the command variable to see if there is an extra parenthesis, or other invalid syntax.  

Moreover, try to simplify things some.  Instead of the Cartesian product, you can put these two queries together.
 
(SELECT T1.completed,
       (SELECT MIN(completed)
         FROM log',mylogID, '
         WHERE completed > T1.completed) AS next_completed
      FROM log',mylogID, ' T1
      ORDER BY T1.completed
     ) TimeOrder,(SELECT T2.completed,
       (SELECT MAX(completed)
         FROM log',mylogID, '
         WHERE completed < T2.completed) AS 1stcompleted
      FROM log',mylogID, ' T2
      ORDER BY T2.completed
     )

Open in new window


To:
 
(
    SELECT T1.completed
         , (SELECT MIN(T2.completed) 
            FROM log',mylogID, ' T2
            WHERE T2.completed > T1.completed) AS next_completed
         , (SELECT MAX(T2.completed) 
            FROM log',mylogID, ' T2
            WHERE T2.completed < T1.completed) AS frst_completed
    FROM log',mylogID, ' T1
    ORDER BY T1.completed
) TimeOrder

Open in new window


As you simplify, it will help identify other issues.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

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.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
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.

771 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

9 Experts available now in Live!

Get 1:1 Help Now