我想将表中的多个数据显示为一个单个单元格。请找到我的表格的脚本。
DECLARE @Tab TABLE(code VARCHAR(10), name varchar(20), val1 INT)
INSERT INTO @Tab VALUES ('A', 'Test', 34)
INSERT INTO @Tab VALUES ('B', 'Test', 6)
DECLARE @Tab1 TABLE(code VARCHAR(10), name varchar(20), val2 INT)
INSERT INTO @Tab1 VALUES ('A','Test', 178)
DECLARE @Tab2 TABLE(code VARCHAR(10), name varchar(20), Total INT)
INSERT INTO @Tab2 VALUES ('A','Test', 180)
INSERT INTO @Tab2 VALUES ('B', 'Test', 10)
DECLARE @Tab3 TABLE(code VARCHAR(10), name varchar(20), val1 INT)
INSERT INTO @Tab3 VALUES ('A', 'Test1', 56)
DECLARE @Tab4 TABLE(code VARCHAR(10), name varchar(20), val2 INT)
INSERT INTO @Tab4 VALUES ('A','Test1', 87)
DECLARE @Tab5 TABLE(code VARCHAR(10), name varchar(20), Total INT)
INSERT INTO @Tab5 VALUES ('A','Test1', 93)
我想以下面的格式显示单元格单元格中的数据: -
由于
答案 0 :(得分:1)
希望这会对你有所帮助。
With T (code, [name], val1, val2, total)
AS
(
Select code, [name], val1, 0, 0 from @Tab
Union
Select code, [name], 0, val2, 0 from @Tab1
Union
Select code, [name], 0, 0, Total from @Tab2
Union
Select code, [name], val1, 0, 0 from @Tab3
Union
Select code, [name], 0, val2, 0 from @Tab4
Union
Select code, [name], 0, 0, Total from @Tab5
),
T2 (code, [name], total, val1, val2)
AS
(
Select code, [name], total = sum(total), val1 = sum(val1), val2 = sum(val2)
from T group by code, [name]
)
Select code,
IsNull(Min( case [name] when 'Test' then CONCAT('Total ', total, '(val1:', val1, ',val2:', val2,')') end ), 'Total 0(val1:0,val2:0)') Test,
IsNull(Min( case [name] when 'Test1' then CONCAT('Total ', total, '(val1:', val1, ',val2:', val2,')') end ), 'Total 0(val1:0,val2:0)') Test1
from T2
Group By code