Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Or...</p> <pre><code>select Date , t1.Price as Stock_1_Price , t2.Price as Stock_3_price from ( select "Date" , max(Price) as Price from myData where ID = 1 group by "Date" ) t1 full join ( select "Date" , max(Price) as Price from myData where ID = 3 group by "Date" ) t2 on t2.Date = t1.Date </code></pre> <p>As far as populating a temp table, any of the usual ways works:</p> <ul> <li><p>Table variable:</p> <pre><code>declare @work table ( yyyymmdd varchar(32) not null , stock_1_price money null , stock_3_price money null ) insert @work ( yyyymmdd , stock_1_price , stock_3_price ) select Date , t1.Price as Stock_1_Price , t2.Price as Stock_3_price from ( select "Date" , max(Price) as Price from myData where ID = 1 group by "Date" ) t1 full join ( select "Date" , max(Price) as Price from myData where ID = 3 group by "Date" ) t2 on t2.Date = t1.Date </code></pre></li> <li><p>Declared temp table in tempdb</p> <pre><code>create table #work ( yyyymmdd varchar(32) not null primary key clustered , stock_1_price money null , stock_3_price money null ) insert #work ( yyyymmdd , stock_1_price , stock_3_price ) select Date , t1.Price as Stock_1_Price , t2.Price as Stock_3_price from ( select "Date" , max(Price) as Price from myData where ID = 1 group by "Date" ) t1 full join ( select "Date" , max(Price) as Price from myData where ID = 3 group by "Date" ) t2 on t2.Date = t1.Date </code></pre></li> <li><p>non-declare temp table in tempdb via <code>select into</code>:</p> <pre><code>select Date , t1.Price as Stock_1_Price , t2.Price as Stock_3_price into #work from ( select "Date" , max(Price) as Price from myData where ID = 1 group by "Date" ) t1 full join ( select "Date" , max(Price) as Price from myData where ID = 3 group by "Date" ) t2 on t2.Date = t1.Date </code></pre></li> </ul>
    singulars
    1. This table or related slice is empty.
    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.
    1. This table or related slice is empty.
    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