多表查询组合出一张表

wen1512 2010-05-04 04:59:05
--表a
ReportID_u ReportColumnName_ch ItemID_i ColumnFormulaID_ch
1338e817-696c-440c-8702-fa2cf809e7f1 字段1 1 1
1338e817-696c-440c-8702-fa2cf809e7f1 字段2 2 3
1338e817-696c-440c-8702-fa2cf809e7f1 字段3 3 7
1338e817-696c-440c-8702-fa2cf809e7f1 字段4 4 9

--表b
ReportID_u OrgID_ch
1338e817-696c-440c-8702-fa2cf809e7f1 组织1
1338e817-696c-440c-8702-fa2cf809e7f1 组织2
1338e817-696c-440c-8702-fa2cf809e7f1 组织3
1338e817-696c-440c-8702-fa2cf809e7f1 组织4
1f9cd177-ce43-40bc-a378-021e4fde4f3d 组织5

--表c
OrgID_ch ItemID_i ColumnFormulaID_ch ColumnValue_ch
组织1 1 1 10
组织2 1 1 15
组织3 1 1 5
组织4 1 1 1
组织1 2 1 1
组织2 2 1 5
组织3 2 1 5
组织4 2 1 2

--要得到的结果:
--对应每一个ReportID_u,查询出一张表,其中第一列来自于表b,数据来自于表c的ColumnValue_ch值
OrgID_ch 字段1 字段2 字段3 字段4 ——表头
组织1 10 1 3 3
组织2
组织3
组织4
...全文
239 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
xing020010 2010-05-06
  • 打赏
  • 举报
回复
貌似是行列转换类的,只不过数据源是多个表的
htl258_Tony 2010-05-05
  • 打赏
  • 举报
回复
先帮顶。
renadg 2010-05-05
  • 打赏
  • 举报
回复
我也没看懂.
结果是怎么个关系..好乱..
new4everlau 2010-05-04
  • 打赏
  • 举报
回复
我最近就喜欢顶帖子,楼主谅解啊
wen1512 2010-05-04
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 xys_777 的回复:]

没看懂这3个表是个什么关系,数据怎去取
[/Quote]通过每一行的组织和每一列的字段对应的ItemID_i、ColumnFormulaID_ch,对应到表c中找对应的值,
OrgID_ch 字段1 字段2 字段3 字段4 ——表头
组织1 10 1 3 3
组织2
组织3
组织4

“10”的来源:组织1、字段1对应的ItemID_i和ColumnFormulaID_ch(分别为1,1),到表c中去找ColumnValue_ch值,就是10 了
--小F-- 2010-05-04
  • 打赏
  • 举报
