SQL Left Join问题 解决立马结帖

happy664618843 2017-07-16 12:26:07
EMR_ProcessUReadMessage表数据如下:一共6条
EMR_Code CreateByPlatform IsRead
201707130001 Doctor 1
201707130001 Desgin 0
201707130001 Medicine 1
201707130001 Doctor 1
201707130001 Desgin 0
201707130001 Medicine 1


doctor_essays 表 两条记录:数据如下
EMR_Code doctorRemarkDate doctorRemark
201707130001 2017.07.14 14:19:00 添加一条未读信息
201707130001 2017.07.14 14:08:42 测试未读

simple_EMR表数据只有一条:
EMR_Code patient_Name patient_Age patient_Sex patient_Tel
201707130001 测试医学服务中心New 2017-08-16 1 15221105698

SQL语句如下:
 SELECT    dess.EMR_Code , H.isRead, 'Remark' as RecordType,H.CreateByPlatform,
dess.doctorRemark AS 'message' ,
dess.createDate

FROM dbo.doctor_essays dess

LEFT join dbo.simple_EMR E on dess.EMR_Code=E.EMR_Code
LEFT join EMR_ProcessUReadMessage H on H.EMR_Code=dess.EMR_Code
WHERE dess.EMR_Code = '201707130001' and H.CreateByPlatform='Doctor'
AND dess.flag = 1


查询出来的结果如下:
EMR_Code isRead RecordType CreateByPlatform message createDate
201707130001 1 Remark Doctor 添加一条未读信息 2017-07-14 14:19:04.167
201707130001 1 Remark Doctor 添加一条未读信息 2017-07-14 14:19:04.167
201707130001 1 Remark Doctor 测试未读 2017-07-14 14:08:45.170
201707130001 1 Remark Doctor 测试未读 2017-07-14 14:08:45.170

最终我想得到结果如下:请问各位高手如何实现??
EMR_Code isRead RecordType CreateByPlatform message createDate
201707130001 1 Remark Doctor 添加一条未读信息 2017-07-14 14:19:04.167
201707130001 1 Remark Doctor 测试未读 2017-07-14 14:08:45.170
...全文
417 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
happy664618843 2017-07-19
  • 打赏
  • 举报
回复
顶 有没有高手来啊
OwenZeng_DBA 2017-07-18
  • 打赏
  • 举报
回复
你把这个SQL 拆分下吧,,join太多表,逻辑也复杂,如果数据量变大,性能也会存在隐患
happy664618843 2017-07-17
  • 打赏
  • 举报
回复
引用 8 楼 sinat_28984567 的回复:
随便改了一个doctor是1的改成0 了
--测试数据
if not object_id(N'Tempdb..#EMR_ProcessUReadMessage') is null
    drop table #EMR_ProcessUReadMessage
Go
Create table #EMR_ProcessUReadMessage([EMR_Code] bigint,[CreateByPlatform] nvarchar(28),[IsRead] int)
Insert #EMR_ProcessUReadMessage
select 201707130001,N'Doctor',1 union all
select 201707130001,N'Desgin',0 union all
select 201707130001,N'Medicine',1 union all
select 201707130001,N'Doctor',0 union all
select 201707130001,N'Desgin',0 union all
select 201707130001,N'Medicine',1
GO
if not object_id(N'Tempdb..#doctor_essays') is null
    drop table #doctor_essays
Go
 CREATE TABLE #doctor_essays([EMR_Code] bigint,doctorRemarkDate DATETIME,doctorRemark NVARCHAR(100))
Insert #doctor_essays
select 201707130001,'2017.07.14 14:19:00',N'添加一条未读信息' union all
select 201707130001,'2017.07.14 14:08:42',N'测试未读'
GO
if not object_id(N'Tempdb..#simple_EMR') is null
    drop table #simple_EMR
