Sql server SQL Server-Subselect返回多行。如何正确选择所有?

Sql server SQL Server-Subselect返回多行。如何正确选择所有?,sql-server,tsql,Sql Server,Tsql,与这个问题相关的是“TATTORTIES”表 它具有以下属性,例如: GUID|Attribute|RelatedGUID|.... 1 | Val1 | 2 | 3 | Val2 | NULL | 4 | val3 | 5 | Val1是一个属性,其中RelatedGUID为NULL的Val2是一个类别 实例,即: F0F9EA32-C3AC-48A9-B6BE-09807B720818| gaming | 09E8

与这个问题相关的是“TATTORTIES”表

它具有以下属性,例如:

GUID|Attribute|RelatedGUID|....
1   | Val1    | 2         |
3   | Val2    | NULL      |
4   | val3    | 5         |  
Val1是一个属性,其中RelatedGUID为NULL的Val2是一个类别

实例,即:

F0F9EA32-C3AC-48A9-B6BE-09807B720818| gaming | 09E898CC-5DE2-4664-B9B2-14F17FBC37DB
32B74398-83C9-4225-81E2-0A1CB6C67954 | Function skills | NULL
“游戏”是一种属性

“功能技能”是一个范畴

我如何再选择它,以便在列属性中,我将只具有属性、RelatedGUID不同于NULL的值,以及在列类别、类别中,具有RelatedGUID eqaul到NULL的值

我有一个疑问:

select tsearch.Description,

tcompany.CompanyName,
tcompany.GUID as CompanyGuid,
tcompanylocation.LocationName,
tsearchtype.SearchType,
tsearchresult.searchresult,
tpeople.GUID as PersonPlacedGuid,
tpeople.LastName As PersonPlacedLName,
tpeople.Firstname As PersonPlacedFName,
tsearch.SearchNotes,
( select tpeople.Firstname  from tpeople where tpeople.guid=tSearch.RepresentativeGUID) as repfirstname, 
( select tpeople.Lastname from tpeople where tpeople.guid=tSearch.RepresentativeGUID) as replastname,
tsearch.RepresentativeGUID as RepGuid,
tposition.Position as backgroundposition,
tdepartment.Department as backgrounddepartment,
( select tpeople.Lastname from tpeople where tpeople.guid=tSearch.ReferredByGUID) as referredbylastname,
( select tpeople.FirstName from tpeople where tpeople.guid=tSearch.ReferredByGUID) as referredbylastname,
tsearch.ReferredByGUID as PersonwhorefferedGuid,
( select tcompany.CompanyName from tCompany where tCompany.guid=tSearch.PlacedFromGUID) as placedfrom,
tinstantstatustype.InstantStatus,
tWorkbench.WorkbenchName,
( select tpeople.Lastname from tpeople where tpeople.guid=tInstantStatus.PeopleGUID) as Candlastname,
( select tpeople.FirstName from tpeople where tpeople.guid=tInstantStatus.PeopleGUID) as candFirstname,
tInstantStatus.ForClientNotes,
tinstantstatus.InstantStatusNotes as InstanttatusNotesSummary,
( select top 1 tAttributes.Attribute from tAttributes where tAttributes.RelatedGUID is not NUll ) as Attributes,
( select top 1 tAttributes.Attribute from tAttributes where tAttributes.RelatedGUID is  NUll ) as Categories


from tSearch


full join tCompany on tsearch.CompanyGUID = tcompany.guid
full join tcompanylocation on tcompanylocation.guid= tcompany.LocationGUID
full join tSearchType on tsearchtype.GUID = tSearch.SearchTypeGUID
full join tSearchResult on tSearchResult.GUID = tsearch.SearchResultGUID
full join tPeople on tPeople.GUID = tsearch.PlacedGUID
full join tPosition on tPosition.GUID = tsearch.PositionGUID
full join tDepartment on tdepartment.GUID = tsearch.DepartmentGUID
full join tInstantStatus on tInstantStatus.SearchGUID = tSearch.guid
full join tInstantStatusType on tInstantStatusType.GUID = tInstantStatus.InstantStatusGUID 
full join tWorkbench on tWorkbench.SearchGUID=tsearch.GUID
full join tSearchCluendex on tSearchCluendex.CPSGUID=tsearch.GUID
full join tAttributes on tAttributes.GUID=tSearchCluendex.AttributeGUID
在这里,我使用了“top 1”技巧,但问题是类别多于1,因此“top 1”不是一个完整的解决方案:

