下面是在结果列中包含三列的表,我将权重与过去7天相乘以显示结果,还有另一个如果赠款总额与我编写的某些代码的总匹配匹配,我现在只希望显示该表旁边的类别中的一个类别,该代码现在可以倍增并将总和计入一列,但我想知道可以更改此代码以进行比较以及类别。
在您看到的图片框中,存在三个类别,其中级别列为解释名称。
我想要的是,您在左侧框中看到的总计是56.5,现在它与右侧框中的> 42.5 值匹配,我希望我的sql代码能给我低于结果。
Score Category Interpretation
56.5 >42.5 High
我确实尝试过下面的代码,但我想对其进行一些修改,我认为,目前,在图片右框中,如果没有创建表格,就可以实现上述结果,那么我没有针对这三个类别的另一个特定表格对于类别,最好还是与我一起创建另一个表并加入,请向我展示
select (SUM (FCS_CEREALS_AND_TUBERS * 2) + SUM (FCS_PULSES * 3) +
SUM (FCS_VEGETABLES * 1) + SUM (FCS_FRUIT * 1) +
SUM (FCS_MEAT_AND_FISH * 4) + SUM (FCS_MILK * 4) +
SUM (FCS_SUGAR * 0.5) + SUM (FCS_OIL * 0.5)) AS RESULT
from TBL_FCS
答案 0 :(得分:0)
好像您已经处理了数据。如果原始表中的数据未透视,则在该表上工作。这是您当前结构的解决方案:
with
weights(id, food_group, weight) as (
select 1, 'CEREALS AND TUBERS', 2 from dual union all
select 2, 'PULSES', 3 from dual union all
select 3, 'VEGETABLES', 1 from dual union all
select 4, 'FRUIT', 1 from dual union all
select 5, 'MEAT AND FISH', 4 from dual union all
select 6, 'MILK', 4 from dual union all
select 7, 'SUGAR', .5 from dual union all
select 8, 'OIL', .5 from dual )
select food_group, past_7_days, weight, result,
case when id is null and result <= 28 then 'Low'
when id is null and result between 28.5 and 42 then 'Normal'
when id is null and result >= 42.5 then 'High'
end as interpretation
from (
select food_group, past_7_days, weight, sum(past_7_days * weight) result, id
from (
select *
from tbl_fcs
unpivot (past_7_days for food_group in (
fcs_cereals_and_tubers as 'CEREALS AND TUBERS',
fcs_pulses as 'PULSES',
fcs_vegetables as 'VEGETABLES',
fcs_fruit as 'FRUIT',
fcs_meat_and_fish as 'MEAT AND FISH',
fcs_milk as 'MILK',
fcs_sugar as 'SUGAR',
fcs_oil as 'OIL')))
join weights using (food_group)
group by rollup((food_group, past_7_days, weight, id)))
order by id
您必须取消每个食物组的值,然后与它们的权重相乘并相乘。使用case when
添加按汇总汇总和条件解释。