Go
Create table #simple_EMR([EMR_Code] bigint,[patient_Name] nvarchar(31),[patient_Age] Date,[patient_Sex] bigint,[patient_Tel] bigint)
Insert #simple_EMR
select 201707130001,N'测试医学服务中心New','2017-08-16',1,15221105698
Go
--测试数据结束
;WITH ctea AS (
SELECT *,ROW_NUMBER()OVER(PARTITION BY [EMR_Code],[CreateByPlatform] ORDER BY (SELECT 1)) AS num FROM #EMR_ProcessUReadMessage
),cteb AS (
SELECT *,ROW_NUMBER()OVER(PARTITION BY [EMR_Code] ORDER BY doctorRemarkDate) AS num  FROM #doctor_essays
)
SELECT  dess.EMR_Code ,
        H.isRead ,
        'Remark' AS RecordType ,
        H.CreateByPlatform ,
        dess.doctorRemark AS 'message'
FROM    cteb dess
        LEFT JOIN #simple_EMR E ON dess.EMR_Code = E.EMR_Code
        LEFT JOIN ctea H ON H.EMR_Code = dess.EMR_Code AND H.num = dess.num
WHERE   dess.EMR_Code = '201707130001'
        AND H.CreateByPlatform = 'Doctor'
                                               
亲 你好,谢谢,结果是对,我发帖的时候,SQL精简了,以下SQL如何改成我想要的结果??
SELECT  distinct *
                            FROM    (
							
								 SELECT  e.patient_Name as patientName,e.patient_Sex as patientSex,e.patient_Tel as patientTel,   es.EMR_Code , H.isRead, 'Process' as RecordType,H.CreateByPlatform,
                                                es.message ,
                                                es.createDate ,
                                                CASE WHEN d.userRealName IS NULL
                                                          OR d.userRealName = ''
                                                     THEN CASE WHEN f.factoryName IS NULL
                                                                    OR f.factoryName = ''
                                                               THEN CASE WHEN der.userRealName IS NULL
                                                                              OR der.userRealName = ''
                                                                         THEN  es.Designer
                                                                    END
                                                               ELSE f.factoryName
                                                          END
                                                     ELSE d.userRealName
                                                END AS doctorName
                                      FROM      dbo.EMR_stream es
                                                LEFT JOIN dbo.doctors d ON d.Id = es.createId
                                                                           AND d.flag = 1
                                                LEFT JOIN dbo.factory f ON f.Id = es.createId
                                                                           AND f.flag = 1
                                                LEFT JOIN dbo.designers der ON der.Id = es.createId
                                                                               AND der.flag = 1
                                                LEFT join dbo.simple_EMR  E on es.EMR_Code=E.EMR_Code
                                                Left Join  EMR_ProcessUReadMessage H on H.EMR_Code=es.EMR_Code
                                      WHERE     es.EMR_Code = '201707130001'  and   H.CreateByPlatform='Doctor'
                                                AND es.flag = 1
                                      UNION
                                      SELECT   e.patient_Name,E.patient_Sex,e.patient_Tel, dess.EMR_Code , H.isRead, 'Remark' as RecordType,H.CreateByPlatform,
                                                dess.doctorRemark AS 'message' ,
                                                dess.createDate ,
                                                CASE WHEN d.userRealName IS NULL
                                                          OR d.userRealName = ''
                                                     THEN CASE WHEN der.userRealName IS NULL
                                                                    OR der.userRealName = '' THEN   isnull(dess.Designer,'作者') 
                                                               ELSE der.userRealName
                                                          END
                                                     ELSE d.userRealName
                                                END AS doctorName
                                      FROM      dbo.doctor_essays dess
                                                LEFT JOIN dbo.doctors d ON d.Id = dess.createId
                                                                           AND d.flag = 1
                                                LEFT JOIN dbo.designers der ON der.Id = dess.createId
                                                                               AND der.flag = 1
                                                LEFT join dbo.simple_EMR  E on dess.EMR_Code=E.EMR_Code
                                                LEFT join  EMR_ProcessUReadMessage H on H.EMR_Code=dess.EMR_Code
                                      WHERE     dess.EMR_Code = '201707130001'  and   H.CreateByPlatform='Doctor'
                                                AND dess.flag = 1
                                    ) a
                            ORDER BY a.createDate ASC
二月十六 版主 2017-07-17
  • 打赏
  • 举报
