Solved

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

Posted on 2013-12-26
2
311 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 13

Expert Comment

by:AielloJ
ID: 39741728
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 60

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 39741755
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This guide whil teach how to setup live replication (database mirroring) on 2 servers for backup or other purposes. In our example situation we have this network schema (see atachment). We need to replicate EVERY executed SQL query on server 1 to…
Foreword This article was written many years ago, in the days when PHP supported the MySQL extension (http://php.net/manual/en/function.mysql-connect.php).  Today (http://php.net/manual/en/migration70.removed-exts-sapis.php) you would not use MySQL…

734 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