Я хочу добавить столбец «Status» в мое представление «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