【问题标题】:I need to figure out: making sql AND operator work我需要弄清楚:使 sql AND 运算符工作
【发布时间】:2018-11-28 22:20:09
【问题描述】:

我正在尝试使此代码正常工作。我需要 11 条记录,但通过此查询获得 28 条记录。我可以通过取消注释约会得到 11,但这会给出错误的记录。括号中的 AND 子句似乎不会过滤。谁能给我一些关于我做错了什么的想法?(不是最好的工作):

create table #enc
(birthdate date,pidd numeric(19), id numeric(20),lastn varchar(30),firstn 
varchar(30), cdate date)

insert into #enc
select pp.Birthdate,PP.PID, pp.patientprofileid, pp.last, pp.first, 
cast(CAST(DateAdd(s, CAST(Left(d.CLINICALDATE,Len(d.CLINICALDATE)-6) AS 
DECIMAL(20,0)),'Jan 01 1960') AS DATE) as DATE) as visit
 FROM DOCUMENT d
--INNER JOIN Appointments a
--ON d.AppointmentsId = a.AppointmentsId
INNER JOIN PatientVisit v
ON d.PatientVisitId = v.PatientVisitId
inner join patientprofile pp on pp.PId = d.PID
inner join ORDERS o on o.PID= pp.PId
inner join ORDERCODES oc on oc.CODE = o.CODE

where DOCTYPE in(1)  and d.status in( 's','u','h','a') and d.USRID 
=xxxxx

((((and  o.CODE not in('00100','00103','00110','00113','00126'))))

 and oc.ORDERTYPE not in('t')

AND CAST(VISIT AS DATE) BETWEEN '10/01/2018' AND '10/31/2018' 
    
   
    
 select distinct cdate, * from #enc order by lastn

【问题讨论】:

  • 您的示例没有按原样运行。请发布一个工作示例。
  • 显然不是每个文档都需要预约,这解释了为什么连接会减少行数。不过,您仍然需要向我们提供有关这 17 个额外行的更多信息。
  • 请阅读this,了解一些改进问题的技巧。
  • 请不要破坏您的帖子。提交帖子后,您已将内容授权给整个 Stack Overflow 社区 (under the CC BY-SA license)。根据 Stack Exchange 政策,任何破坏行为都将被撤销。

标签: sql tsql sql-server-2008


【解决方案1】:

可能 Document 表和 Appointments 表具有多对多关系,这意味着一个文档可能有多个与其链接的约会记录。这将导致重复记录。试试这个:

create table #enc
(birthdate date,pidd numeric(19), id numeric(20),lastn varchar(30),first 
 nvarchar(30), cdate date)

insert into #enc
select 
    pp.Birthdate,PP.PID, pp.patientprofileid, pp.last, pp.first, 
    cast(CAST(DateAdd(s, CAST(Left(d.CLINICALDATE,Len(d.CLINICALDATE)-6) AS DECIMAL(20,0)),'Jan 01 1960') AS DATE) as DATE) as visit
FROM DOCUMENT d
INNER JOIN ( 
    select distinct AppointmentsId from Appointments
) a ON d.AppointmentsId = a.AppointmentsId
INNER JOIN PatientVisit v
ON d.PatientVisitId = v.PatientVisitId
inner join patientprofile pp on pp.PId = d.PID
inner join ORDERS o on o.PID= pp.PId
inner join ORDERCODES oc on oc.CODE = o.CODE

where DOCTYPE in(1)  and d.status in( 's','u','h','a') and d.USRID 
=xxxxx

((((and  o.CODE not in('00100','00103','00110','00113','00126'))))

 and oc.ORDERTYPE not in('t')

AND CAST(VISIT AS DATE) BETWEEN '10/01/2018' AND '10/31/2018' 



 select distinct cdate, * from #enc order by lastn

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-28
    • 1970-01-01
    • 2017-05-17
    • 1970-01-01
    • 1970-01-01
    • 2020-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多