回复
随便改了一个doctor是1的改成0 了
--测试数据
if not object_id(N'Tempdb..#EMR_ProcessUReadMessage') is null
drop table #EMR_ProcessUReadMessage
Go
Create table #EMR_ProcessUReadMessage([EMR_Code] bigint,[CreateByPlatform] nvarchar(28),[IsRead] int)
Insert #EMR_ProcessUReadMessage
select 201707130001,N'Doctor',1 union all
select 201707130001,N'Desgin',0 union all
select 201707130001,N'Medicine',1 union all
select 201707130001,N'Doctor',0 union all
select 201707130001,N'Desgin',0 union all
select 201707130001,N'Medicine',1
GO
if not object_id(N'Tempdb..#doctor_essays') is null
drop table #doctor_essays
Go
CREATE TABLE #doctor_essays([EMR_Code] bigint,doctorRemarkDate DATETIME,doctorRemark NVARCHAR(100))
Insert #doctor_essays
select 201707130001,'2017.07.14 14:19:00',N'添加一条未读信息' union all
select 201707130001,'2017.07.14 14:08:42',N'测试未读'
GO
if not object_id(N'Tempdb..#simple_EMR') is null
drop table #simple_EMR
Go
Create table #simple_EMR([EMR_Code] bigint,[patient_Name] nvarchar(31),[patient_Age] Date,[patient_Sex] bigint,[patient_Tel] bigint)
Insert #simple_EMR
select 201707130001,N'测试医学服务中心New','2017-08-16',1,15221105698
Go
--测试数据结束
;WITH ctea AS (
SELECT *,ROW_NUMBER()OVER(PARTITION BY [EMR_Code],[CreateByPlatform] ORDER BY (SELECT 1)) AS num FROM #EMR_ProcessUReadMessage
),cteb AS (
SELECT *,ROW_NUMBER()OVER(PARTITION BY [EMR_Code] ORDER BY doctorRemarkDate) AS num FROM #doctor_essays
)
SELECT dess.EMR_Code ,
H.isRead ,
'Remark' AS RecordType ,
H.CreateByPlatform ,
dess.doctorRemark AS 'message'
FROM cteb dess
LEFT JOIN #simple_EMR E ON dess.EMR_Code = E.EMR_Code
LEFT JOIN ctea H ON H.EMR_Code = dess.EMR_Code AND H.num = dess.num
WHERE dess.EMR_Code = '201707130001'
AND H.CreateByPlatform = 'Doctor'



happy664618843 2017-07-17
  • 打赏
  • 举报
回复
引用 6 楼 sinat_28984567 的回复:
[quote=引用 5 楼 happy664618843 的回复:] 不是去重的问题,是我没有描述清楚,数据查询出来是这样的: 查询出来的结果如下: EMR_Code isRead RecordType CreateByPlatform message createDate 201707130001 0 Remark Doctor 添加一条未读信息 2017-07-14 14:19:04.167 201707130001 1 Remark Doctor 添加一条未读信息 2017-07-14 14:19:04.167 201707130001 0 Remark Doctor 测试未读 2017-07-14 14:08:45.170 201707130001 1 Remark Doctor 测试未读 2017-07-14 14:08:45.170 最终我想得到结果如下:请问各位高手如何实现?? EMR_Code isRead RecordType CreateByPlatform message createDate 201707130001 1 Remark Doctor 添加一条未读信息 2017-07-14 14:19:04.167 201707130001 1 Remark Doctor 测试未读 2017-07-14 14:08:45.170
楼主我用你的测试数据和sql语句查询出来的美誉 10 10 这样的就是1111; EMR_ProcessUReadMessage 里边doctor的isread全是1没有0的[/quote] 你好,EMR_ProcessUReadMessage doctor的isRead字段有1 和0的,若全是1,我可以用distinct.之前是因为数据搞错了。
二月十六 版主 2017-07-17
  • 打赏
  • 举报
