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

The Archive Base Latest Questions

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

I have some data in SQL Server as: att1 att2 att3 att4 att5 …

  • 0

I have some data in SQL Server as:

att1 att2 att3 att4 att5   ...   att205  
---------------------------------------
438  498  3625 3645 5000   ...   5000
438  498  3625 3648 5000   ...   5040
438  498  3625 3629 5000   ...   5330
437  501  3625 3626 5000   ...   5040
438  498  3626 3629 5000   ...   5050

I want to know the square root of the sum for each column of data, to do so I do:

CREATE VIEW VIEW_myTable (ID, Value) AS (
            SELECT 1,  SQRT(SUM(att1)) FROM myTABLE
  UNION ALL SELECT 2,  SQRT(SUM(att2)) FROM myTABLE
  UNION ALL SELECT 3,  SQRT(SUM(att3)) FROM myTABLE
  UNION ALL SELECT 4,  SQRT(SUM(att4)) FROM myTABLE
  ...
  UNION ALL SELECT 205,  SQRT(SUM(att205)) FROM myTABLE
 ) FROM INFORMATION_SCHEMA.COLUMNS WHERE  TABLE_NAME = 'myTABLE'

Also would like to ADD a extra result, the number of rows it sumed…
So in the example above there are 5 rows and 205 columns.

How can I take advantage of the scan sql did to the table while suming the elements?

so I do not do something like another SELECT aka.

SELECT COUNT(*) FROM [myTABLE]

In fewer words I want to take advantage of scanning the table in that query…
I was thinking to put Something like SUM(1) somewhere but do not know where….

I used the FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'myTABLE' because I am doing dynamic sql… but the query looks like that…

—————-EDIT———————-

As answered by @Neil Moss:

if I would have lots of columns, more than 1024… SQL will not support this… Is there a way to overcome this situation?

  • 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-24T13:57:24+00:00Added an answer on May 24, 2026 at 1:57 pm

    If you are prepared to accept the query returning a single row, with a SqrtSumAttrX column for each AttrX column, then dynamically constructing the following works just fine:

    select
        sqrt(sum(attr1)) as SqrtSumAttr1,
        sqrt(sum(attr2)) as SqrtSumAttr2,
        sqrt(sum(attr3)) as SqrtSumAttr3,
        ...
        sqrt(sum(attr205)) as SqrtSumAttr205,
        sum(1) as RowsScanned
    from
        MyTable
    

    This has the bonus of only scanning the table once, whereas the sample in your question scans it 205 times – once for each union.

    Input:

    Attr1 Attr2 Attr3
    1     2     3
    4     5     6
    7     8     9
    10    11    12
    

    Output:

    SqrtSumAttr1      SqrtSumAttr2      SqrtSumAttr3      RowsScanned
    4.69041575982343  5.09901951359278  5.47722557505166  4
    

    EDIT post-acceptance

    For dynamic construction, try this:

    declare @columns nvarchar(max)
    declare @sql nvarchar(max)
    set @columns = ''
    select 
        @columns = @columns + 'sqrt(sum([' + [COLUMN_NAME] + '])) as SumSqrt' + [COLUMN_NAME] + ',' 
    from 
        [INFORMATION_SCHEMA].[COLUMNS] 
    where 
        TABLE_NAME = 'MyTable' 
        and 
        DATA_TYPE in ('int', 'decimal', 'float') -- numerics only (add other datatypes as needed)
    order by 
        ORDINAL_POSITION 
    
    set @sql = 'select ' + @columns + 'count(*) as RowsScanned from Results'
    
    exec (@sql)
    

    I’ve used count(*) rather than sum(1) as suggested by Andriy M as this returns 0 if no rows exist, rather than null.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some data with a timestamp in SQL Server, I would like to
I have a Microsoft SQL server DB that imports some data which needs a
I have some questions about importing data from Excel/CSV File into SQL Server. Let
I have some data stored in SQL Server that contains apostrophes. Within the SQL
We have some data in the SQL server table that needs to scramble. Some
I have some utf8 data which I would like to bulk insert (sql server
Let's say I have some data, either in a SQL Server 2008 table or
I have some data like this : (AT sql sERVER) MemberID,ParemtID,Amt,OtherInfo 1, NULL, 200,dfdsf
I have some XML data stored in a varchar(max) column on SQL Server 2005.
I have some troubles trying to move data from SQL Server 2000 (SP4) to

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.