2012-01-25 46 views
1

私はselectを選択しています。この列は同じ選択から塗りつぶされなければなりませんが、私はそれを2度選択する必要はありません。それはpossoble 1回選択した後、最初から2番目の列の値を設定した後ですか? 例:選択値から選択した値を選択します。

insert into @temptable from 
select 
a = (select aa from table1 where quantity > 5) 
b = (select aa from table1 where quantity > 5) 

私が必要:

insert into @temptable from 
select 
a = (select aa from table1 where quantity > 5) 
b = {value from a} 

更新。私は悪い例を書いて、私はバランス

INSERT @tmpBalances 
     SELECT PA.ContractId AS 'ContractId', 
      Con.Name AS 'ContractName', 
      Bal.PortfolioAccountId AS 'PortfolioAccountId', 
      PA.Name AS 'PortfolioAccountName', 
      RA.GeneralId AS 'RegisterAccountGeneralId', 
      Bal.BalanceTypeId AS 'BalanceTypeId', 
      Bt.Name AS 'BalanceTypeName', 
      Bt.Type AS 'BalanceTypeType', 
      Bal.BalanceTimeType AS 'BalanceTimeType', 
      Bal.InstrumentId AS 'InstrumentId', 
      Ins.Name AS 'InstrumentName', 
      Ins.GeneralId AS 'InstrumentGeneralId', 
      (Bal.Balance - 
       (  
        SELECT COALESCE(SUM(Mov.Amount), 0) 
         FROM trd.Movements AS Mov 
        WHERE 
         Bal.InstrumentId = Mov.InstrumentId AND 
         Bal.PortfolioAccountId = Mov.PortfolioAccountId AND 
         Bal.BalanceTypeId = Mov.BalanceTypeId AND 
         Bal.BalanceTimeType = Mov.BalanceTimeType AND 
         DateDiff(DAY, @Date, Mov.Date) > 0 AND 
         -- Currency může být null a NULL = NULL nejde 
         COALESCE(Bal.CurrencyId, -1) = COALESCE(Mov.CurrencyId, -1) 
       ) 
      ) as Balance, 
      Balance AS 'BalancePrediction1', 
      Balance AS 'BalancePrediction2', 
      Bal.CurrencyId AS 'CurrencyId', 
      Ccy.Code AS 'CurrencyCode', 
      PA.PositionServiceType 'PositionServiceType', 
      Ccy.Name 'CurrencyName', 
      S.Nominal AS 'Nominal', 
      S.NominalCurrencyId AS 'NominalCurrencyId', 
      trd.GetCurrencyCode(S.NominalCurrencyId) AS 'NominalCurrencyCode' 
      FROM trd.Balances AS Bal 
      JOIN trd.PortfolioAccounts AS PA ON PA.Id = Bal.PortfolioAccountId 
      JOIN trd.Contracts AS Con ON Con.Id = PA.ContractId 
      JOIN trd.RegisterAccounts AS RA ON RA.Id = PA.RegisterAccountId 
      JOIN trd.BalanceTypes AS Bt ON Bt.Id = Bal.BalanceTypeId 
      JOIN trd.Instruments AS Ins ON Ins.Id = Bal.InstrumentId 
      LEFT OUTER JOIN trd.Currencies AS Ccy ON Ccy.Id = Bal.CurrencyId 
      LEFT JOIN trd.SecuritiesView S ON s.Id = Ins.Id AND DateDiff(d, S.ValidFrom, @Date) >= 0 AND (S.ValidTo IS NULL OR DateDiff(d, S.ValidTo, @Date) < 0) 
       AND S.InstrumentInstrumentTypePriceUnit = 1 

答えて

3

あなたは、挿入した後、テーブル変数の更新を行うことができます。

update @tmpBalances 
set BalancePrediction1 = Balance, 
    BalancePrediction2 = Balance 

また、cross applyを使用して合計を計算することもできます。

INSERT @tmpBalances 
     SELECT PA.ContractId AS 'ContractId', 
      Con.Name AS 'ContractName', 
      Bal.PortfolioAccountId AS 'PortfolioAccountId', 
      PA.Name AS 'PortfolioAccountName', 
      RA.GeneralId AS 'RegisterAccountGeneralId', 
      Bal.BalanceTypeId AS 'BalanceTypeId', 
      Bt.Name AS 'BalanceTypeName', 
      Bt.Type AS 'BalanceTypeType', 
      Bal.BalanceTimeType AS 'BalanceTimeType', 
      Bal.InstrumentId AS 'InstrumentId', 
      Ins.Name AS 'InstrumentName', 
      Ins.GeneralId AS 'InstrumentGeneralId', 
      (Bal.Balance - Mov.SumAmount) AS Balance, 
      (Bal.Balance - Mov.SumAmount) AS 'BalancePrediction1', 
      (Bal.Balance - Mov.SumAmount) AS 'BalancePrediction2', 
      Bal.CurrencyId AS 'CurrencyId', 
      Ccy.Code AS 'CurrencyCode', 
      PA.PositionServiceType 'PositionServiceType', 
      Ccy.Name 'CurrencyName', 
      S.Nominal AS 'Nominal', 
      S.NominalCurrencyId AS 'NominalCurrencyId', 
      trd.GetCurrencyCode(S.NominalCurrencyId) AS 'NominalCurrencyCode' 
      FROM trd.Balances AS Bal 
      JOIN trd.PortfolioAccounts AS PA ON PA.Id = Bal.PortfolioAccountId 
      JOIN trd.Contracts AS Con ON Con.Id = PA.ContractId 
      JOIN trd.RegisterAccounts AS RA ON RA.Id = PA.RegisterAccountId 
      JOIN trd.BalanceTypes AS Bt ON Bt.Id = Bal.BalanceTypeId 
      JOIN trd.Instruments AS Ins ON Ins.Id = Bal.InstrumentId 
      LEFT OUTER JOIN trd.Currencies AS Ccy ON Ccy.Id = Bal.CurrencyId 
      LEFT JOIN trd.SecuritiesView S ON s.Id = Ins.Id AND DateDiff(d, S.ValidFrom, @Date) >= 0 AND (S.ValidTo IS NULL OR DateDiff(d, S.ValidTo, @Date) < 0) 
       AND S.InstrumentInstrumentTypePriceUnit = 1 
      CROSS APPLY (SELECT COALESCE(SUM(Mov.Amount), 0) 
         FROM trd.Movements AS Mov 
         WHERE 
          Bal.InstrumentId = Mov.InstrumentId AND 
          Bal.PortfolioAccountId = Mov.PortfolioAccountId AND 
          Bal.BalanceTypeId = Mov.BalanceTypeId AND 
          Bal.BalanceTimeType = Mov.BalanceTimeType AND 
          DateDiff(DAY, @Date, Mov.Date) > 0 AND 
          -- Currency může být null a NULL = NULL nejde 
          COALESCE(Bal.CurrencyId, -1) = COALESCE(Mov.CurrencyId, -1) 
         ) Mov(SumAmount) 
0

一つの方法からBalancePrediction1とBalancePrediction2値に設定する必要があります。

;with T (value) as (
    select aa from table1 where quantity > 5 
) 
insert into @temptable 
    select value, value from T 
1
SELECT aa AS a, aa AS b 
FROM table1 
WHERE quantity > 5 
関連する問題