( select top 1 tAttributes.Attribute from tAttributes where tAttributes.RelatedGUID is not NUll ) as Attributes,
( select top 1 tAttributes.Attribute from tAttributes where tAttributes.RelatedGUID is  NUll ) as Categories
select tsearch.Description,

tcompany.CompanyName,
tcompany.GUID as CompanyGuid,
tcompanylocation.LocationName,
tsearchtype.SearchType,
tsearchresult.searchresult,
tpeople.GUID as PersonPlacedGuid,
tpeople.LastName As PersonPlacedLName,
tpeople.Firstname As PersonPlacedFName,
tsearch.SearchNotes,
( select tpeople.Firstname  from tpeople where tpeople.guid=tSearch.RepresentativeGUID) as repfirstname, 
( select tpeople.Lastname from tpeople where tpeople.guid=tSearch.RepresentativeGUID) as replastname,
tsearch.RepresentativeGUID as RepGuid,
tposition.Position as backgroundposition,
tdepartment.Department as backgrounddepartment,
( select tpeople.Lastname from tpeople where tpeople.guid=tSearch.ReferredByGUID) as referredbylastname,
( select tpeople.FirstName from tpeople where tpeople.guid=tSearch.ReferredByGUID) as referredbylastname,
tsearch.ReferredByGUID as PersonwhorefferedGuid,
( select tcompany.CompanyName from tCompany where tCompany.guid=tSearch.PlacedFromGUID) as placedfrom,
tinstantstatustype.InstantStatus,
tWorkbench.WorkbenchName,
( select tpeople.Lastname from tpeople where tpeople.guid=tInstantStatus.PeopleGUID) as Candlastname,
( select tpeople.FirstName from tpeople where tpeople.guid=tInstantStatus.PeopleGUID) as candFirstname,
tInstantStatus.ForClientNotes,
tinstantstatus.InstantStatusNotes as InstanttatusNotesSummary,
a1.Attribute as Attributes,
a2.Attribute as Categories


from tSearch


full join tCompany on tsearch.CompanyGUID = tcompany.guid
full join tcompanylocation on tcompanylocation.guid= tcompany.LocationGUID
full join tSearchType on tsearchtype.GUID = tSearch.SearchTypeGUID
full join tSearchResult on tSearchResult.GUID = tsearch.SearchResultGUID
full join tPeople on tPeople.GUID = tsearch.PlacedGUID
full join tPosition on tPosition.GUID = tsearch.PositionGUID
full join tDepartment on tdepartment.GUID = tsearch.DepartmentGUID
full join tInstantStatus on tInstantStatus.SearchGUID = tSearch.guid
full join tInstantStatusType on tInstantStatusType.GUID = tInstantStatus.InstantStatusGUID 
full join tWorkbench on tWorkbench.SearchGUID=tsearch.GUID
full join tSearchCluendex on tSearchCluendex.CPSGUID=tsearch.GUID 
full join tAttributes AS a1 ON a1.GUID=tSearchCluendex.AttributeGUID and a1.RelatedGUID is not NULL
full join tAttributes AS a2 ON a2.GUID=tSearchCluendex.AttributeGUID and a2.RelatedGUID is NULL
提前谢谢

更新1

基于@Shungo提示

这也不起作用:

select tsearch.Description,

