适用场景: 需要根据现有字段经过一定条件得到新的查询字段
相关语法: CASE WHEN 条件1 TEHN 结果1 WHEN 条件2 THEN 结果2 ...... ELSE 结果N END 练习代码:
 SELECT  * ,
( '评分: <span style=''font-weight:bold''>' + INFO.CMScore + '</span><br/>评级: <span style=''font-weight:bold''>' + INFO.CMGrade+'</span>' ) AS CMEvaluate ,
( '评分: <span style=''font-weight:bold''>' + INFO.ExamScore + '</span><br/>评级: <span style=''font-weight:bold''>' + INFO.ExamGrade+'</span>' ) AS ExamEvaluate
FROM ( SELECT dbo.LB_Project.LB_Pro_ID , dbo.LB_Project.BD_ID , LB_Project.LB_Pro_Code ,
-- ISNULL((SELECT TOP 1 Emp_Name FROM dbo.IC_Employee WHERE Emp_ID=dbo.LB_Porject_ScoreCard.Emp_ID_Report),'- -') AS ReportEmp,
-- ISNULL((SELECT TOP 1 Emp_Name FROM dbo.IC_Employee WHERE Emp_ID=dbo.LB_Porject_ScoreCard.Emp_ID_Exam),'- -') AS ExamEmp,
(SELECT TOP 1 BDName FROM dbo.Busi_Definition WHERE BD_ID = dbo.LB_Project.BD_ID ) AS BDName ,
(SELECT TOP 1 BDCode FROM dbo.Busi_Definition WHERE BD_ID = dbo.LB_Project.BD_ID ) AS BDCode ,
dbo.LB_Project.Cust_ID ,
(SELECT TOP 1 Cust_Name FROM dbo.Cust_Main WHERE Cust_ID = dbo.LB_Project.Cust_ID) AS CustName ,
(SELECT TOP 1 IDCard FROM dbo.Cust_Main WHERE Cust_ID = dbo.LB_Project.Cust_ID) AS CustIDCard ,
--dbo.LB_Project.Dept_ID ,
(SELECT TOP 1 Dept_Name FROM dbo.IC_Departments WHERE Dept_ID = LB_Project.Dept_ID) AS DeptName ,
dbo.LB_Project.Emp_ID_ZB ,
(SELECT TOP 1 Emp_Name FROM dbo.IC_Employee WHERE Emp_ID = LB_Project.Emp_ID_ZB ) AS EmpName_ZB ,
dbo.LB_Project.Emp_ID_XB ,
(SELECT TOP 1 Emp_Name FROM dbo.IC_Employee WHERE Emp_ID = LB_Project.Emp_ID_XB ) AS EmpName_XB ,
dbo.LB_Project.LoanAmount ,
--dbo.LB_Project.TermValue ,
--dbo.LB_Project.LoanUses ,
--dbo.LB_Project.TermUnit ,
(CAST(TermValue AS NVARCHAR(10)) + ( CASE TermUnit WHEN '月' THEN '个' + TermUnit ELSE TermUnit END ) ) AS Term ,
(CAST(CAST(RateValue AS DECIMAL(6,2)) AS NVARCHAR(10)) + '%/' + RateUnit ) AS Rate ,
--dbo.LB_Project.RateValue , dbo.LB_Project.RateUnit , dbo.LB_Project.RM_ID ,
(SELECT TOP 1 RM_Name FROM dbo.Set_RepayMent WHERE RM_ID = LB_Project.RM_ID) AS RMName ,
--dbo.LB_Project.GM_ID ,
(SELECT '['+GM_Name+']' FROM dbo.Set_GuaranteeMethod WHERE GM_ID IN (SELECT Value FROM dbo.SplitString(LB_Project.GM_ID,',',1) ) FOR XML PATH('')) AS GMName ,
--dbo.LB_Project.VerifyStatus ,
dbo.LB_Project.CreateTime ,
dbo.LB_Project.Status ,
(CASE dbo.LB_Project.Status WHEN 0 THEN '待确认' WHEN 1 THEN '待复核' WHEN 2 THEN '已评分' ELSE '- -' END ) StatusName ,
--dbo.LB_Project_JRWPFD.LB_Pro_JP_ID ,
--dbo.LB_Project_JRWPFD.MM_ID ,
(CASE WHEN EXISTS(SELECT 1 FROM dbo.Busi_Definition WHERE BD_ID=dbo.LB_Project.BD_ID AND BDCode='BD_CODE_JRWPFD')
THEN (SELECT TOP 1 MM_Name FROM dbo.Market_Main WHERE MM_ID = LB_Project_JRWPFD.MM_ID)
WHEN EXISTS(SELECT 1 FROM dbo.Busi_Definition WHERE BD_ID=dbo.LB_Project.BD_ID AND BDCode='BD_CODE_ZHLGCYD')
THEN (SELECT TOP 1 MarketName FROM dbo.LB_Project_ZHLGCYD) ELSE '- -' END ) AS MMName ,
--dbo.LB_Project_JRWPFD.S_BC_ID ,
(CASE WHEN EXISTS(SELECT 1 FROM dbo.Busi_Definition WHERE BD_ID=dbo.LB_Project.BD_ID AND BDCode='BD_CODE_JRWPFD')
THEN (SELECT TOP 1 Name FROM dbo.Set_BusinessCategory WHERE S_BC_ID = dbo.LB_Project_JRWPFD.S_BC_ID)
WHEN EXISTS(SELECT 1 FROM dbo.Busi_Definition WHERE BD_ID=dbo.LB_Project.BD_ID AND BDCode='BD_CODE_ZHLGCYD')
THEN (SELECT TOP 1 CategoryArea FROM dbo.LB_Project_ZHLGCYD) ELSE '- -' END) AS CateGoryName ,
--dbo.LB_Project_JRWPFD.Paving ,
--dbo.LB_Project_JRWPFD.ValuationDetail ,
--dbo.LB_Project_JRWPFD.Valuation ,
(CASE WHEN dbo.LB_Project.Status = 2 THEN CONVERT(NVARCHAR(10), dbo.LB_Porject_ScoreCard.ExamScore)
ELSE '- -' END ) AS ExamScore ,
(CASE WHEN LB_Porject_ScoreCard.Status = 2 THEN ISNULL(( SELECT TOP 1 GSName FROM ( SELECT GSName FROM dbo.SC_GradeStandard
WHERE Status = 0 AND LB_Porject_ScoreCard.ExamScore BETWEEN MinValue AND MaxValue AND SC_ID = dbo.LB_Porject_ScoreCard.SC_ID) INFO
WHERE INFO.GSName <> '' ), '- -') ELSE '- -' END ) AS ExamGrade ,
(CASE WHEN dbo.LB_Project.Status > 0 THEN CONVERT(NVARCHAR(10), dbo.LB_Porject_ScoreCard.ReportScore) ELSE '- -' END ) AS CMScore ,
(CASE WHEN LB_Porject_ScoreCard.Status > 0 THEN ISNULL(( SELECT TOP 1 GSName FROM ( SELECT GSName FROM dbo.SC_GradeStandard
WHERE Status = 0 AND LB_Porject_ScoreCard.ReportScore BETWEEN MinValue AND MaxValue AND SC_ID = dbo.LB_Porject_ScoreCard.SC_ID) INFO
WHERE INFO.GSName <> '' ), '- -') ELSE '- -' END ) AS CMGrade FROM dbo.LB_Project
LEFT JOIN dbo.LB_Porject_ScoreCard ON LB_Porject_ScoreCard.LB_Pro_ID = LB_Project.LB_Pro_ID
LEFT OUTER JOIN dbo.LB_Project_JRWPFD ON dbo.LB_Project_JRWPFD.LB_Pro_ID = dbo.LB_Project.LB_Pro_ID
LEFT JOIN dbo.LB_Project_ZHLGCYD ON LB_Project_ZHLGCYD.LB_Pro_ID = LB_Project.LB_Pro_ID) INFO;

  


 

