2012-11-02 58 views
1

我有一个select语句,我希望根据表中的其他值计算立方体体积。不过,我想检查pr.Length_mm或pr.Width_mm或pr.Height_mm是否为NULL。我查看了CASE语句,但它似乎只是一次评估一列。选择查询的多个条件

SELECT 
     sa.OrderName, 
     sa.OrderType, 
     pr.Volume_UOM 
,pr.Length_mm*pr.Width_mm*pr.Height_mm AS Volume_Cubic 
,pr.Length_mm*pr.Width_mm AS Volume_Floor 
,pr.Length_mm 
,pr.Height_mm 
,pr.Width_mm 
FROM CostToServe_MCB.staging.Sale sa 
LEFT JOIN staging.Product pr 
ON sa.ID = pr.ID 
+1

你为什么左侧的接合部在这里? 'LEFT JOIN'-ing,然后在右边的表上使用'IS NOT NULL'似乎毫无意义。所有通过使用左连接而不是内连接保留的行在所有“pr”列中都有“NULL”。 –

+0

@MartinSmith我认为你是对的,但你不能把条件加入连接吗?检查我更新的答案。 –

+0

这是完整查询的子集。还有其他的栏目,我需要带入,其中可能不是右桌的一部分。 – stats101

回答

2
SELECT pr.Volume_UOM 
,pr.Length_mm*pr.Width_mm*pr.Height_mm AS Volume_Cubic 
,pr.Length_mm*pr.Width_mm AS Volume_Floor 
,pr.Length_mm 
,pr.Height_mm 
,pr.Width_mm 
FROM CostToServe_MCB.staging.Sale sa 
LEFT JOIN staging.Product pr 
ON sa.ID = pr.ID 
and pr.Length_mm is not null 
and pr.Width_mm is not null 
and pr.Height_mm is not null 
2
SELECT pr.Volume_UOM 
,pr.Length_mm*pr.Width_mm*pr.Height_mm AS Volume_Cubic 
,pr.Length_mm*pr.Width_mm AS Volume_Floor 
,pr.Length_mm 
,pr.Height_mm 
,pr.Width_mm 
FROM CostToServe_MCB.staging.Sale sa 
    LEFT JOIN staging.Product pr ON sa.ID = pr.ID 
where pr.Length_mm is not null and pr.Width_mm is not null and pr.Height_mm is not null 
+0

对不起,我提出的查询并不完全代表我正在使用的查询。它包含许多其他列。以上答案限制了3个值的结果。 – stats101

0
where pr.Length_mm is not null 
    and pr.Width_mm is not null 
    and pr.Height_mm is not NULL