tcompany.CompanyName,
tcompany.GUID as CompanyGuid,
tcompanylocation.LocationName,
tsearchtype.SearchType,
tsearchresult.searchresult,
tpeople.GUID as PersonPlacedGuid,
tpeople.LastName As PersonPlacedLName,
tpeople.Firstname As PersonPlacedFName,
tsearch.SearchNotes,
( select tpeople.Firstname  from tpeople where tpeople.guid=tSearch.RepresentativeGUID) as repfirstname, 
( select tpeople.Lastname from tpeople where tpeople.guid=tSearch.RepresentativeGUID) as replastname,
tsearch.RepresentativeGUID as RepGuid,
tposition.Position as backgroundposition,
tdepartment.Department as backgrounddepartment,
( select tpeople.Lastname from tpeople where tpeople.guid=tSearch.ReferredByGUID) as referredbylastname,
( select tpeople.FirstName from tpeople where tpeople.guid=tSearch.ReferredByGUID) as referredbylastname,
tsearch.ReferredByGUID as PersonwhorefferedGuid,
( select tcompany.CompanyName from tCompany where tCompany.guid=tSearch.PlacedFromGUID) as placedfrom,
tinstantstatustype.InstantStatus,
tWorkbench.WorkbenchName,
( select tpeople.Lastname from tpeople where tpeople.guid=tInstantStatus.PeopleGUID) as Candlastname,
( select tpeople.FirstName from tpeople where tpeople.guid=tInstantStatus.PeopleGUID) as candFirstname,
tInstantStatus.ForClientNotes,
tinstantstatus.InstantStatusNotes as InstanttatusNotesSummary,
( select a1.Attribute from tAttributes where tAttributes.RelatedGUID is not NUll ) as Attributes,
( select a1.Attribute from tAttributes where tAttributes.RelatedGUID is  NUll ) as Categories


from tSearch


full join tCompany on tsearch.CompanyGUID = tcompany.guid
full join tcompanylocation on tcompanylocation.guid= tcompany.LocationGUID
full join tSearchType on tsearchtype.GUID = tSearch.SearchTypeGUID
full join tSearchResult on tSearchResult.GUID = tsearch.SearchResultGUID
full join tPeople on tPeople.GUID = tsearch.PlacedGUID
full join tPosition on tPosition.GUID = tsearch.PositionGUID
full join tDepartment on tdepartment.GUID = tsearch.DepartmentGUID
full join tInstantStatus on tInstantStatus.SearchGUID = tSearch.guid
full join tInstantStatusType on tInstantStatusType.GUID = tInstantStatus.InstantStatusGUID 
full join tWorkbench on tWorkbench.SearchGUID=tsearch.GUID
full join tSearchCluendex on tSearchCluendex.CPSGUID=tsearch.GUID
full join tAttributes on tAttributes.GUID=tSearchCluendex.AttributeGUID
LEFT JOIN tAttributes AS a1 ON a1.GUID=tSearchCluendex.AttributeGUID
返回:

Msg 512, Level 16, State 1, Line 3
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
更新3:

select * from tAttributes where RelatedGUID is null;
列出11个类别,其中RelatedGUID为空的条目

现在,当我按照@iamdave solution的建议尝试时:

( select top 1 tAttributes.Attribute from tAttributes where tAttributes.RelatedGUID is not NUll ) as Attributes,
( select top 1 tAttributes.Attribute from tAttributes where tAttributes.RelatedGUID is  NUll ) as Categories
select tsearch.Description,

tcompany.CompanyName,
tcompany.GUID as CompanyGuid,
tcompanylocation.LocationName,
tsearchtype.SearchType,
tsearchresult.searchresult,
tpeople.GUID as PersonPlacedGuid,
tpeople.LastName As PersonPlacedLName,
tpeople.Firstname As PersonPlacedFName,
tsearch.SearchNotes,
( select tpeople.Firstname  from tpeople where tpeople.guid=tSearch.RepresentativeGUID) as repfirstname, 
( select tpeople.Lastname from tpeople where tpeople.guid=tSearch.RepresentativeGUID) as replastname,
tsearch.RepresentativeGUID as RepGuid,
tposition.Position as backgroundposition,
tdepartment.Department as backgrounddepartment,
( select tpeople.Lastname from tpeople where tpeople.guid=tSearch.ReferredByGUID) as referredbylastname,
( select tpeople.FirstName from tpeople where tpeople.guid=tSearch.ReferredByGUID) as referredbylastname,
tsearch.ReferredByGUID as PersonwhorefferedGuid,
( select tcompany.CompanyName from tCompany where tCompany.guid=tSearch.PlacedFromGUID) as placedfrom,
tinstantstatustype.InstantStatus,
tWorkbench.WorkbenchName,
( select tpeople.Lastname from tpeople where tpeople.guid=tInstantStatus.PeopleGUID) as Candlastname,
( select tpeople.FirstName from tpeople where tpeople.guid=tInstantStatus.PeopleGUID) as candFirstname,
tInstantStatus.ForClientNotes,
tinstantstatus.InstantStatusNotes as InstanttatusNotesSummary,
a1.Attribute as Attributes,
a2.Attribute as Categories


from tSearch


full join tCompany on tsearch.CompanyGUID = tcompany.guid
full join tcompanylocation on tcompanylocation.guid= tcompany.LocationGUID
full join tSearchType on tsearchtype.GUID = tSearch.SearchTypeGUID
full join tSearchResult on tSearchResult.GUID = tsearch.SearchResultGUID
full join tPeople on tPeople.GUID = tsearch.PlacedGUID
full join tPosition on tPosition.GUID = tsearch.PositionGUID
full join tDepartment on tdepartment.GUID = tsearch.DepartmentGUID
full join tInstantStatus on tInstantStatus.SearchGUID = tSearch.guid
full join tInstantStatusType on tInstantStatusType.GUID = tInstantStatus.InstantStatusGUID 
full join tWorkbench on tWorkbench.SearchGUID=tsearch.GUID
full join tSearchCluendex on tSearchCluendex.CPSGUID=tsearch.GUID 
full join tAttributes AS a1 ON a1.GUID=tSearchCluendex.AttributeGUID and a1.RelatedGUID is not NULL
full join tAttributes AS a2 ON a2.GUID=tSearchCluendex.AttributeGUID and a2.RelatedGUID is NULL
它返回如下输出:

i、 e

其中“程序员”应该在“IT”类别中,“秘书”在“商务开发”等中。因此属性是可以的,但是类别不会被查找,尽管连接和条件存在

这个数据库相当大,大约有40个表,所以我很难粘贴SQL结构,但希望一些聪明和优秀的灵魂能够理解这一点。如果没有,请告诉我应该粘贴什么(请指定要在MS SSMS中执行的命令)


谢谢,

CASE
语句应该可以做到这一点。与其查找null或notnull的记录,不如检查每个记录中的值并相应地进行处理。比如:

SELECT
(CASE WHEN tAttributes.RelatedGUID is not null then tAttributes.Attribute ELSE null END) as Attributes,
(CASE WHEN tAttributes.RelatedGUID is null then tAttributes.Attribute ELSE null END) as Categories
FROM tAttributes

在where和using CASE中添加条件。。什么时候然后。。试着按下面的方法做

从表格中选择col1,col2,…colN..,其中当@PARAMETER=0时,clm1=CASE,然后选择col1 ELSE@PARAMETER


一旦将同一列名设置为WHERE条件,则从表中检索所有数据;否则,如果要作为参数传递,则从表中检索基于条件值的数据。

如果期望得到多个结果,则必须为此使用
连接。但你会得到重复的行…重复的行是可以的,只要我有属性和类别列彼此匹配。怎么做?谢谢什么是
t属性.RelatedGUID
?如果这指向结果集的GUID,只需添加一个
左联接
,并在
-clauseOn的
中使用此值。另一个提示:如果在联接中更频繁地使用同一个表,则必须使用名称别名。有些人认为像
左连接属性为a1上的a1。RelatedGUID=Someother.GUID
。在选择列表中,您可以使用
a1.Attribute
获得值。下一次调用“a2”等等…@android\u dev,尝试将您的
(选择a1.Attribute…
替换为
at.Attribute
。不需要在那里进行子选择…谢谢!!!好主意…我正在检查我的数据集,但它似乎正在做它应该做的事情
SELECT
(CASE WHEN tAttributes.RelatedGUID is not null then tAttributes.Attribute ELSE null END) as Attributes,
(CASE WHEN tAttributes.RelatedGUID is null then tAttributes.Attribute ELSE null END) as Categories
FROM tAttributes