SQL 查询中case的运用的更多相关文章

  1. SQL查询中in、exists、not in、not exists的用法与区别

    1.in和exists in是把外表和内表作hash(字典集合)连接,而exists是对外表作循环,每次循环再对内表进行查询.一直以来认为exists比in效率高的说法是不准确的,如果查询的两个表大小 ...

  2. SQL查询中关键词的执行顺序

    写在前面:最近的工作主要是写SQL脚本,在编写过程中对SQL的执行和解析过程特别混乱不清,造成了想优化却无从下手.为此专门在网上找博文学习,并做了如下总结. 1.查询中常用到的关键词有: SELECT ...

  3. 在sql查询中为了提高查询效率,我们常常会采取一些措施对查询语句进行sql优化,下面总结的一些方法,有需要的可以参考参考。

    转载https://www.cnblogs.com/zhang-bo/p/9138151.html 1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建 ...

  4. SQL查询中关键字的执行顺序

    SQL语句中的每个关键字都按照顺序往下执行,而每一步操作会生成一个临时表,最后的临时表就是最终结果: FROM <left_table>:from子句返回初始结果集 <join_ty ...

  5. SQL 查询中not in 与 not exists 的区别

    1.in和exists in是把外表和内表作hash连接,而exists是对外表作loop循环,每次loop循环再对内表进行查询,一直以来认为exists比in效率高的说法是不准确的.如果查询的两个表 ...

  6. sql server中case when的用法

    Case具有两种格式.简单Case函数和Case搜索函数. --简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其他' END ...

  7. Oracle的sql语句中case关键字的用法 & 单双引号的使用

    关于sql中单引号和双引号的使用,来一点说明: 1. 查询列的别名如果含有汉字或者特殊字符(如以'_'开头),需要用双引号引起来.而且只能用双引号,单引号是不可以的. 2. 如果想让某列返回固定的值, ...

  8. SQL查询中连接--学习

    一.开头说 不出意外,还是先说下SQL中所有的联接类型: 内连接.外连接(左连接.右连接 .全连接).交叉连接 然后接下来就是依次学习下各种连接的使用效果 二.各种连接秀 首先准备两张表   学生表: ...

  9. SQL语句中case函数

    case函数,严格的意义上来讲case函数已经试流程控制语句了,不是简单意义上的函数,不过为了方便,很多人将case函数称为流程控制函数. case函数的一般有两种用法:1.case expressi ...

