假设有一个列有2个属性a和b。我只想让它只考虑一个整体情况。我如何在案例陈述中应用它?
类似的东西:
Case when student> 100 then 'big class'
else ''
End [big class ]
*exclude class 10th.*
答案 0 :(得分:0)
我没有得到你的问题,如果你想获得Divison A的“大班”,没有必要使用案例。或者您还有其他要求。
;WITH SampleTable(GroupNo,NoOfStudent,Division)AS(
SELECT 'grp 1',105,'a' UNION ALL
SELECT 'grp 2',107,'a' UNION ALL
SELECT 'grp 3',108,'b' UNION ALL
SELECT 'grp 4',110,'b' UNION ALL
SELECT 'grp 5',85,'b' UNION ALL
SELECT 'grp 6',84,'a' UNION ALL
SELECT 'grp 7',83,'a' UNION ALL
SELECT 'grp 8',110,'b' UNION ALL
SELECT 'grp 9',105,'a' UNION ALL
SELECT 'grp 10',140,'b'
)
--1
SELECT * FROM SampleTable WHERE Division='A' AND NoOfStudent>100
GroupNo NoOfStudent Division ------- ----------- -------- grp 1 105 a grp 2 107 a grp 9 105 a
--2
SELECT * ,CASE WHEN Division='A' AND NoOfStudent>100 THEN 'big class' ELSE '' END AS [BigClass]
FROM SampleTable
GroupNo NoOfStudent Division BigClass ------- ----------- -------- --------- grp 1 105 a big class grp 2 107 a big class grp 3 108 b grp 4 110 b grp 5 85 b grp 6 84 a grp 7 83 a grp 8 110 b grp 9 105 a big class grp 10 140 b