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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:04:03+00:00 2026-06-14T10:04:03+00:00

I have a table with an index and I am executing a aggregate SQL

  • 0

I have a table with an index and I am executing a aggregate SQL query using sum
you can see what I am doing here in sqlfiddle.

Create table TX (
  i int NOT NULL PRIMARY KEY,
  x1 DECIMAL(7,3), 
  x2 DECIMAL(7,3), 
  x3 DECIMAL(7,3)
);


INSERT INTO TX (i,x1,x2,x3) values
(1,5, 6,6) ;
INSERT INTO TX (i,x1,x2,x3) values
(2,6, 7, 5);
INSERT INTO TX (i,x1,x2,x3) values
(3,5, 6, 7) ;
INSERT INTO TX (i,x1,x2,x3) values
(4,6, 7, 4);

My question is How can I insert into 3 different tables the results of that query?

SELECT SUM(1),
       SUM(x1),SUM(x2),SUM(x3),
       SUM(x1*x1),
       SUM(x2*x1),SUM(x2*x2),
       SUM(x3*x1),SUM(x3*x2),SUM(x3*x3)

FROM TX

so

how can I get something like

Sum(1)
-----
n

index  Sums
------------
1      4
2      22
3      26

index1  index2   Mult
----------------------
1            1   122
2            1   144
2            2   170
3            1   119
3            2   141
3            3   126

Instead of

    SUM(1) SUM(X1) SUM(X2) SUM(X3) SUM(X1*X1) SUM(X2*X1) SUM(X2*X2) SUM(X3*X1) SUM(X3*X2)  SUM(X3*X3)
_____________________________________________________________________________________________________
        4      22       26     22        122        144       170      119          141          126
  • 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-06-14T10:04:05+00:00Added an answer on June 14, 2026 at 10:04 am
    SELECT SUM(1)
    FROM TX;
    
    SELECT 1, SUM(x1)
    FROM TX
    UNION ALL 
    SELECT 2, SUM(x2)
    FROM TX
    UNION ALL 
    SELECT 3, SUM(x3)
    FROM TX;
    
    SELECT a.x i1, b.x i2, SUM(a.s * b.s)
    FROM
    (
        SELECT i, 1 x, x1 s
        FROM TX
        UNION ALL 
        SELECT i, 2 x, x2 s
        FROM TX
        UNION ALL 
        SELECT i, 3 x, x3 s
        FROM TX
    ) a
    INNER JOIN
    (
        SELECT i, 1 x, x1 s
        FROM TX
        UNION ALL 
        SELECT i, 2 x, x2 s
        FROM TX
        UNION ALL 
        SELECT i, 3 x, x3 s
        FROM TX
    ) b ON a.i = b.i AND a.x >= b.x
    GROUP BY a.x, b.x;
    

    SQL Fiddle using your data – Note that your data’s sums (second query) do not match those in your question. I trust this is a typo.

    Notice I got a bit lazy with the third query. Instead of writing out the expansion I flattened the table first and joined it on itself.

    Also note that in the first query SUM(1) can be replaced with COUNT(*).

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

Sidebar

Related Questions

If I have a table column with data and create an index on this
I'm using SQL Server 2008. I have a table with over 3 million records,
I have a table with 1million record and i designed 2queries. table index: id
I have a table which already contains an index in MySQL. I added some
I have a table users with primary index userId . Currently, each user has
I have a table like Table1: col1 col2 col3 and have an index over
I have a table with 10 million records with a nonclustered index key on
I have a large table with a multi-part index. I need to run several
I have a functional index on a table where the index is the decimal
I have got a simple MySQL table and primary index (id) is not numbered

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.