简体   繁体   中英

MariaDB select from inner joined query

I am not able to further select from a joined subquery.

I have data in three tables: "events", "records" and "work_list". Each table has one piece of the puzzle where work_list is the shortest and contains top-level data, and the events table tracks many tiny frequent events.

I need to calculate many statistical variables from the events based on some key variables defined in work_list like weighted moving average etc. I have those metrics ready and working, but I have problems filtering the data in events based on selected parameters stored in work_list.

Here is code that does not work. The SELECT * is not important, I will change it to be more meaningful later, it is for clarity. However, I have tried many selections in place of the * without success.

What is wrong with this query from subquery?

Query example 1:

SELECT * FROM
    (SELECT events.id, events.type,events.timestamp, work_list.task
    FROM 
        ( events
            INNER JOIN records ON events.record_id = records.id
            INNER JOIN work_list ON records.work_list_id = work_list.id
        )
    WHERE work_list.customer_number = '1234' AS subquery
);

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as subquery ) LIMIT 0, 25' at line 8

The inner joined subquery works and it returns a normal table.

Query example 2:

SELECT events.id, events.type,events.timestamp, work_list.task
FROM (
        events
    INNER JOIN records ON events.record_id = records.id
    INNER JOIN work_list ON records.work_list_id = work_list.id
    )
WHERE work_list.customer_number = '1234';

I tried using parenthesis in different orders, and I changed selected variables in SELECT events.id, events.type,events.timestamp, work_list.task . I wonder if this is a poor way of doing this. I have the calculation part. So even if there might be better structures for this, I am interested in solutions that maintain this structure.

The goal of this phase is to filter the events table for further queries that are coded on top of it replacing the SELECT * .

These are the final calculations made earlier which I plan to use when I figure out the problem with Query example 1.

Query example 3:

SELECT *, ((SUM(rate * diff) OVER (ORDER BY startTime
     ROWS BETWEEN 4 PRECEDING AND CURRENT ROW)) /
     (SUM(diff) OVER(ORDER BY startTime
     ROWS BETWEEN 4 PRECEDING AND CURRENT ROW))) as rate_WMA
FROM (
    SELECT id, startTime, counts, diff, (counts / diff)*3600 as rate
    FROM (
        SELECT id, TIMESTAMPDIFF(SECOND, MIN(timestamp), MAX(timestamp))AS diff, SUM(change) as counts, MIN(timestamp) as startTime
        FROM `the filered subquery here`
        GROUP BY id 
    ) AS subquery
    WHERE diff > 0 
) AS totaltotal;

You have extra parenthesis (no need for those) and the alias for the subquery should be placed after the subquery:

SELECT * 
FROM (
  SELECT events.id, events.type,events.timestamp, work_list.task
  FROM events
    INNER JOIN records ON events.record_id = records.id
    INNER JOIN work_list ON records.work_list_id = work_list.id
  WHERE work_list.customer_number = '1234'
) AS subquery;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM