2016-10-12 39 views
2

有關這個問題表 「tAttributes」SQL Server - 子選擇返回多於1行。如何正確選擇所有?

CREATE TABLE [dbo].[tAttributes](
    [GUID] [uniqueidentifier] ROWGUIDCOL NOT NULL, 
    [Attribute] [varchar](50) NOT NULL, 
    [RelatedGUID] [uniqueidentifier] NULL, 
    [LevelLow] [decimal](12, 2) NULL, 
    [LevelHigh] [decimal](12, 2) NULL, 
    [LevelStep] [decimal](12, 2) NULL, 
    [AttributeLevel] [decimal](12, 2) NULL, 
    [Description] [varchar](100) NULL, 
    [AutoselectGUID] [uniqueidentifier] NULL, 
    [CompanySystem] [bit] NOT NULL, 
    [PeopleSystem] [bit] NOT NULL, 
    [SearchSystem] [bit] NOT NULL, 
    [Populate] [bit] NOT NULL, 
    [Synonyms] [varchar](255) NULL, 
    [TimeEntered] [datetime] NULL, 
    [UserEnteredGUID] [uniqueidentifier] NULL, 
    [TimeUpdated] [datetime] NULL, 
    [UserUpdatedGUID] [uniqueidentifier] NULL, 
    [UpdateHistory] [varchar](255) NULL, 
    [Stamp] [timestamp] NULL, 
CONSTRAINT [PK_tAttributes] PRIMARY KEY CLUSTERED 
(
    [GUID] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY], 
CONSTRAINT [IX_AttributeRelatedGUID] UNIQUE NONCLUSTERED 
(
    [Attribute] ASC, 
    [RelatedGUID] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY] 
) ON [PRIMARY] 

它例如屬性:

GUID|Attribute|RelatedGUID|.... 
1 | Val1 | 2   | 
3 | Val2 | NULL  | 
4 | val3 | 5   | 

VAL1是一個屬性,VAL2其中RelatedGUID是NULL是一個類別

直播例如:

F0F9EA32-C3AC-48A9-B6BE-09807B720818| gaming | 09E898CC-5DE2-4664-B9B2-14F17FBC37DB 
32B74398-83C9-4225-81E2-0A1CB6C67954 | Function skills | NULL 

「賭博」是一個n屬性

「功能的技巧」是一個類別

我如何再選擇它的話,該列的屬性我將只有屬性,這些屬性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 

在這裏,我用它「頂1」的把戲,但問題是,有超過1個類別,因此,「頂部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 

在此先感謝!

更新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. 

更新2:

看起來更好,可惜屬性和類別具有相同的價值。我希望屬性具有相關GUID不爲NULL的值和相關GUID具有值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, 
tAttributes.Attribute as Attributes, 
a1.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 on tAttributes.GUID=tSearchCluendex.AttributeGUID 
LEFT JOIN tAttributes AS a1 ON a1.GUID=tSearchCluendex.AttributeGUID 

更新3:

select * from tAttributes where RelatedGUID is null; 

名單11個類別,已RelatedGUID爲NULL條目。

現在,當我嘗試的建議通過@iamdave解決方案:

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 

它返回象下面這樣的輸出:

我。Ë

... Attribute | Category 

... NULL  | NULL 
... Programmer | NULL 
... Secretary | NULL 
... Manager | NULL 

其中,「程序員」應該是「IT」的範疇,「祕書」,在「商務開發」等。所以屬性都OK,但分類不擡頭,儘管JOIN和條件在那裏....

這個數據庫很大,大約有40個表格,所以我很難粘貼SQL結構,但希望一些聰明和善良的靈魂能夠明白這一點。如果不是請讓我知道我應該怎麼粘貼(請註明在MS SSMS執行命令)

感謝,

+1

如果您希望獲得多個結果,則必須使用「JOIN」。但你會得到重複的行... – Shnugo

+0

重複行是好的,只要我有相互匹配的屬性和類別列。怎麼做?謝謝! –

+0

@android_dev - 你需要關聯你的'子查詢' –

回答

1

CASE語句應該做的伎倆。不是查找空或非空的記錄,而是檢查每個記錄中的值並相應地執行。例如:

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 
+0

謝謝!好主意....我正在檢查我的數據集,但它似乎在做它應該做的事情 –