Sql server 2005 Case来提取多个列
我想知道是否有类似的情况,我可以在SQL中使用1个Case表达式在SQLServer2005中获得多个列。我是SQL方面的新手。希望不是太容易,我错过了。提前谢谢你Sql server 2005 Case来提取多个列,sql-server-2005,Sql Server 2005,我想知道是否有类似的情况,我可以在SQL中使用1个Case表达式在SQLServer2005中获得多个列。我是SQL方面的新手。希望不是太容易,我错过了。提前谢谢你 Select RD.RepDailyInfoID, RD.TypeofDayID Case WHEN RD.TypeofDayID = 1 THEN isnull(cast(S.AmountSold as numeric(10,2)), 0) as AmountSold, isnull(cast(S.S_Amoun
Select RD.RepDailyInfoID, RD.TypeofDayID
Case WHEN RD.TypeofDayID = 1
THEN
isnull(cast(S.AmountSold as numeric(10,2)), 0) as AmountSold,
isnull(cast(S.S_AmountCollected as numeric(10,2)), 0) as AmountCollected,
S.S_PaymentMethod as PaymentMethod
When RD.TypeofDayID = 9
THEN
isnull(cast(U.AmountUpgraded as numeric(10,2)), 0) as AmountUpgraded,
isnull(cast(U.U_UpgradedCollected as numeric(10,2)), 0) + isnull(cast(U.RenewalCollected as numeric(10,2)), 0) as AmountCollected,
U.U_PaymentMethod as PaymentMethod
END
from RepDailyInfo RD
left outer join SellingInfo S on S.RepDailyInfoID = RD.RepDailyInfoID
left outer join UpgradingInfo U on U.RepDailyInfoID = RD.RepDailyInfoID
where RepID = @RepID
case语句的THEN部分不能有多个字段。您可以使用相同的逻辑构建多个case语句:
Case WHEN RD.TypeofDayID = 1 THEN isnull(cast(S.AmountSold as numeric(10,2)), 0) END as AmountSold,
Case WHEN RD.TypeofDayID = 1 THEN isnull(cast(S.S_AmountCollected as numeric(10,2)), 0) END as AmountCollected
等等。案例语句的THEN部分不能有多个字段。您可以使用相同的逻辑构建多个case语句:
Case WHEN RD.TypeofDayID = 1 THEN isnull(cast(S.AmountSold as numeric(10,2)), 0) END as AmountSold,
Case WHEN RD.TypeofDayID = 1 THEN isnull(cast(S.S_AmountCollected as numeric(10,2)), 0) END as AmountCollected
等等。这没有经过测试,但应该可以让您开始。每个领域都必须有一个案例
select RD.RepDailyInfoID, RD.TypeofDayID
, AmountSold = case when RD.TypeofDayID = 1 THEN isnull(cast(S.AmountSold as numeric(10,2)), 0) else 0 end
, AmountUpgraded = case when RD.TypeofDatID = 9 THEN isnull(cast(U.AmountUpgraded as numeric(10,2)), 0) else 0 end
, AmountCollected = case when RD.TypeofDayID = 1 then isnull(cast(S.S_AmountCollected as numeric(10,2)), 0) else
when RD.TypeofDayID = 9 then isnull(cast(U.U_UpgradedCollected as numeric(10,2)), 0) + isnull(cast(U.RenewalCollected as numeric(10,2)), 0) end
, PaymentMethod = case when RD.TypeofDayID = 1 then S.S_PaymentMethod else
when RD.TypeofDayID = 9 then U.U_PaymentMethod end
from RepDailyInfo RD
left outer join SellingInfo S on S.RepDailyInfoID = RD.RepDailyInfoID
left outer join UpgradingInfo U on U.RepDailyInfoID = RD.RepDailyInfoID
where RepID = @RepID
这是没有测试,但应该让你开始。每个领域都必须有一个案例
select RD.RepDailyInfoID, RD.TypeofDayID
, AmountSold = case when RD.TypeofDayID = 1 THEN isnull(cast(S.AmountSold as numeric(10,2)), 0) else 0 end
, AmountUpgraded = case when RD.TypeofDatID = 9 THEN isnull(cast(U.AmountUpgraded as numeric(10,2)), 0) else 0 end
, AmountCollected = case when RD.TypeofDayID = 1 then isnull(cast(S.S_AmountCollected as numeric(10,2)), 0) else
when RD.TypeofDayID = 9 then isnull(cast(U.U_UpgradedCollected as numeric(10,2)), 0) + isnull(cast(U.RenewalCollected as numeric(10,2)), 0) end
, PaymentMethod = case when RD.TypeofDayID = 1 then S.S_PaymentMethod else
when RD.TypeofDayID = 9 then U.U_PaymentMethod end
from RepDailyInfo RD
left outer join SellingInfo S on S.RepDailyInfoID = RD.RepDailyInfoID
left outer join UpgradingInfo U on U.RepDailyInfoID = RD.RepDailyInfoID
where RepID = @RepID
事实上,这种情况确实存在。记录在案 不过,我觉得你的格式可能有点不对劲。尝试:
SELECT RD.RepDailyInfoID, AmountSold =
CASE RD.TypeofDayID
WHEN '1' THEN isnull(cast(S.AmountSold as numeric(10,2)), 0)
END
FROM RepDailyInfo RD
left outer join SellingInfo S on S.RepDailyInfoID = RD.RepDailyInfoID
left outer join UpgradingInfo U on U.RepDailyInfoID = RD.RepDailyInfoID
WHERE RepID = @RepID
我还没有测试过它,但这只是我在快速查看文档后对格式的理解
祝你好运
编辑:看来兰斯·忍者打败了我。他的代码更加完整,但(部分)证实了我所认为的情况。事实上确实存在这种情况。记录在案 不过,我觉得你的格式可能有点不对劲。尝试:
SELECT RD.RepDailyInfoID, AmountSold =
CASE RD.TypeofDayID
WHEN '1' THEN isnull(cast(S.AmountSold as numeric(10,2)), 0)
END
FROM RepDailyInfo RD
left outer join SellingInfo S on S.RepDailyInfoID = RD.RepDailyInfoID
left outer join UpgradingInfo U on U.RepDailyInfoID = RD.RepDailyInfoID
WHERE RepID = @RepID
我还没有测试过它,但这只是我在快速查看文档后对格式的理解
祝你好运
编辑:看来兰斯·忍者打败了我。他的代码更完整,但(部分)证实了我的想法。只需复制并将案例逻辑粘贴到每列即可复制并将案例逻辑粘贴到每列即可