随机推荐

  1. 模型转换[yolov3模型在keras与darknet之间转换]

    首先借助qqwweee/keras-yolo3中的convert.py和tensorrt例子yolov3_onnx,并重新编写了代码,实现将darknet格式的yolov3的yolov3.cfg和yo ...

  2. Dapeng框架-开源高性能分布式微服务框架

    我们公司性质是新零售,公司也有专门的框架组.这群大牛自己开发了一整套分布式微服务框架.我们也在使用这套框架,有很多心得体会. 该框架既Dapeng也!开源github地址:https://github ...

  3. Android USB Headset: Device Specification

    For USB headsets to work seamlessly across the Android ecosystem, Android devices must support them ...

  4. C语言中的神兽strdup

    C语言的确博大精深,在C语言的世界中遨游了那么多年,发现自己仍是菜鸟一枚,很多利器没有能够驾驭,今天介绍一个神兽,威力无比,但是却很少人能用得好. 函数原型: #include <string. ...

  5. 消费阿里云日志服务SLS

    此文档只关心消费接入,不关心日志接入,只关心消费如何接入,可直接跳转到[sdk消费接入] SLS简介 日志服务: 日志服务(Log Service,简称 LOG)是针对日志类数据的一站式服务,在阿里巴 ...

  6. 【重学计算机】计组D3章:运算方法与运算器

    1. 定点数运算及溢出 定点数加减法:减法化加法,用补码直接相加,忽略进位 溢出:运算结果超出了某种数据类型的表示范围 溢出检测方法:统一思想概括为正正得负或负负得正则溢出,正负或负正不可能溢出 方法 ...

  7. Asp .Net Core 读取appsettings.json配置文件

         Asp .Net Core 如何读取appsettings.json配置文件?最近也有学习到如何读取配置文件的,主要是通过 IConfiguration,以及在Program中初始化完成的. ...

  8. .NET Core微服务之基于Ocelot+Butterfly实现分布式追踪

    Tip: 此篇已加入.NET Core微服务基础系列文章索引 一.什么是Tracing? 微服务的特点决定了功能模块的部署是分布式的,以往在单应用环境下,所有的业务都在同一个服务器上,如果服务器出现错 ...

  9. 【Android Studio安装部署系列】三十七、从Android Studio3.2升级到Android Studio3.4【以及创建Android Q模拟器】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 概述 保持Android Studio开发环境的最新版本. 下载Android Studio3.4 使用Android Studio自带的 ...

  10. 使用Atlas进行元数据管理之Atlas简介

    背景:笔者和团队的小伙伴近期在进行数据治理/元数据管理方向的探索, 在接下来的系列文章中, 会陆续与读者们进行分享在此过程中踩过的坑和收获. 元数据管理系列文章: [0] - 使用Atlas进行元数据 ...