Solved

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

Posted on 2013-12-26
2
308 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
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 59

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Fore-Foreword Today (2016) Maxmind has a new approach to the distribution of its data sets.  This article may be obsolete.  Instead of using the examples here, have a look at the MaxMind API (https://www.maxmind.com/en/geolite2-developer-package). …
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.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

929 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

11 Experts available now in Live!

Get 1:1 Help Now