回复
引用 5 楼 happy664618843 的回复:
不是去重的问题,是我没有描述清楚,数据查询出来是这样的: 查询出来的结果如下: EMR_Code isRead RecordType CreateByPlatform message createDate 201707130001 0 Remark Doctor 添加一条未读信息 2017-07-14 14:19:04.167 201707130001 1 Remark Doctor 添加一条未读信息 2017-07-14 14:19:04.167 201707130001 0 Remark Doctor 测试未读 2017-07-14 14:08:45.170 201707130001 1 Remark Doctor 测试未读 2017-07-14 14:08:45.170 最终我想得到结果如下:请问各位高手如何实现?? EMR_Code isRead RecordType CreateByPlatform message createDate 201707130001 1 Remark Doctor 添加一条未读信息 2017-07-14 14:19:04.167 201707130001 1 Remark Doctor 测试未读 2017-07-14 14:08:45.170
楼主我用你的测试数据和sql语句查询出来的美誉 10 10 这样的就是1111; EMR_ProcessUReadMessage 里边doctor的isread全是1没有0的
happy664618843 2017-07-17
  • 打赏
  • 举报
回复
引用 3 楼 sinat_28984567 的回复:
--测试数据
if not object_id(N'Tempdb..#EMR_ProcessUReadMessage') is null
	drop table #EMR_ProcessUReadMessage
Go
Create table #EMR_ProcessUReadMessage([EMR_Code] bigint,[CreateByPlatform] nvarchar(28),[IsRead] int)
Insert #EMR_ProcessUReadMessage
select 201707130001,N'Doctor',1 union all
select 201707130001,N'Desgin',0 union all
select 201707130001,N'Medicine',1 union all
select 201707130001,N'Doctor',1 union all
select 201707130001,N'Desgin',0 union all
select 201707130001,N'Medicine',1
GO
if not object_id(N'Tempdb..#doctor_essays') is null
	drop table #doctor_essays
Go
 CREATE TABLE #doctor_essays([EMR_Code] bigint,doctorRemarkDate DATETIME,doctorRemark NVARCHAR(100))
Insert #doctor_essays
select 201707130001,'2017.07.14 14:19:00',N'添加一条未读信息' union all
select 201707130001,'2017.07.14 14:08:42',N'测试未读'
GO
if not object_id(N'Tempdb..#simple_EMR') is null
	drop table #simple_EMR
Go
Create table #simple_EMR([EMR_Code] bigint,[patient_Name] nvarchar(31),[patient_Age] Date,[patient_Sex] bigint,[patient_Tel] bigint)
Insert #simple_EMR
select 201707130001,N'测试医学服务中心New','2017-08-16',1,15221105698
Go
--测试数据结束
SELECT DISTINCT dess.EMR_Code ,
        H.IsRead ,
        'Remark' AS RecordType ,
        H.CreateByPlatform ,
        dess.doctorRemark AS 'message'
FROM    #doctor_essays dess
        LEFT JOIN #simple_EMR E ON dess.EMR_Code = E.EMR_Code
        LEFT JOIN #EMR_ProcessUReadMessage H ON H.EMR_Code = dess.EMR_Code
WHERE   dess.EMR_Code = '201707130001'
        AND H.CreateByPlatform = 'Doctor';
                                            
不是去重的问题,是我没有描述清楚,数据查询出来是这样的: 查询出来的结果如下: EMR_Code isRead RecordType CreateByPlatform message createDate 201707130001 0 Remark Doctor 添加一条未读信息 2017-07-14 14:19:04.167 201707130001 1 Remark Doctor 添加一条未读信息 2017-07-14 14:19:04.167 201707130001 0 Remark Doctor 测试未读 2017-07-14 14:08:45.170 201707130001 1 Remark Doctor 测试未读 2017-07-14 14:08:45.170 最终我想得到结果如下:请问各位高手如何实现?? EMR_Code isRead RecordType CreateByPlatform message createDate 201707130001 1 Remark Doctor 添加一条未读信息 2017-07-14 14:19:04.167 201707130001 1 Remark Doctor 测试未读 2017-07-14 14:08:45.170
happy664618843 2017-07-17
  • 打赏
  • 举报
