如何在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 theCATEGORYvalue is either 'WST' or 'SEM'—if true, returns 0 forMetal_kg.ELSE ROUND(TONNES * EU3O8 / 1000, 0): For all otherCATEGORYvalues, uses your original calculation (rounded to 0 decimal places) to computeMetal_kg.
This keeps all your existing calculations intact while adding the conditional rule you need.
内容的提问来源于stack exchange,提问作者Uno
相关产品推荐
相关产品推荐

