Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7162165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:41:24+00:00 2026-05-28T13:41:24+00:00

I have select, iside select have 2 column. This column must be filled from

  • 0

I have select, iside select have 2 column. This column must be filled from same select, but I don’t want use select twice for it. Is it possoble use select 1 time and after that set second column value from first?
Example:

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

I need:

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

Update. I wrote bad example, I need set to BalancePrediction1 and BalancePrediction2 value from Balance

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T13:41:25+00:00Added an answer on May 28, 2026 at 1:41 pm

    You could do an update to the table variable after the insert.

    update @tmpBalances
    set BalancePrediction1 = Balance,
        BalancePrediction2 = Balance
    

    Or you can use cross apply to calculate the sum.

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this postgres sql query: select * from stats where athlete_id = 5
Let's say I have SELECT name FROM table which gives me something like foo
I have a table. In this table have select element. How can I find
Right now, I have SELECT gp_id FROM gp.keywords WHERE keyword_id = 15 AND (SELECT
I have a select statement with calculated columns and I would like to use
I have a select list which is being populated using the values from a
Lets say I have this SQL statements: ALTER TABLE dbo.[tbl] ALTER COLUMN col1 varchar(300)
I have a table that I wish to select a subset of columns from
I have a column in a MySQL table called year. I want to check
Hi i have the following query/table from a local bookstore $queryadmin =SELECT last_name, first_name,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.