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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:02:17+00:00 2026-05-23T14:02:17+00:00

I have two relations(tables in SQL Database) having purely numbers. These two relations are

  • 0

I have two relations(tables in SQL Database) having purely numbers. These two relations are to be imagined as Matrices.

The Question is to write an SQL query to multiply there two relations as we do with normal matrix multiplication.

I have been racking my brains for this…. But to no use :O

Can anyone please help me out?????

Table number 1 :
enter image description here

@ Aaron Bertrand :

I am using SQL Server 2008 R2
The Screenshots of the tables are :

Table 1 :

enter image description here

Table 2 :

enter image description here

I used the Query :

select t1.A,t2.B,SUM(t1.C*t2.C)
from Table_1 as t1 join Table_2 as t2 on t1.B=t2.A 
group by t1.A,t2.B   
order by t1.A

The Answer :

enter image description here

This way is quite Easy rather than my original Matrix way as suggested by @Marcelo Cantos and @ypercube…

  • 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-23T14:02:17+00:00Added an answer on May 23, 2026 at 2:02 pm

    Assuming the following structure:

    A (row INT, col INT, value FLOAT)
    B (row INT, col INT, value FLOAT)
    

    You can write this:

    SELECT A.row, B.col, SUM(A.value * B.value)
      FROM A JOIN B ON A.col = B.row
     GROUP BY A.row, B.col
    

    EDIT: Using the tabular layout of the table to mimic a matrix structure makes this problem much more difficult, for at least two reasons:

    1. You can’t deal with rows and columns in a symmetrical fashion.
    2. You can’t portably refer to the row an element comes from.

    The best work-around would be to add a row number column to each table thus…

    table1 (row, A, B, C)
    table2 (row, A, B, C)
    

    …then synthesise the structure I suggest above as a view on each table…

    CREATE VIEW A
        SELECT row, 1, A FROM table1 UNION
        SELECT row, 2, B FROM table1 UNION
        SELECT row, 3, C FROM table1 UNION
    
    -- Ditto VIEW B
    

    …and finally use the SQL above to evaluate the answer. If necessary, you could write another view that converts the result back into a tabular structure.

    Frankly, however, I consider all this to be very misguided. You will find things work much better with a row/col/value structure (and more logically coherent) than an A/B/C/… structure. Consider, for instance, how easily you can compute the transpose of a matrix using row/col/value:

    SELECT col AS row, row AS col, value
      FROM A
    

    or sum the diagonal:

    SELECT SUM(value)
      FROM A
     WHERE row = col
    

    and ask yourself how easy these would be using your structure.

    P.S.: On the subject of logical coherence, your tables are not relations in the proper sense of the word, since they define an order on the rows, and must necessarily allow for duplicate rows, neither of which make sense in a relation, which is (loosely) defined as a set of tuples. This conflation of n-dimensional relations with 2-dimensional tables is at the heart of much that is ill with data management these days.

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

Sidebar

Related Questions

I have a SQL database with two tables like this: Users Id (PK) Name
I have a database that contains a two tables, where these table have a
We have two tables Family and Member, the relation between these two is Family
I have a question about how to set up the relations between two models
I have a SQL Server database with two table : Users and Achievements. My
How do you do SQL query for the following condition? Suppose you have two
I have Two Many-To-Many tables which I need to write a Entity Framework Linq
I have two tables: Category (Id, Name, ...) and Question (Id, CategoryId, Title, ...)
Hypothetically, in a SQL Server database, if I have a table with two int
I have a simple database with two tables: Photo and Tag . There is

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.