在选择语句中的布尔逻辑

21 浏览
0 Comments

在选择语句中的布尔逻辑

这个问题在这里已经有了答案

如何在SQL SELECT语句中执行IF...THEN?

如何在条件评估期间从查询中返回布尔值?

在查询运行时,我如何在SELECT语句中实现布尔逻辑?

SELECT t.[Key]
      ,t.[Parent_Key]
      ,t.[Parent_Code]
      ,t.[Code]
      ,t.[Desc] 
      ,t.[Point]
      ,[isChild] -- If Point > 2, then true, if Point == 1 Then false   
      ,t.[By] 
      ,t.[On]
FROM [db].[stats] t WHERE t.[Parent_Key]= @tmpParameter

我想要编写一些逻辑来根据t.[Point]确定[isChild]布尔值。

admin 更改状态以发布 2023年5月25日
0
0 Comments

案例是你的朋友...

 SELECT Key, Parent_Key, Parent_Code, Code, Desc, point, 
     case when point > 2 then 1 
          when point = 1 then 0 end isChild, 
     [By], [On]
 FROM db.stats  
 WHERE Parent_Key= @tmpParameter

0
0 Comments

SELECT t.[Key]
      ,t.[Parent_Key]
      ,t.[Parent_Code]
      ,t.[Code]
      ,t.[Desc] 
      ,t.[Point]
      ,CASE WHEN t.[Point] > 2 THEN 1 ELSE  
            CASE WHEN t.[Point] = 1 THEN 0 ELSE NULL END 
       END AS [isChild]
      ,t.[By] 
      ,t.[On]
FROM [db].[stats] t WHERE t.[Parent_Key]= @tmpParameter

注意,当 t.[Point] < 1 时,[isChild] 将为空。

0