SQL Server与列的联合行为

时间:2017-12-11 22:29:36

标签: sql-server

可以从N个表中获得这样的结果(这个例子有3个表)

表1并不总是最多行:

结果

Public description_col As String, unitcost_col As String, total_idiqpricing_col As String, mat_unit_cost_col As String

Public mat_qty_col As String

Public man_hours_col As String, rate_per_hour_col As String, eq_hours_col As String, eq_rate_per_hour_col As String

Public sub_cost_col As String, L_tot_col As String, eq_tot_col As String, mat_tot_col As String, tot_tot_col As String

Public description_col_U As Range ' for description column user selected

Public tot_tot_col_U As Range

Public Descell As Variant, tot_totalcell As Variant

Table1Id  Table2Id  Table3Id
-----------------------------
1         33        Y12
2         43        M34
3         23        R77
4         56        NULL
5         NULL      NULL

1 个答案:

答案 0 :(得分:3)

您可以使用row-number()select t1.id as Table1Id, t2.id as Table2Id, t3.id as Table3Id from (select t1.*, row_number() over (order by id) as seqnum) from t1 ) t1 full join (select t2.*, row_number() over (order by id) as seqnum) from t2 ) t2 full join on t1.seqnum = t2.seqnum full join (select t3.*, row_number() over (order by id) as seqnum) from t3 ) t3 on t3.seqnum = coalesce(t1.seqnum, t2.seqnum);

{{1}}

还有其他方法,但这应该足够了。