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 6331613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:06:10+00:00 2026-05-24T18:06:10+00:00

I have created a temporary table with dynamic columns. These columns are the values

  • 0

I have created a temporary table with dynamic columns. These columns are the values in a master table.

Say,

Id |Name
1  |Prepaid
2  |Postpaid

so the temporary table columns will be

Id Prepaid Postpaid

In this case the columns are dynamic.

consider the below code for temp table:

declare @temp as table (Id int, Prepaid float, postpaid float)

insert into @temp values(1,100,200)
insert into @temp values(1,10,500)
insert into @temp values(1,-100,-100)
insert into @temp values(1,10,200)
insert into @temp values(1,100,-100)
insert into @temp values(1,150,560)
insert into @temp values(1,90,200)

I need to insert a row with average of each column.

Average should be calculated taking the rows which meet the condition, i.e, the column should not contain a value -100.

I need to insert a row with average values like for the above values.

Id       PrepaidAvg      PostPaidAvg  
1     76.6666666666667  320
  • 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-24T18:06:12+00:00Added an answer on May 24, 2026 at 6:06 pm

    Hard to determine exactly what result you expect, but here is one try:

    SELECT 
      Id, 
      PrepaidAvg = AVG(CASE WHEN Prepaid > 0 THEN Prepaid ELSE NULL END),
      PostpaidAvg = AVG(CASE WHEN postpaid > 0 THEN postpaid ELSE NULL END)
    FROM @temp
    GROUP BY Id;
    

    If this is off you can get more elaborate:

    ;WITH prepaid AS
    (
      SELECT Id, PrepaidAvg = AVG(Prepaid)
        FROM @temp WHERE Prepaid > 0
    ), postpaid AS
    (
      SELECT Id, PostpaidAvg = AVG(postpaid)
        FROM @temp WHERE postpaid > 0
    )
    SELECT Id = COALESCE(prep.Id, postp.Id),
      prep.PrepaidAvg,
      postp.PostpaidAvg
    FROM prepaid AS prep
    FULL OUTER JOIN postpaid AS postp
    ON prep.Id = postp.Id;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a temporary table where I have duplicated rows with new id's
I have created a Temporal Table: create table #T_Table(id int,name nvarchar(80),value float); I am
I have created a sql function in SQLServer 2008 that declared a temporary table
I have created a foreign key (in SQL Server) by: alter table company add
I have created an sql table where I have indexed files on disk. There
I have a table witch is in intense workload lest say T1 (about 100
I have a table that is created and then deleted at the end of
I'm using SQL Server 2005. I have a temporary sorted table ( Table_A )
I have 2 tables, temporary and permanent table. My objective is to copy temporary
I have a Stored Procedure in MSSQL 2008, inside of this i've created a

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.