简体   繁体   English

MySQL-在select语句中包含过程的结果

[英]MySQL - Including the results of a procedure in a select statement

I have a table, called gcrunarrays with the columns {run_id, time_id, co2}. 我有一张名为gcrunarrays的表,其列为{run_id,time_id,co2}。 I have several thousand runs with distinct run_id's, each with 58 time-dependent values for co2. 我有数千个运行具有不同的run_id,每个运行具有58个与时间相关的CO2值。 I have a procedure to obtain the median co2 of all runs at a given time. 我有一个程序来获取给定时间所有运行的中值二氧化碳。 Now, I need a select statement that will obtain the medians for each time. 现在,我需要一个select语句,该语句将获取每次的中值。 So far, I have 到目前为止,我有

select DISTINCT A.time_id, M.co2 from gcrunarrays A, call getMedian(1, A.time_id) M
GO

Which gets a syntax error. 哪个得到语法错误。 I am fairly new at SQL and I'm working in MYSQL. 我是SQL的新手,正在使用MYSQL。 I've tried about a dozen different ways of wording this statement but now I'm at the point where I feel like I've done something inherently wrong. 我已经尝试了十多种不同的措辞表达方式,但是现在我感觉自己做错了一些内在的东西。 I think it might work better if median were a function but I'm not sure even how to get the median without using a select statement. 我认为,如果中位数是一个函数,可能会更好,但是我不确定即使不使用select语句也如何获取中位数。 Any suggestions are greatly appreciated. 任何建议,不胜感激。

For greater clarification: 为了进一步说明:

Table gcrunarrays 表gcrunarrays

run_id | time_id | co2
1      | 1       | 
1      | 2       | 
...
1      | 58      | 
2      | 1       | 
...    
2      | 58      | 
3 ...

Median Procedure 中位数程序

CREATE PROCEDURE getMedian (IN e INT, t INT)
BEGIN
SELECT count(*), x.co2
FROM (SELECT B.exp_id, A.* FROM gcRunArrays A JOIN gcRuns B ON A.run_id=B.run_id WHERE B.exp_id=e and A.time_id=t) x, 
     (SELECT B.exp_id, A.* FROM gcRunArrays A JOIN gcRuns B ON A.run_id=B.run_id WHERE B.exp_id=e and A.time_id=t) y
GROUP BY x.co2
HAVING SUM(SIGN(1-SIGN(y.co2-x.co2))) = CEILING((COUNT(*)+1)/2);
END
GO

Use a temp table can easily solve the issue. 使用临时表可以轻松解决问题。 And it is not possible to use procedure results as table directly. 而且,不可能将过程结果直接用作表。

Can a stored procedure/function return a table? 存储过程/函数可以返回表吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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