我不完全確定所要求的措辭,但我正在嘗試爲報告創建查詢,並且我想要創建的每個列的所有值都在當前的單個列中。這是我目前的查詢:如何加快我從另一列中的值創建新列的查詢?
SELECT [SubjectID]
,(SELECT [DataValue]
FROM [SummarySubjectDataValueAll]
WHERE [LibraryEventID] IN (
SELECT [LibraryEventID]
FROM [LibraryEvent]
WHERE [Name] LIKE '%First Name%')
) as FirstName
,(SELECT [DataValue]
FROM [SummarySubjectDataValueAll]
WHERE [LibraryEventID] IN
(SELECT [LibraryEventID]
FROM [LibraryEvent]
WHERE [Name] LIKE '%Last Name%')) as LastName
,(SELECT [DataValue]
FROM [SummarySubjectDataValueAll]
WHERE [LibraryEventID] IN (
SELECT [LibraryEventID]
FROM [LibraryEvent]
WHERE [Name] LIKE 'Primary Phone')) as Phone
,(SELECT [DataValue]
FROM [SummarySubjectDataValueAll]
WHERE [LibraryEventID] IN (
SELECT [LibraryEventID]
FROM LibraryEvent]
WHERE [Name] = 'Secondary Phone')) as Phone2
,(SELECT [DataValue]
FROM [SummarySubjectDataValueAll]
WHERE [LibraryEventID] IN (
SELECT [LibraryEventID]
FROM [LibraryEvent]
WHERE [Name] = 'Next of Kin Phone')) as KinPhone
FROM [SummarySubjectDataValueAll]
WHERE DataValue = @Phone
(單獨,他們查詢拉什麼我需要和非常快)。試圖拉動信息的時間超過10分鐘或更長時間,我知道這是因爲我再次將它運行在同一張桌子上6次。我不記得如何以更有效的方式做到這一點。有人可以協助嗎?本報告正在做什麼的一些背景:它本質上是一個反向電話目錄。我需要根據他們的電話號碼提取這個人的信息,這個電話號碼可以是上述三種類型中的任何一種(小學,中學或最近的親屬)。
預先感謝您
你能提供執行計劃嗎? –
SQL版本?看起來像一個非常簡單的支點給我。 –
此現有查詢可能不會執行您的想法。它將爲每個字段名稱返回LibraryEvent中的_first_條目,而不是匹配SummarySubjectDataValueAll表格中行內任何內容的條目。 –