使用Firebird 3.0我有这些表
model_types表有一些类型,如Type1,type2,type3 ......我需要得到这样的结果:
Type All UFZ Tempered
=================================
Type1 45 23 12
Type2 0 0 0
Type3 2 0 0
所以我写了这个SQL:
select
sum((window.win_width * window.win_height) / 144),
model_types.model_type_description
from model_types
left outer join models on (model_types.model_type_id = models.model_type_id)
inner join series_model on (models.model_id = series_model.model_id)
inner join window on (series_model.sm_id = window.sm_id)
where (quote_id = 122) and (window.ufz = 0) and (window.tempered = 0)
group by model_types.model_type_description
这个SQL只给我的问题" type1"如果其他类型为零,那么我如何列出model_types表中的所有类型,即使它们是零?
另一个问题:在这个SQL中where条件:
(window.ufz = 0) and (window.tempered = 0)
代表" All"获得UFZ时我需要使用这些条件:
(window.ufz = 1) and (window.tempered = 0)
并获得Tempered我需要使用这个条件:
(window.ufz = 0) and (window.tempered = 1)
那么我如何在单独的列中显示所有3个结果(All,UFZ,Tempered)?
答案 0 :(得分:0)
经过多次测试后,我发现我可以使用Case
,但不知道我的SQL是否正确,如果它们是零则不会显示所有模型类型。
select
sum(CASE WHEN (window.ufz = 0) and (window.tempered = 0)
THEN ((window.win_width * window.win_height) / 144) END) AS models_all,
sum(CASE WHEN (window.ufz = 1) and (window.tempered = 0)
THEN ((window.win_width * window.win_height) / 144) END) AS models_ufz,
sum(CASE WHEN (window.ufz = 0) and (window.tempered = 1)
THEN ((window.win_width * window.win_height) / 144) END) AS models_temp,
model_types.model_type_description
from model_types
left outer join models on (model_types.model_type_id = models.model_type_id)
inner join series_model on (models.model_id = series_model.model_id)
inner join window on (series_model.sm_id = window.sm_id)
where (quote_id = 122)
group by model_types.model_type_description
答案 1 :(得分:0)
CASE构造具有ELSE部分,如果您的WHEN部分未经验证,则可以在其中添加默认值。我认为这将完成查询结果中丢失的数据。