回复
引用 楼主 happy664618843 的回复:
EMR_ProcessUReadMessage表数据如下:一共6条 EMR_Code CreateByPlatform IsRead 201707130001 Doctor 1 201707130001 Desgin 0 201707130001 Medicine 1 201707130001 Doctor 1 201707130001 Desgin 0 201707130001 Medicine 1 doctor_essays 表 两条记录:数据如下 EMR_Code doctorRemarkDate doctorRemark 201707130001 2017.07.14 14:19:00 添加一条未读信息 201707130001 2017.07.14 14:08:42 测试未读 simple_EMR表数据只有一条: EMR_Code patient_Name patient_Age patient_Sex patient_Tel 201707130001 测试医学服务中心New 2017-08-16 1 15221105698 SQL语句如下:
 SELECT    dess.EMR_Code , H.isRead, 'Remark' as RecordType,H.CreateByPlatform,
                                                dess.doctorRemark AS 'message' ,
                                                dess.createDate 
                                                
                                      FROM      dbo.doctor_essays dess
                                               
                                                LEFT join dbo.simple_EMR  E on dess.EMR_Code=E.EMR_Code
                                                LEFT join  EMR_ProcessUReadMessage H on H.EMR_Code=dess.EMR_Code
                                      WHERE     dess.EMR_Code = '201707130001'  and   H.CreateByPlatform='Doctor'
                                                AND dess.flag = 1
查询出来的结果如下: EMR_Code isRead RecordType CreateByPlatform message createDate 201707130001 1 Remark Doctor 添加一条未读信息 2017-07-14 14:19:04.167 201707130001 1 Remark Doctor 添加一条未读信息 2017-07-14 14:19:04.167 201707130001 1 Remark Doctor 测试未读 2017-07-14 14:08:45.170 201707130001 1 Remark Doctor 测试未读 2017-07-14 14:08:45.170 最终我想得到结果如下:请问各位高手如何实现?? EMR_Code isRead RecordType CreateByPlatform message createDate 201707130001 1 Remark Doctor 添加一条未读信息 2017-07-14 14:19:04.167 201707130001 1 Remark Doctor 测试未读 2017-07-14 14:08:45.170
[/quote] 不是去重的问题,是我没有描述清楚,数据查询出来是这样的: 查询出来的结果如下: EMR_Code isRead RecordType CreateByPlatform message createDate 201707130001 0 Remark Doctor 添加一条未读信息 2017-07-14 14:19:04.167 201707130001 1 Remark Doctor 添加一条未读信息 2017-07-14 14:19:04.167 201707130001 0 Remark Doctor 测试未读 2017-07-14 14:08:45.170 201707130001 1 Remark Doctor 测试未读 2017-07-14 14:08:45.170 最终我想得到结果如下:请问各位高手如何实现?? EMR_Code isRead RecordType CreateByPlatform message createDate 201707130001 1 Remark Doctor 添加一条未读信息 2017-07-14 14:19:04.167 201707130001 1 Remark Doctor 测试未读 2017-07-14 14:08:45.170
二月十六 版主 2017-07-16
  • 打赏
  • 举报
回复
SELECT  DISTINCT
        dess.EMR_Code ,
        H.isRead ,
        'Remark' AS RecordType ,
        H.CreateByPlatform ,
        dess.doctorRemark AS 'message' ,
        dess.createDate
FROM    dbo.doctor_essays dess
        LEFT JOIN dbo.simple_EMR E ON dess.EMR_Code = E.EMR_Code
        LEFT JOIN EMR_ProcessUReadMessage H ON H.EMR_Code = dess.EMR_Code
WHERE   dess.EMR_Code = '201707130001'
        AND H.CreateByPlatform = 'Doctor'
        AND dess.flag = 1;
OwenZeng_DBA 2017-07-16
  • 打赏
  • 举报