回复
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2010-05-04 17:14:34
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:[a]
if object_id('[a]') is not null drop table [a]
go
create table [a]([ReportID_u] uniqueidentifier,[ReportColumnName_ch] varchar(5),[ItemID_i] int,[ColumnFormulaID_ch] int)
insert [a]
select '1338e817-696c-440c-8702-fa2cf809e7f1','字段1',1,1 union all
select '1338e817-696c-440c-8702-fa2cf809e7f1','字段2',2,3 union all
select '1338e817-696c-440c-8702-fa2cf809e7f1','字段3',3,7 union all
select '1338e817-696c-440c-8702-fa2cf809e7f1','字段4',4,9
--> 测试数据:[b]
if object_id('[b]') is not null drop table [b]
go
create table [b]([ReportID_u] uniqueidentifier,[OrgID_ch] varchar(5))
insert [b]
select '1338e817-696c-440c-8702-fa2cf809e7f1','组织1' union all
select '1338e817-696c-440c-8702-fa2cf809e7f1','组织2' union all
select '1338e817-696c-440c-8702-fa2cf809e7f1','组织3' union all
select '1338e817-696c-440c-8702-fa2cf809e7f1','组织4' union all
select '1f9cd177-ce43-40bc-a378-021e4fde4f3d','组织5'
--> 测试数据:[c]
if object_id('[c]') is not null drop table [c]
go
create table [c]([OrgID_ch] varchar(5),[ItemID_i] int,[ColumnFormulaID_ch] int,[ColumnValue_ch] int)
insert [c]
select '组织1',1,1,10 union all
select '组织2',1,1,15 union all
select '组织3',1,1,5 union all
select '组织4',1,1,1 union all
select '组织1',2,1,1 union all
select '组织2',2,1,5 union all
select '组织3',2,1,5 union all
select '组织4',2,1,2
--------------开始查询--------------------------
declare @sql varchar(8000)
set @sql = 'select b.OrgID_ch '
select @sql = @sql + ' , max(case a.ReportColumnName_ch when ''' + ReportColumnName_ch + ''' then c.ColumnValue_ch else 0 end) [' + ReportColumnName_ch + ']'
from (select distinct a.ReportColumnName_ch from a) as a
set @sql = @sql + ' from a,b,c where a.ReportID_u=b.ReportID_u and b.OrgID_ch=c.OrgID_ch group by b.OrgID_ch'
exec(@sql)
----------------结果----------------------------
/*OrgID_ch 字段1 字段2 字段3 字段4
-------- ----------- ----------- ----------- -----------
组织1 10 10 10 10
组织2 15 15 15 15
组织3 5 5 5 5
组织4 2 2 2 2

(4 行受影响)

*/
wen1512 2010-05-04
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 fredrickhu 的回复:]

SQL code
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2010-05-04 17:14:34
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.0……
[/Quote]你理解错了,表a中的“字段1,字段2”不固定,可能“字段1...字段100”,也可能只有“字段1”,你不能把它写死了
--小F-- 2010-05-04
  • 打赏
  • 举报
回复
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2010-05-04 17:14:34
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:[a]
if object_id('[a]') is not null drop table [a]
go
create table [a]([ReportID_u] uniqueidentifier,[ReportColumnName_ch] varchar(5),[ItemID_i] int,[ColumnFormulaID_ch] int)
insert [a]
select '1338e817-696c-440c-8702-fa2cf809e7f1','字段1',1,1 union all
select '1338e817-696c-440c-8702-fa2cf809e7f1','字段2',2,3 union all
select '1338e817-696c-440c-8702-fa2cf809e7f1','字段3',3,7 union all
select '1338e817-696c-440c-8702-fa2cf809e7f1','字段4',4,9
--> 测试数据:[b]
if object_id('[b]') is not null drop table [b]
go
create table [b]([ReportID_u] uniqueidentifier,[OrgID_ch] varchar(5))
insert [b]
select '1338e817-696c-440c-8702-fa2cf809e7f1','组织1' union all
select '1338e817-696c-440c-8702-fa2cf809e7f1','组织2' union all
select '1338e817-696c-440c-8702-fa2cf809e7f1','组织3' union all
select '1338e817-696c-440c-8702-fa2cf809e7f1','组织4' union all
select '1f9cd177-ce43-40bc-a378-021e4fde4f3d','组织5'
--> 测试数据:[c]
if object_id('[c]') is not null drop table [c]
go
create table [c]([OrgID_ch] varchar(5),[ItemID_i] int,[ColumnFormulaID_ch] int,[ColumnValue_ch] int)
insert [c]
select '组织1',1,1,10 union all
select '组织2',1,1,15 union all
select '组织3',1,1,5 union all
select '组织4',1,1,1 union all
select '组织1',2,1,1 union all
select '组织2',2,1,5 union all
select '组织3',2,1,5 union all
select '组织4',2,1,2
--------------开始查询--------------------------
select
b.OrgID_ch,
max(case a.ReportColumnName_ch when '字段1' then c.ColumnValue_ch else 0 end) as 字段1,
max(case a.ReportColumnName_ch when '字段2' then c.ColumnValue_ch else 0 end) as 字段2,
max(case a.ReportColumnName_ch when '字段3' then c.ColumnValue_ch else 0 end) as 字段3,
max(case a.ReportColumnName_ch when '字段4' then c.ColumnValue_ch else 0 end) as 字段4
from
a,b,c
where
a.ReportID_u=b.ReportID_u
and
b.OrgID_ch=c.OrgID_ch
group by
b.OrgID_ch
----------------结果----------------------------
/*OrgID_ch 字段1 字段2 字段3 字段4
-------- ----------- ----------- ----------- -----------
组织1 10 10 10 10
组织2 15 15 15 15
组织3 5 5 5 5
组织4 2 2 2 2

(4 行受影响)

*/
永生天地 2010-05-04
  • 打赏
  • 举报
回复
没看懂这3个表是个什么关系,数据怎去取
abcdef1111111 2010-05-04
  • 打赏
  • 举报
回复
来学习
--小F-- 2010-05-04
  • 打赏
  • 举报
回复
select
b.OrgID_ch,
max(case a.ReportColumnName_ch when '字段1' then c.ColumnValue_ch else 0 end) as 字段1,
max(case a.ReportColumnName_ch when '字段2' then c.ColumnValue_ch else 0 end) as 字段2,
max(case a.ReportColumnName_ch when '字段3' then c.ColumnValue_ch else 0 end) as 字段3
from
a,b,c
where
a.ReportID_u=b.ReportID_u
and
b.OrgID_ch=c.OrgID_ch
group by
b.OrgID_ch
--小F-- 2010-05-04
  • 打赏
  • 举报
回复
select
b.OrgID_ch,
max(case a.ReportColumnName_ch when '字段1' then c.ColumnValue_ch else 0 end) as 字段1,
max(case a.ReportColumnName_ch when '字段1' then c.ColumnValue_ch else 0 end) as 字段2,
max(case a.ReportColumnName_ch when '字段1' then c.ColumnValue_ch else 0 end) as 字段3
from
a,b,c
where
a.ReportID_u=b.ReportID_u
and
b.OrgID_ch=c.OrgID_ch
group by
b.OrgID_ch

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