Note that there are some explanatory texts on larger screens.

plurals
  1. POIn temporary tables/variables: join multiple rows to table with only 1 row
    primarykey
    data
    text
    <p><strong>Hi,</strong> I have a small problem of joining a temporary variable with a temporary table. Any input would be much appreciated. I present the problem in the order in which I have tried to solve it.</p> <p>First of, I have a temporary variable which is created from a select statement. The variable @enhet has 2 rows/observations (in my test file, later it will be many more):</p> <pre><code>declare @enhet varchar(50) SELECT @enhet = A.[EnhetsId] FROM [StatistikinlamningDataSKL].[dbo].[StatusHistorik] A inner join ( select [EnhetsId], max(SenastUppdaterad) as SenastDatum from [StatistikinlamningDataSKL].[dbo].[StatusHistorik] group by [EnhetsId] ) B on A.[EnhetsId] = B.[EnhetsId] and A.[SenastUppdaterad] = B.SenastDatum WHERE [NyStatus] = 4 </code></pre> <p>Secondly, I want to create a temporary table that combine these two observations (1 variable) with extra variables that are the same for both unique observations of variable @enhet. The aim is to have a table with unique values only for the variable @enhet but the rest is the same. </p> <pre><code>declare @temp2 table ( EnhetsId varchar(50), TjanstId Int, Tabell varchar(50), Kommentar ntext, Uppdaterad datetime ) insert into @temp2 ( EnhetsId, TjanstId, Tabell, Kommentar, Uppdaterad) values ( @enhet, 1, 'GR_PS09_1', 'KLAR', getdate()) select * from @temp2 </code></pre> <p>The problem is that when I run the script the output is only 1 row with just the last observation from the @enhet-variable. </p> <p>Is there anyone who knows what to do? I have tried many things but since I am new to SQL I have failed to write the correct scripts. Can anyone point me in the right direction?</p> <p>Thanks in advance and best regards! :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload