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

  • Home
  • SEARCH
  • 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 4019844
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:09:17+00:00 2026-05-20T10:09:17+00:00

I want to pivot and join to select from 3 tables Table 1: INT,VARCHAR,FLOAT

  • 0

I want to pivot and join to select from 3 tables

Table 1: INT,VARCHAR,FLOAT

ID Name  value
---------------------------
1   a1  32116580
2   a2  50785384
3   a3  54327508
4   a4  61030844

Table 2: INT, VARCHAR, FLOAT

ID   Name     value
---------------------------
1   x11   61326085092
2   x12   80368184260
3   x13   83023398776
4   x14   91144307692
5   x22   95486535484
6   x23   90357090612
7   x24   100588807668
8   x33   707811916752
9   x34   93128452928
10  x44   84566653668

Table 3: INT, VARCHAR, FLOAT

ID   Name     value
---------------------------
1   q1   61326085092
2   q2   95486535484
3   q3   707811916752
4   q4   84566653668

output table:

column1              column2            column3             column4
--------------------------------------------------------------------------
  a1*a1/(q1+q1+x11)  a1*a2/(q1+q2+x12)    a1*a3/(q1+q3+x13)     a1*a4/(q1+q4+x14)
  null               a2*a2/(q2+q2+x22)    a2*a3/(q2+q3+x23)     a2*a4/(q2+q4+x24)
  null               null                 a3*a3/(q3+q3+x339     a3*a4/(q3+q4+x34)
  null               null                 null                  a4*a4/(q4+q4+x44)

(I’m putting the ‘Name’ of the column of the 3 different tables instead of numbers)

  • How to do this?
  • I guess I have to do two pivots? and
    unpivot?…

Well do not how to complete it..

SELECT *
  FROM (
        SELECT
         t1.a1,
         t1.a2,
         t2.x,
         t3.q
         FROM table1 t1
         INNER JOIN table2 t2
         ON t1.id = t2.id
         ...
   ) Output
   PIVOT (
      name IN (
            ...
      PIVOT(name ... )
      )
  ) PivotTable

UPDATE
Previously I have *‘s I have changed it to division and sum, the *’s were just an example,

  • 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-20T10:09:18+00:00Added an answer on May 20, 2026 at 10:09 am

    Sample tables

    create table Table1(ID int, Name varchar(10), value float)
    insert table1 select
    1 ,'a1', 32116580 union all select
    2 ,'a2', 50785384 union all select
    3 ,'a3', 54327508 union all select
    4 ,'a4', 61030844
    
    create table Table2(ID int, Name varchar(10), value float)
    insert Table2 select
    1 ,'x11', 61326085092 union all select
    2 ,'x12', 80368184260 union all select
    3 ,'x13', 83023398776 union all select
    4 ,'x14', 91144307692 union all select
    5 ,'x22', 95486535484 union all select
    6 ,'x23', 90357090612 union all select
    7 ,'x24', 100588807668 union all select
    8 ,'x33', 707811916752 union all select
    9 ,'x34', 93128452928 union all select
    10 ,'x44', 84566653668
    
    create table Table3(ID int, Name varchar(10), value float)
    insert Table3 select
    1 ,'q1', 61326085092 union all select
    2 ,'q2', 95486535484 union all select
    3 ,'q3', 707811916752 union all select
    4 ,'q4', 84566653668
    

    The query you need, for N = 4. For any other N, just use dynamic SQL to build the query, changing the 2 lines required as indicated by **.

    ;with coords(i,row,col,total,N) as (
    select 1,1,1,N.N*(N.N+1)/2, N.N
    from (select count(*) N from table1) N
    union all
    select i+1,
           case when col+1>N then row+1 else row end,
           case when col+1>N then row+1 else col+1 end,
           total, N
    from coords
    where i<total
    )
    select [1],[2],[3],[4] -- **, e.g. ,[5],[6],etc
    from
    (
        select
            c.row,
            c.col,
            cellvalue= ar.value*ac.value/(qr.value+qc.value+x.value)
        from coords c
        inner join table1 ar on ar.id = c.row
        inner join table1 ac on ac.id = c.col
        inner join table3 qr on qr.id = c.row
        inner join table3 qc on qc.ID = c.col
        inner join table2 x on x.ID = c.i
    ) p
    pivot (max(cellvalue) for col in ([1],[2],[3],[4])) pv   -- **
    order by row
    

    Output:

    1                      2                      3                      4
    ---------------------- ---------------------- ---------------------- ----------------------
    5606.50338459295       6876.83326310711       2047.51559459649       8269.17991568225
    NULL                   9003.55641750708       3087.36780924588       11044.2303130135
    NULL                   NULL                   1389.95405212248       3744.35614651666
    NULL                   NULL                   NULL                   14681.7678040306
    

    The dynamic version

    declare @Sql nvarchar(max)
    select @Sql = ISNULL(@sql + ',', '') + QUOTENAME(RIGHT(number,10))
    from master..spt_values
    where type='P' and number between 1 and (select COUNT(*) From table1)
    set @Sql = '
    ;with coords(i,row,col,total,N) as (
    select 1,1,1,N.N*(N.N+1)/2, N.N
    from (select count(*) N from table1) N
    union all
    select i+1,
           case when col+1>N then row+1 else row end,
           case when col+1>N then row+1 else col+1 end,
           total, N
    from coords
    where i<total
    )
    select ' + @sql + '
    from
    (
        select
            c.row,
            c.col,
            cellvalue= ar.value*ac.value/(qr.value+qc.value+x.value)
        from coords c
        inner join table1 ar on ar.id = c.row
        inner join table1 ac on ac.id = c.col
        inner join table3 qr on qr.id = c.row
        inner join table3 qc on qc.ID = c.col
        inner join table2 x on x.ID = c.i
    ) p
    pivot (max(cellvalue) for col in (' + @sql + ')) pv
    order by row
    option (maxrecursion 0)  -- ! use with caution
    '
    exec(@sql)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Could you help me to check as below: DECLARE @tblCity TABLE (CityID INT, CityName
I have 3 tables in my MySQL database : the Threads table (id, title)
I have two tables; Leads, Territories and Referrers. Lead has columns: ID, Name, TerritoryId
I want to design a table for items. There are many types of items,
i have some mysql script for pivot table and then counting some data inside
I want to create a control which takes a while to create (Pivot) and
I have a question about how to pivot/total (for want of a better word)
I have generated a table using PIVOT and the ouput of columns are dynamic.
Want to know what the stackoverflow community feels about the various free and non-free
Want my FireFox at work to be in sync with my FireFox at my

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.