Passa il valore per la tabella derivata
-
12-12-2019 - |
Domanda
C'è un modo per passare un valore a una query da tavola derivata?
Nella tabella derivata che voglio riferimento un valore ([docSVsys].[sID])
dalla query esterna.
ottengo un errore:
.MSG 4104, Livello 16, stato 1, linea 7 L'identificatore multipart "Docsvsys.sid" non potrebbe essere vincolato.
Sì, so che questa query può essere semplificata a nessun ciclo.
Avere un cursore che deve loop e cercare di convertirlo così impostato..select top 10 [docSVsys].[sID], [sI].[count] from docSVsys join ( select count(*) as [count] from docSVenum1 as [sIt] where [sIt].[sID] = [docSVsys].[sID] ) as [sI] on '1' = '1' order by [docSVsys].[sID]
Cross Apply sembrava fare il trucco. Ed è esattamente 1/3 più veloce della versione del cursore. La query reale usando la croce si applica qui sotto.
.SELECT [sO].[sID], [sI].[max], [sI].[avg], [sI].[stdev] FROM docSVsys as [sO] with (nolock) cross apply ( select [sO].[sID], max(list.match) as 'max', avg(list.match) as 'avg', stdev(list.match) as 'stdev' from ( select #SampleSet.[sID], [match] = 200 * count(*) / CAST ( #SampleSetSummary.[count] + [sO].[textUniqueWordCount] as numeric(8,0) ) from #SampleSet with (nolock) join FTSindexWordOnce as [match] with (nolock) -- this is current @sID on match.wordID = #SampleSet.wordID and [match].[sID] = [sO].[sID] join #SampleSetSummary with (nolock) -- to get the word count from the sample set on #SampleSetSummary.[sID] = #SampleSet.[sID] group by #SampleSet.[sID], #SampleSetSummary.[count] ) as list having max(list.match) > 60 ) as [sI] where [textUniqueWordCount] is not null and [textUniqueWordCount] > 4 and [sO].[sID] <= 10686 order by [sO].[sID]
Soluzione
Puoi fare ciò che vuoi con una croce applicabile piuttosto che un join:
select top 10 [docSVsys].[sID], [sI].[count]
from docSVsys
cross apply
(
select count(*) as [count]
from docSVenum1 as [sIt]
where [sIt].[sID] = [docSVsys].[sID]
) as [sI]
order by [docSVsys].[sID]
. Altri suggerimenti
Aggiungi l'ID alla tabella derivata e partecipare a questo:
select top 10 [docSVsys].[sID], [sI].[count]
from docSVsys
join
(
select [sIt].[sID], count(*) as [count]
from docSVenum1 as [sIt]
group by [sIt].[sID]
) as [sI]
on [sI].[sID] = [docSVsys].[sID]
order by [docSVsys].[sID]
.