Sql server Microsoft SQL查询困境

Sql server Microsoft SQL查询困境,sql-server,Sql Server,我有4张桌子: tblWorkArea (*WA, WA.Description, Unitcode*) tblWAFileNumber (*WA, FNo, Fno.Description*) tblElement (*WA, FNo, Tasklist, Task, Element, Code*) tblMiscValues (*WA, FNo, FieldLabel, Datavalue*). 我需要从这四个表中提取一份报告,将WA、FNo字段连接起来 在包含以下列的所有表中通用: WA

我有4张桌子:

tblWorkArea (*WA, WA.Description, Unitcode*)
tblWAFileNumber (*WA, FNo, Fno.Description*)
tblElement (*WA, FNo, Tasklist, Task, Element, Code*)
tblMiscValues (*WA, FNo, FieldLabel, Datavalue*).
我需要从这四个表中提取一份报告,将WA、FNo字段连接起来

在包含以下列的所有表中通用:

WA,WA.说明,FNo,FNo.说明,任务列表,任务,元素,代码,数据值-其中Unitcod='pq'和FieldLabel='xyz'和数据值不应为空

我尝试了连接的组合,但它仍然没有以某种方式提取我需要的报告并提取重复记录

来自注释:这是当前查询:

select e.WA, w.description as WorkArea_Description
     , e.FNo
     , f.description as FileNumber_Description
     , e.tasklist
     , e.task
     , element
     , code
     , datavalue as Partnumber 
from tblElement e 
join tblworkarea w on e.WA=w.WA and 
join tblWAFileNumber f on e.WA=f.WA and e.Fno=f.FNo and 
join tblMiscValues m on e.WA=m.WA and e.FNo=m.FNo 
where UnitCode='pq' FieldLabel='xyz' 
  and DataValue not in ('Null','')

如果只需要匹配的键,则可以使用内部联接

select *
from tblWAFileNumber t1
inner join tblElement t2 on t1.WA = t2.WA and t1.FNo = t2.FNo
inner join tblMiscValues t3 on t1.WA = t3.WA and t1.FNo = t3.FNo
inner join tblWorkArea t4 on t1.WA = t4.WA
where t4.Unitcode = 'pq'
and t3.FieldLabel='xyz'
and t3.Datavalue is not null 
或者,如果还需要不匹配与键相关的行,请使用左连接

select *
from tblWAFileNumber t1
left join tblElement t2 on t1.WA = t2.WA and t1.FNo = t2.FNo
left join tblMiscValues t3 on t1.WA = t3.WA and t1.FNo = t3.FNo 
    and  t3.FieldLabel='xyz' and t3.Datavalue is not null 
left  join tblWorkArea t4 on t1.WA = t4.WA and t4.Unitcode = 'pq'

只是为了解决出现的语法错误,我重新设置了格式,以便能够更快地识别语法问题

SELECT e.WA, w.description as WorkArea_Description
     , e.FNo
     , f.description as FileNumber_Description
     , e.tasklist
     , e.task
     , element
     , code
     , datavalue as Partnumber 
FROM tblElement e 
INNER JOIN tblworkarea w  
   on e.WA=w.WA 
-- and  had an extra and.
INNER JOIN tblWAFileNumber f 
   on e.WA=f.WA 
  and e.Fno=f.FNo --had an extra and here.
INNER JOIN tblMiscValues m 
   on e.WA=m.WA 
  and e.FNo=m.FNo 
WHERE UnitCode='pq' 
  and FieldLabel='xyz' --missing the leading and here.
  and DataValue not in ('Null','') --this just seems odd to check for null string and empty set.. I'd need to see sample data containing null string and empty set to understand what you're trying to do here.
--and DateValue <> ''  --maybe this 
--and Datevalue is not null --with this instead?

当涉及多个表时,明智的做法是完全限定源表中的元素,因为我们不知道哪些值来自哪些表。因此,元素、代码、日期值需要定义源表。在select和where子句中,所有值都需要完全限定。

发布当前查询,以便我们可以帮助您。这是MySQL或sql server?您好,欢迎使用So。我们需要更多的细节来提供帮助。这是一个很好的起点。如果不发布查询,则很难找到查询的错误。显示示例数据和所需结果,以及发布您尝试的任何和所有查询。此答案不正确,使用WHERE子句左连接,与内部连接相同。而且它没有解决重复的问题。非常感谢您的快速响应。我尝试运行这两个查询,但不幸的是,这两个查询都没有记录我真的很抱歉,但我无法在此处添加示例数据,但Datavalue字段确实包含Null、、*、-。。等但是,报告应该删除所有这些垃圾值,并且应该只包括那些具有定义良好的值的记录。示例数据值为“DQ01054”。因此,我需要删除Datavalue字段中没有采用这种格式的所有内容。谢谢您的帮助。Datavalue Allay是否应该为7个字符?目前还没有足够的信息来说明哪些数据是坏数据。NULL和'NULL'不是一回事。NULL表示缺少任何数据。是空集,“NULL”是一个字符串,因此可能和lentrimdatevalue>7或length而不是len,具体取决于db。非常感谢。。将尝试使用此..: