SELECT * FROM (select aa.*,bb.mentalvisitid,
case when bb.BCSFXS='' then '门诊' when bb.BCSFXS='' then '家庭访视' else '电话' end as BCSFXS ,bb.visitdate,
case when bb.Hospitalstatus='' then '目前正在住院' when bb.Hospitalstatus='' then '曾住院,现未住院' else '未住院' end as Hospitalstatus,
case when bb.visittype='' then '不稳定' when bb.visittype='' then '基本稳定' else '稳定' end as visittype ,
case when bb.Followmedicine='' then '按医嘱规律用药' when bb.Followmedicine='' then '间断用药' when bb.Followmedicine='' then '不用药' else '医嘱无需用药' end as Followmedicine,
case when bb.Treatment='' then '痊愈' when bb.Treatment='' then '好转' when bb.Treatment='' then '无变化' when bb.Treatment='' then '加重' else '此项不适用' end as Treatment,
case when bb.Labcheck ='' then '有' else '无' end as Labcheck, bb.labcheckother, (CASE WHEN bb.SYMPTOMS like '%1%' then '幻觉 ' end ||
CASE WHEN bb.SYMPTOMS like '%2%' then '交流困难 ' end ||
CASE WHEN bb.SYMPTOMS like '%3%' then '猜疑 ' end ||
CASE WHEN bb.SYMPTOMS like '%4%' then '喜怒无常 ' end ||
CASE WHEN bb.SYMPTOMS like '%5%' then '行为怪异 ' end ||
CASE WHEN bb.SYMPTOMS like '%6%' then '兴奋话多 ' end ||
CASE WHEN bb.SYMPTOMS like '%7%' then '伤人毁物 ' end ||
CASE WHEN bb.SYMPTOMS like '%8%' then '悲观厌世 ' end ||
CASE WHEN bb.SYMPTOMS like '%9%' then '无故外走 ' end ||
CASE WHEN bb.SYMPTOMS like '%10%' then '自语自笑 ' end ||
CASE WHEN bb.SYMPTOMS like '%11%' then '孤僻懒散 ' end ||
CASE WHEN bb.SYMPTOMS like '%12%' then '其他 ' end
) as SYMPTOMS,
(CASE WHEN bb.RECOVERYMANNER like '%1%' then '生活劳动能力 ' end ||
CASE WHEN bb.RECOVERYMANNER like '%2%' then '职业训练 ' end ||
CASE WHEN bb.RECOVERYMANNER like '%3%' then '学习能力 ' end ||
CASE WHEN bb.RECOVERYMANNER like '%4%' then '社会交往 ' end ||
CASE WHEN bb.RECOVERYMANNER like '%5%' then '其他 ' end
) as RECOVERYMANNER, (select wm_concat(to_char(medname) || '(' || '每日' || to_char(usage) || '次 , ' || to_char(amount) || ')' )
from MB_MentalVisit_Medicine
where ISSTATE = ''
and Mentalvisitid = bb.mentalvisitid) AS MEDICINE, case when bb.DANGERSTATUS='' then '0级' when bb.DANGERSTATUS='' then '1级' when bb.DANGERSTATUS='' then '2级' when bb.DANGERSTATUS='' then '3级' when bb.DANGERSTATUS='' then '4级' when bb.DANGERSTATUS='' then '5级' end as DANGERSTATUS,
case when bb.SELFKNOW='' then '自知力完全' when bb.SELFKNOW='' then '自知力不全' when bb.SELFKNOW='' then '自知力缺失' end as SELFKNOW,
case when bb.SLEEP='' then '良好' when bb.SLEEP='' then '一般' when bb.SLEEP='' then '较差' end as SLEEP,
case when bb.EATING='' then '良好' when bb.EATING='' then '一般' when bb.EATING='' then '较差' end as EATING,
case when bb.PERSONALLIFE='' then '良好' when bb.PERSONALLIFE='' then '一般' when bb.PERSONALLIFE='' then '较差' end as PERSONALLIFE,
case when bb.FAMILY='' then '良好' when bb.FAMILY='' then '一般' when bb.FAMILY='' then '较差' end as FAMILY,
case when bb.PRODUCTWROK='' then '良好' when bb.PRODUCTWROK='' then '一般' when bb.PRODUCTWROK='' then '较差' end as PRODUCTWROK,
case when bb.STUDY='' then '良好' when bb.STUDY='' then '一般' when bb.STUDY='' then '较差' end as STUDY,
case when bb.SOCIAL='' then '良好' when bb.SOCIAL='' then '一般' when bb.SOCIAL='' then '较差' end as SOCIAL,
bb.VISITDOCTOR,
case when (select Count(*) from MB_Autograph where Sfid=bb.mentalvisitid and Sf_type ='kf_mentalvisit')=1 then '电子签名' else '' end as DZQM,
bb.HZJSQM,
ROW_NUMBER() OVER(PARTITION BY aa.personid ORDER BY bb.Mentalvisitid DESC) RNO from (SELECT PERSONID,
TOWNNAME,
PERSONCODE,
PNAME,
PHONENO,
BIRTHDAY,
RESIDENCEADDRESS,
CREATEDATE,
GENDERCODE,
GENDERNAME,
DISEASE1,
DISDATE1,
DISEASE2,
DISDATE2,
DISEASE3,
DISDATE3,
DISEASE4,
DISDATE4,
DISEASE5,
DISDATE5,
DISEASE6,
DISDATE6,
IDCARDNO,VILLAGENAME,relationname,
(SELECT COUNT(A.MENTALVISITID)
FROM KF_MENTALVISIT A
WHERE A.PERSONID = P.PERSONID
AND A.ISDELETED = '' and Visitdate >= to_date('2015-05-14','yyyy-MM-dd') and Visitdate <= to_date('2018-07-24','yyyy-MM-dd')) AS VISITEDTIMES,
(SELECT B.RELATION
FROM KF_MENTAL B
WHERE B.ISDELETED = ''
AND B.PERSONID = P.PERSONID
AND ROWNUM <= 1) AS RELATION,
(SELECT B.LOCKSITUATION
FROM KF_MENTAL B
WHERE B.ISDELETED = ''
AND B.PERSONID = P.PERSONID
AND ROWNUM <= 1) AS LOCKSITUATION,
(SELECT B.RECENTDIAGNOSE
FROM KF_MENTAL B
WHERE B.ISDELETED = ''
AND B.PERSONID = P.PERSONID
AND ROWNUM <= 1) AS RECENTDIAGNOSE,
(SELECT GUARDIAN
FROM KF_MENTAL B
WHERE B.ISDELETED = ''
AND B.PERSONID = P.PERSONID
AND ROWNUM <= 1) AS GUARDIAN,
(SELECT FIRSTTIME
FROM KF_MENTAL B
WHERE B.ISDELETED = ''
AND B.PERSONID = P.PERSONID
AND ROWNUM <= 1) AS FIRSTTIME,ROW_NUMBER() OVER(ORDER BY PERSONCODE DESC) AS RN
FROM DAGL_PERSON P WHERE p.isdeleted = ''
and p.status = '正常'
and p.diseasecode like '%,8,%' and ( p.villagecode like '370783%' ) and ( p.villagecode like '370783112%' ) and (select count(t.MENTALVISITID) as visitedTimes
from KF_MENTALVISIT t
where t.isdeleted = '' and t.personid = p.personid and Visitdate >= to_date('2015-05-14','yyyy-MM-dd') and Visitdate <= to_date('2018-07-24','yyyy-MM-dd')) >= 1 and exists(select 1 from kf_mental where kf_mental.personid=p.personid) ) aa left join kf_mentalvisit bb ON aa.personid = bb.personid)C
where 1=1 and Visitdate >= to_date('2015-05-14','yyyy-MM-dd') and Visitdate <= to_date('2017-07-24','yyyy-MM-dd')