回复
引用 楼主 happy664618843 的回复:
EMR_ProcessUReadMessage表数据如下:一共6条 EMR_Code CreateByPlatform IsRead 201707130001 Doctor 1 201707130001 Desgin 0 201707130001 Medicine 1 201707130001 Doctor 1 201707130001 Desgin 0 201707130001 Medicine 1 doctor_essays 表 两条记录:数据如下 EMR_Code doctorRemarkDate doctorRemark 201707130001 2017.07.14 14:19:00 添加一条未读信息 201707130001 2017.07.14 14:08:42 测试未读 simple_EMR表数据只有一条: EMR_Code patient_Name patient_Age patient_Sex patient_Tel 201707130001 测试医学服务中心New 2017-08-16 1 15221105698 SQL语句如下:
 SELECT    dess.EMR_Code , H.isRead, 'Remark' as RecordType,H.CreateByPlatform,
                                                dess.doctorRemark AS 'message' ,
                                                dess.createDate 
                                                
                                      FROM      dbo.doctor_essays dess
                                               
                                                LEFT join dbo.simple_EMR  E on dess.EMR_Code=E.EMR_Code
                                                LEFT join  EMR_ProcessUReadMessage H on H.EMR_Code=dess.EMR_Code
                                      WHERE     dess.EMR_Code = '201707130001'  and   H.CreateByPlatform='Doctor'
                                                AND dess.flag = 1
查询出来的结果如下: EMR_Code isRead RecordType CreateByPlatform message createDate 201707130001 1 Remark Doctor 添加一条未读信息 2017-07-14 14:19:04.167 201707130001 1 Remark Doctor 添加一条未读信息 2017-07-14 14:19:04.167 201707130001 1 Remark Doctor 测试未读 2017-07-14 14:08:45.170 201707130001 1 Remark Doctor 测试未读 2017-07-14 14:08:45.170 最终我想得到结果如下:请问各位高手如何实现?? EMR_Code isRead RecordType CreateByPlatform message createDate 201707130001 1 Remark Doctor 添加一条未读信息 2017-07-14 14:19:04.167 201707130001 1 Remark Doctor 测试未读 2017-07-14 14:08:45.170
前面加个去重就可以 SELECT distinct dess.EMR_Code , H.isRead, 'Remark' as RecordType,H.CreateByPlatform, dess.doctorRemark AS 'message' , dess.createDate FROM dbo.doctor_essays dess LEFT join dbo.simple_EMR E on dess.EMR_Code=E.EMR_Code LEFT join EMR_ProcessUReadMessage H on H.EMR_Code=dess.EMR_Code WHERE dess.EMR_Code = '201707130001' and H.CreateByPlatform='Doctor' AND dess.flag = 1
二月十六 版主 2017-07-16
  • 打赏
  • 举报
回复
--测试数据
if not object_id(N'Tempdb..#EMR_ProcessUReadMessage') is null
drop table #EMR_ProcessUReadMessage
Go
Create table #EMR_ProcessUReadMessage([EMR_Code] bigint,[CreateByPlatform] nvarchar(28),[IsRead] int)
Insert #EMR_ProcessUReadMessage
select 201707130001,N'Doctor',1 union all
select 201707130001,N'Desgin',0 union all
select 201707130001,N'Medicine',1 union all
select 201707130001,N'Doctor',1 union all
select 201707130001,N'Desgin',0 union all
select 201707130001,N'Medicine',1
GO
if not object_id(N'Tempdb..#doctor_essays') is null
drop table #doctor_essays
Go
CREATE TABLE #doctor_essays([EMR_Code] bigint,doctorRemarkDate DATETIME,doctorRemark NVARCHAR(100))
Insert #doctor_essays
select 201707130001,'2017.07.14 14:19:00',N'添加一条未读信息' union all
select 201707130001,'2017.07.14 14:08:42',N'测试未读'
GO
if not object_id(N'Tempdb..#simple_EMR') is null
drop table #simple_EMR
Go
Create table #simple_EMR([EMR_Code] bigint,[patient_Name] nvarchar(31),[patient_Age] Date,[patient_Sex] bigint,[patient_Tel] bigint)
Insert #simple_EMR
select 201707130001,N'测试医学服务中心New','2017-08-16',1,15221105698
Go
--测试数据结束
SELECT DISTINCT dess.EMR_Code ,
H.IsRead ,
'Remark' AS RecordType ,
H.CreateByPlatform ,
dess.doctorRemark AS 'message'
FROM #doctor_essays dess
LEFT JOIN #simple_EMR E ON dess.EMR_Code = E.EMR_Code
LEFT JOIN #EMR_ProcessUReadMessage H ON H.EMR_Code = dess.EMR_Code
WHERE dess.EMR_Code = '201707130001'
AND H.CreateByPlatform = 'Doctor';



34,837

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