包含全部联合的视图中的MySQL Case语句

时间:2018-10-02 12:12:22

标签: mysql sql case sql-view union-all

我想在视图“ v_file_details”中添加一个“状态”列,该列将具有一个值,具体取决于“ v_file_details.records_waiting_count”和“ v_file_details.records_correct_count”列中的数据。但是我不明白如何在视图中使用Case?

create view v_file_details as
select 
fi.file_importer_id,
fi.file_name,
convert(time(0), fi.file_date) import_time,
fi.error_type,
((SELECT COUNT(*) From reservation Where file_importer_id = fi.file_importer_id AND status in ('S', 'S1')) + 
(SELECT COUNT(*) From error_file_record Where file_importer_id = fi.file_importer_id) +
(SELECT COUNT(*) From startup Where file_importer_id = fi.file_importer_id AND status in ('T', '6'))) as records_correct_count,
((SELECT COUNT(*) From location Where file_importer_id = fi.file_importer_id AND status = 'Y') + 
(SELECT COUNT(*) From reservation Where file_importer_id = fi.file_importer_id AND status = 'N') + 
(SELECT COUNT(*) From startup Where file_importer_id = fi.file_importer_id AND status in ('X', 'X1'))) as records_waiting_count,
case 'status' when records_waiting_count > 0 THEN 'Waiting' ELSE 'ACCEPTED', -- it is the problem, I don't know how to use CASE here
fi.new_file_name,

from file_importer fi
left join startup ts on fi.file_importer_id = ts.file_importer_id 
where ts.file_importer_id is not null

union all

select 
fi.file_importer_id,
fi.file_name,
convert(time(0), fi.file_date) import_time,
fi.error_type,
((SELECT COUNT(*) From reservation Where file_importer_id = fi.file_importer_id AND status in ('S', 'S1')) + 
(SELECT COUNT(*) From error_file_record Where file_importer_id = fi.file_importer_id) +
(SELECT COUNT(*) From startup Where file_importer_id = fi.file_importer_id AND status in ('T', '6'))) as records_correct_count,
((SELECT COUNT(*) From location Where file_importer_id = fi.file_importer_id AND status = 'Y') + 
(SELECT COUNT(*) From reservation Where file_importer_id = fi.file_importer_id AND status = 'N') + 
(SELECT COUNT(*) From startup Where file_importer_id = fi.file_importer_id AND status in ('X', 'X1'))) as records_waiting_count,
case 'status' when records_waiting_count > 0 THEN 'Waiting' ELSE 'ACCEPTED', --here the same
fi.new_file_name,

from file_importer fi
left join reservation res on fi.file_importer_id = res.file_importer_id 
where res.file_importer_id is not null

GO

1 个答案:

答案 0 :(得分:1)

您不能在jobs = Dict{Int,Future}() @sync for i in workers() @async jobs[i] = @spawnat i Test() end @sync for i in workers() @async fetch(jobs[i]) end 中使用别名,因此您不能引用SELECT,需要使用它所引用的表达式。另外,您已经有一个名为records_waiting_count的列,因此您不能调用该status表达式的值(取而代之的是CASE)。因此,您的waiting_status表达式应如下所示:

CASE