解析:

1.Case When Then end

 添加的 if else的逻辑

2. wm_concat()   , wm_concat(to_char())

  多条记录合并成一条,类似于for

3.  ||   拼接字符串用

  类似于操作符了

4. () 可以连表查字段

  类似于函数执行一部分操作

5.PARTITION BY ... ORDER BY ... DESC

  多表合并时使用的操作

6.where 条件中  exists()

7.left join  on  注意小括号的使用


sql 应用记录的更多相关文章

  1. SQL重复记录处理(查找,过滤,删除)

    SQL重复记录处理(查找,过滤,删除)     ID int    Title nvarchar(50)    AddDate datetime    数据  www.2cto.com     ID ...

  2. Yii2如何添加sql日志记录的配置信息

    在使用Yii2框架的时候,常常会出现没有sql日志记录的问题.在代码里一句一句的打印sql语句也不现实.所以就要用文件记录起来. 在 config/web.php 里面的 log配置中增加如下配置 [ ...

  3. EF Core3.0+ 通过拦截器实现读写分离与SQL日志记录

    前言 本文主要是讲解EF Core3.0+ 通过拦截器实现读写分离与SQL日志记录 注意拦截器只有EF Core3.0+ 支持,2.1请考虑上下文工厂的形式实现. 说点题外话.. 一晃又大半年没更新技 ...

  4. 如何在MySql中记录SQL日志记录

    My SQL可以用下面方法跟踪sql 语句,以下方法以Windows平台为例,linux雷同:   1  配置my.ini文件(在安装目录,linux下文件名为my.cnf     查找到[mysql ...

  5. SQL学习记录

    一些最重要的 SQL 命令 SELECT - 从数据库中提取数据 UPDATE - 更新数据库中的数据 DELETE - 从数据库中删除数据 INSERT INTO - 向数据库中插入新数据 CREA ...

  6. phalcon: plugin 结合Manager事件管理、dispatcher调度控制器 监听sql日志记录或其他拦截出来

    可能用到的类 phalcon\mvc\use\plugin Phalcon\Mvc\Dispatcher as MvcDispatcher Phalcon\Events\Manager as Even ...

  7. SQL查询记录添加序号(HANA)

    语法:ROW_NUMBER() OVER(PARTITION BY COLUMN ORDER BY COLUMN) row_number() OVER() 从1开始,为每一条分组记录返回一个数字,这里 ...

  8. SQL检索记录

    <<第一章检索记录>>:关于表使用SELECT语句和特殊字符"*": *:SELECT * from emp; 1:分别列出每一行:SELECT empno ...

  9. SQL 问题记录

    今天在处理SQL的时候遇到几个问题: 1.如果指定了 SELECT DISTINCT,那么 ORDER BY 子句中的项就必须出现在选择列表中 select distinct id from 收费站 ...

  10. sql知识点记录

    order by就是排序. group by就是分组. WHERE语句在GROUP BY语句之前:SQL会在分组之前计算WHERE语句.    HAVING语句在GROUP BY语句之后:SQL会在分 ...

随机推荐

  1. 对于搭建网站中出现“You don't have permission to access this resource.”错误提示的解决思路

    我是用的是树莓派搭载了ubuntu系统 配置php+apache的网站环境,但在登陆网站进行网站初始化设置时出现错误提示,其大意是没有访问权限,上网搜索了一系列相关教程,都说明对apache2.con ...

  2. 洛谷 UVA11388 GCD LCM

    UVA11388 GCD LCM Description of the title PDF The GCD of two positive integers is the largest intege ...

  3. TP框架上传图片至阿里云oss

    首先安装阿里云oss扩展: composer require aliyuncs/oss-sdk-php 如果这个安装不上可以直接下载SDK的包: 链接:https://pan.baidu.com/s/ ...

  4. 数据结构(集合)学习之Map(二)

    集合 框架关系图 补充:HashTable父类是Dictionary,不是AbstractMap. 一:HashMap中的链循环: 一般来说HashMap中的链循环会发生在多线程操作时(虽然HashM ...

  5. codechef Chef at the River

    难度 \(hard\) 题意 官方中文题意 做法 设\(G=(V,E)\) 定义1:\(M(G)\)为\(G\)的最小点覆盖 定义2:\(ans(G)\)为\(G\)的题意答案,不考虑船夫 结论1:\ ...

  6. 极其实用的JS单行代码

    个人博客 地址:https://www.wenhaofan.com/a/20190629211647 1.强制布尔值 要将变量强制转换为布尔值而不更改其值: const myBoolean = !!  ...

  7. 对象浅拷贝Object.assign

    const target = { a: { b: { c: { d: 1 } }, e: 5, f: 6, h: 10 } } const source = { a: { b: { c: { d: 1 ...

  8. Flink架构,源码及debug

    序 工作中用Flink做批量和流式处理有段时间了,感觉只看Flink文档是对Flink ProgramRuntime的细节描述不是很多, 程序员还是看代码最简单和有效.所以想写点东西,记录一下,如果能 ...

  9. LIS 51Nod 1134 最长递增子序列

    给出长度为N的数组,找出这个数组的最长递增子序列.(递增子序列是指,子序列的元素是递增的) 例如:5 1 6 8 2 4 5 10,最长递增子序列是1 2 4 5 10.   Input 第1行:1个 ...

  10. PTA 1003 Emergency

    问题描述 As an emergency rescue team leader of a city, you are given a special map of your country. The ...