首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

这种效果怎么统计?

2012-12-14 
这种效果如何统计??table1字段ID字段2字段3字段41012012-01-012012-01-01ADBDD102...table2自增ID字段ID字

这种效果如何统计??


table1  
字段ID     字段2          字段3         字段4
101        2012-01-01     2012-01-01    ADBDD
102
...
table2
自增ID     字段ID          字段23
1             101          A部门
2             101          B部门
3             101          C部门
4             102          ..
table3
自增ID     字段ID          字段33
1             101          张三
2             101          李四
3             102          ..
table4
自增ID     字段ID          字段43
1             101          C部门
2             101          B部门
3             102          ..
table5
自增ID     字段ID          字段53
1             101          打牌
2             101          麻将
3             102          ..

统计成
字段ID       字段2       字段3          字段4   字段23               字段33      字段43         字段53
101        2012-01-01   2012-01-01    ADBDD   A部门,B部门,C部门   张三,李四    C部门,B部门    打牌,麻将

[最优解释]
select 字段ID,字段2,字段3,字段4,stuff((select ','+字段23 from table2 where table2.字段ID=tb1.字段ID for xml path('')),1,1,'') 字段23 ,stuff((select ','+字段33 from table3 where table3.字段ID=tb1.字段ID for xml path('')),1,1,'') 字段33,stuff((select ','+字段43 from table4 where table4.字段ID=tb1.字段ID for xml path('')),1,1,'') 字段43 ,stuff((select ','+字段53 from table5 where table5.字段ID=tb1.字段ID for xml path('')),1,1,'') 字段53   from table1 tb1


[其他解释]
Select
字段ID,
字段2,
字段3,
字段4
f1(字段ID),
f2(字段ID),
f3(字段ID),
f4(字段ID)
From table1

Create function f1(@id int)
returns varchar(100)
as
Begin

declare @ret varchar(100)

select 
@ret=@ret +字段23+','
from table2
where 字段ID=@id

return @ret
End
[其他解释]
那就根据字段33 为张三,查询id,根据id关联好几个表就行。
[其他解释]
在1L的基础上后面加一句试试
where tb1.字段id=(select 字段id from table3 where 字段33='张三')
[其他解释]

引用:
select 字段ID,字段2,字段3,字段4,stuff((select ','+字段23 from table2 where table2.字段ID=tb1.字段ID for xml path('')),1,1,'') 字段23 ,stuff((select ','+字段33 from table3 where table3.字段ID=tb1.字段ID for xml……

多谢楼上二位   统计结果是出来了
   我现在想 加上查询条件  该怎么写呢? 我在后面直接加where   说没有这列
比如查询table3中的字段33 为张三  的数据  
[其他解释]
引用:
那就根据字段33 为张三,查询id,根据id关联好几个表就行。

 明白你的意思  就是将2楼的 table1 和table3换个位置  后面再加条件
是这意思吧   这样是统计出来了  但是

....
stuff((select ','+字段2, 字段3,字段4 from table5 where table5.字段ID=tb1.字段ID for xml path('')),1,1,'') 字段2,字段3,字段4
...
..

这样写的话  这样写的话 直接报错
  我想将字段2,字段3,字段4  都显示出来 作为三个字段 
这样可以吗?
[其他解释]
引用:
在1L的基础上后面加一句试试
where tb1.字段id=(select 字段id from table3 where 字段33='张三')

刚才试了 直接报错
子查询返回的值不止一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。
[其他解释]
select max(字段id) from table3 where 字段33='张三'
[其他解释]
stuff((select ','+字段4 from table1 where table1.字段ID=tb1.字段ID for xml path('')),1,1,'') 字段4
  
这样只能显示一个字段  改成
stuff((select ','+字段4,字段2,字段3 from table1 where table1.字段ID=tb1.字段ID for xml path('')),1,1,'') 字段4
这样  它就将字段2和3  都显示在字段4里面了 而且 字段2和字段3的字段名称都显示进去了
[其他解释]
引用:
select max(字段id) from table3 where 字段33='张三'

这样不报错了  查询结果不对
表中 字段33  为张三的有好3个 而且字段id不一样
101
102
103
三个 字段id
现在只查询出来第一个
[其他解释]
select 字段ID,字段2,字段3,字段4,stuff((select ','+字段23 from table2 where table2.字段ID=tb1.字段ID for xml path('')),1,1,'') 字段23 ,stuff((select ','+字段33 from table3 where table3.字段ID=tb1.字段ID for xml path('')),1,1,'') 字段33,stuff((select ','+字段43 from table4 where table4.字段ID=tb1.字段ID for xml path('')),1,1,'') 字段43 ,stuff((select ','+字段53 from table5 where table5.字段ID=tb1.字段ID for xml path('')),1,1,'') 字段53   from table1 tb1 where tb1.字段id in (select 字段id from table3 where 字段33='张三' )


[其他解释]

引用:
select 字段ID,字段2,字段3,字段4,stuff((select ','+字段23 from table2 where table2.字段ID=tb1.字段ID for xml path('')),1,1,'') 字段23 ,stuff((select ','+字段33 from table3 where table3.字段ID=tb1.字段ID for xml……

问题已解决了  多谢...

热点排行