You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在SQL中结合文本与数值列实现IF ELSE条件计算?

Solution for Conditional Metal_kg Calculation in SQL

Got it, let's fix that conditional logic for your Metal_kg column! You can use SQL's CASE statement to handle this exact scenario—it's perfect for applying conditional values based on column data.

Here's the updated full query with the conditional logic applied:

SELECT 
    DATASET, 
    BLOCKID, 
    CATEGORY, 
    ROUND(TONNES, 0) AS TONS, 
    ROUND(VOLUME, 0) AS VOLUME, 
    ROUND(EU3O8, 0) AS GRADE,
    -- Conditional logic for Metal_kg
    CASE
        WHEN CATEGORY IN ('WST', 'SEM') THEN 0
        ELSE ROUND(TONNES * EU3O8 / 1000, 0)
    END AS Metal_kg
FROM dbo.tblPTEvaluationRep_GC75ppm;

Breakdown of the CASE statement:

  • WHEN CATEGORY IN ('WST', 'SEM') THEN 0: Checks if the CATEGORY value is either 'WST' or 'SEM'—if true, returns 0 for Metal_kg.
  • ELSE ROUND(TONNES * EU3O8 / 1000, 0): For all other CATEGORY values, uses your original calculation (rounded to 0 decimal places) to compute Metal_kg.

This keeps all your existing calculations intact while adding the conditional rule you need.


内容的提问来源于stack exchange,提问作者Uno

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:26:36