2017-01-27 3 views
0

プロシージャの出力を1つのテーブルにまとめたい。実際にはHeidlSqlで実行しているときに複数のテーブルに出力を表示しています。結果を1つのテーブルに返す必要があるMysqlプロシージャ

DELIMITER // 
create procedure daily_report() 
BEGIN 
select count(Transaction1) from test1 where (DATE1 between curdate() and curdate()+1); 
select count(Transaction1) from test1 where RESPONSE='Y' and (DATE1 between curdate() and curdate()+1); 
select (sum(RESPONSE='Y')/count(Transaction1))*100 from test1 where (DATE1 between curdate() and curdate()+1); 
select count(Transaction1) from test1 where RESPONSE='N' and (DATE1 between curdate() and curdate()+1); 
select (sum(RESPONSE='N')/count(Transaction1))*100 from test1 where (DATE1 between curdate() and curdate()+1); 
select count(Transaction1),DATE1 from test1 where RESPONSE='N' and ERROR1='30' and (DATE1 between curdate() and curdate()+1); 
select (sum(RESPONSE='N' AND ERROR1='30')/count(Transaction1))*100 from test1 where (DATE1 between curdate() and curdate()+1); 
select count(Transaction1) from test1 where RESPONSE='N' and ERROR1 not like '30' and (DATE1 between curdate() and curdate()+1); 
select (sum(RESPONSE='N' AND ERROR1 not like '30')/count(Transaction1))*100 from test1 where (DATE1 between curdate() and curdate()+1); 
end 
DELIMITER ; 

答えて

0

あなたは、単一の結果セットを取得したい場合は、UNIONとあなたのクエリを組み合わせています。
1つの注意点は、すべて同じ列数を返さなければならないことです。

SELECT 'metric1' AS name, count(Transaction1) AS value1, NULL AS value2 from test1 where (DATE1 between curdate() and curdate()+1) 
UNION 
SELECT 'metric2', count(Transaction1), NULL from test1 where RESPONSE='Y' and (DATE1 between curdate() and curdate()+1) 
UNION 
select 'metric3', (sum(RESPONSE='Y')/count(Transaction1))*100, NULL from test1 where (DATE1 between curdate() and curdate()+1) 
UNION 
select 'metric4', count(Transaction1),DATE1 from test1 where RESPONSE='N' and ERROR1='30' and (DATE1 between curdate() and curdate()+1); 
関連する問題