请保住我工作!

qinggeshou 2012-11-02 12:42:35
sql问题,不写出来饭碗不保,不是我懒,实在是下午必须出来压力太大,望救命!!
问题描述:
table
name dep
A dep1
A dep2
B dep1
C dep2
D dep1
B dep3
需要的效果:
dep name1 name2 name3
dep1 A B D
dep2 A C
dep3 C
dep有上千个
请救命,大恩没齿难忘!
...全文
195 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
qinggeshou 2012-11-02
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
select dep,(case when name='A' then name else '' end),
...
from table
group by dep
[/Quote]
大哥,不能用这样的写法,因为name有几千个,是动态的,而且随时变化的
xuam 2012-11-02
  • 打赏
  • 举报
回复
select dep,(case when name='A' then name else '' end),
...
from table
group by dep
帅惊联合国 2012-11-02
  • 打赏
  • 举报
回复
有心无力
友情帮顶
qinggeshou 2012-11-02
  • 打赏
  • 举报
回复
dep是动态生成的,不能固定的写法
tangxinzitxz 2012-11-02
  • 打赏
  • 举报
回复
王思明 2012-11-02
  • 打赏
  • 举报
回复
以前写过一个员工工效报表,里面的存储过程和这个类似,当时写的时候只有跑楼上问刘*,没有网络的,写了一天
  • 打赏
  • 举报
回复

----------------------------------------------------------------
-- Author :TravyLee
-- Date :2012-11-02 13:02:44
-- Version:
-- Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86)
-- Jul 9 2008 14:43:34
-- Copyright (c) 1988-2008 Microsoft Corporation
-- Developer Edition on Windows NT 6.1 <X86> (Build 7600: )
--
----------------------------------------------------------------
--> 测试数据:[test]
if object_id('[test]') is not null drop table [test]
go
create table [test]([name] varchar(1),[dep] varchar(4))
insert [test]
select 'A','dep1' union all
select 'A','dep2' union all
select 'B','dep1' union all
select 'C','dep2' union all
select 'D','dep1' union all
select 'B','dep3'
go

--2000数据库处理方法:

alter table test
add id int identity(1,1)
go

select
px=(select count(1) from test b where a.dep=b.dep and a.id>=b.id),
[name],
[dep]
into
#tb
from
test a

declare @str varchar(max)
set @str=''
select
@str=@str+',[name'+LTRIM(px)+']=max(case when px='+LTRIM(px)+' then [name] else '+''''''+' end)'
from
#tb
group by
px
exec('select [dep]'+@str+' from #tb group by [dep]')

/*
dep name1 name2 name3
dep1 A B D
dep2 A C
dep3 B
*/


--try
qinggeshou 2012-11-02
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 的回复:]
SQL code



;with c1(staff_user_id, client_id) as
(

select 27, 257 union all
select 135, 257 union all
select 111, 211
),
c2(staff_user_id, staffname) as ---c1,c2 虚视图用来准备数据
(
sele……
[/Quote]

照写了,一直说Server: Msg 170, Level 15, State 1, Line 4
Line 4: Incorrect syntax near 'XML'.
kensouterry1 2012-11-02
  • 打赏
  • 举报
回复


;with c1(staff_user_id, client_id) as
(

select 27, 257 union all
select 135, 257 union all
select 111, 211
),
c2(staff_user_id, staffname) as ---c1,c2 虚视图用来准备数据
(
select 27, '张三' union all
select 111, '李四' union all
select 135, '王二'
)
SELECT d.client_id,
STUFF((SELECT ','+LTRIM(c2.staffname)
FROM c1
JOIN c2 ON c1.staff_user_id = c2.staff_user_id
where client_id = d.client_id FOR XML PATH('')),1,1,'') NAME
FROM c1 d
GROUP BY d.client_id

client_id NAME
----------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
211 李四
257 张三,王二

(2 行受影响)



先这样吧,拆分成列的问题一会儿再来考虑……

qinggeshou 2012-11-02
  • 打赏
  • 举报
回复
可能是我太笨了吧,始终没成功,请各位大侠帮下我,我把原任务写出来吧:
有3个表,一个表A有2个字段client_id和staff_user_id
是这样的关系
A
staff_user_id client_id
27 257
135 257
111 211
.
.
.
.
.
.
表B
staff_user_id staffname
27 张三
111 李四
135 王二





现在要查询出这个结果:
client_id staff1 staff2 staff3
257 张三 王二
211 李四
请就写sql就行,因为我真的刚学不久
!帮忙啊!
我腫了 2012-11-02
  • 打赏
  • 举报
回复
USE test
GO


--if object_id('table') is not null
-- drop table [table]
--Go
----test data
--Create table [table]([name] nvarchar(1),[dep] nvarchar(4))
--Insert into [table]
--Select N'A',N'dep1'
--Union all Select N'A',N'dep2'
--Union all Select N'B',N'dep1'
--Union all Select N'C',N'dep2'
--Union all Select N'D',N'dep1'
--Union all Select N'B',N'dep3'
--Union all Select N'C',N'dep3'
--Union all Select N'D',N'dep3'
--Union all Select N'E',N'dep3'
--Union all Select N'F',N'dep3'
--Union all Select N'G',N'dep3'
--Union all Select N'H',N'dep3'
--Union all Select N'A',N'dep4'


IF object_id('tempdb..#')IS NOT NULL
DROP TABLE #
Go
SELECT N'name'+LTRIM((SELECT COUNT(1) FROM [table] WHERE dep=t.dep AND name<=t.name)) AS col,name,dep INTO # from [table] AS t



DECLARE @Sql NVARCHAR(MAX)

SELECT @Sql=ISNULL(@Sql+',','')+N'MAX(CASE WHEN col=N'''+col+N''' THEN name ELSE '''' END) As '+col FROM #

GROUP BY col

EXEC ( N'SELECT dep,'+@Sql+N' FROM # GROUP BY dep' )

/*
dep name1 name2 name3
---- ----- ----- -----
dep1 A B D
dep2 A C
dep3 B
*/

Go
xuam 2012-11-02
  • 打赏
  • 举报
回复
declare @sql varchar(8000)
set @sql = 'select dep '
select @sql = @sql + ' , max(case name when ''' + name + ''' then name else '' end) ' from (select distinct name from tb) as a
set @sql = @sql + ' from tb group by dep '
exec(@sql)
xuam 2012-11-02
  • 打赏
  • 举报
回复
declare @sql varchar(8000)
set @sql = 'select dep '
select @sql = @sql + ' , max(case name when ''' + name + ''' then name else '' end) from (select distinct name from tb) as a
set @sql = @sql + ' from tb group by dep '
exec(@sql)
xuam 2012-11-02
  • 打赏
  • 举报
回复
declare @sql varchar(8000)
set @sql = 'select dep '
select @sql = @sql + ' , max(case name when ''' + name + ''' then name else '' end) from (select distinct name from tb) as a
set @sql = @sql + ' from tb group by dep
exec(@sql)
qinggeshou 2012-11-02
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]
引用 5 楼 的回复:
SQL code

USE test
GO


--if object_id('table') is not null
-- drop table [table]
--Go
----test data
--Create table [table]([name] nvarchar(1),[dep] nvarchar(4))
--Insert into ……
[/Quote]
我是sqlserver 2000顺便说下
qinggeshou 2012-11-02
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]
SQL code

USE test
GO


--if object_id('table') is not null
-- drop table [table]
--Go
----test data
--Create table [table]([name] nvarchar(1),[dep] nvarchar(4))
--Insert into [table]
……
[/Quote]


Server: Msg 195, Level 15, State 10, Line 1
'ROW_NUMBER' is not a recognized function name.
Server: Msg 170, Level 15, State 1, Line 4
Line 4: Incorrect syntax near 'MAX'.
Server: Msg 137, Level 15, State 1, Line 6
Must declare the variable '@Sql'.
Server: Msg 137, Level 15, State 1, Line 10
Must declare the variable '@Sql'.
  • 打赏
  • 举报
回复

----------------------------------------------------------------
-- Author :TravyLee
-- Date :2012-11-02 13:02:44
-- Version:
-- Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86)
-- Jul 9 2008 14:43:34
-- Copyright (c) 1988-2008 Microsoft Corporation
-- Developer Edition on Windows NT 6.1 <X86> (Build 7600: )
--
----------------------------------------------------------------
--> 测试数据:[test]
if object_id('[test]') is not null drop table [test]
go
create table [test]([name] varchar(1),[dep] varchar(4))
insert [test]
select 'A','dep1' union all
select 'A','dep2' union all
select 'B','dep1' union all
select 'C','dep2' union all
select 'D','dep1' union all
select 'B','dep3'
go


select
px=ROW_NUMBER()over(partition by [dep] order by name),
[name],
[dep]
into
#tb
from
test
--drop table #tb
declare @str varchar(max)
set @str=''
select
@str=@str+',[name'+LTRIM(px)+']=max(case when px='+LTRIM(px)+' then [name] else '+''''''+' end)'
from
#tb
group by
px
exec('select [dep]'+@str+' from #tb group by [dep]')

----------------结果----------------------------
/*
dep name1 name2 name3
---- ----- ----- -----
dep1 A B D
dep2 A C
dep3 B

(3 行受影响)


*/


--更正一下
  • 打赏
  • 举报
回复

----------------------------------------------------------------
-- Author :TravyLee
-- Date :2012-11-02 13:02:44
-- Version:
-- Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86)
-- Jul 9 2008 14:43:34
-- Copyright (c) 1988-2008 Microsoft Corporation
-- Developer Edition on Windows NT 6.1 <X86> (Build 7600: )
--
----------------------------------------------------------------
--> 测试数据:[test]
if object_id('[test]') is not null drop table [test]
go
create table [test]([name] varchar(1),[dep] varchar(4))
insert [test]
select 'A','dep1' union all
select 'A','dep2' union all
select 'B','dep1' union all
select 'C','dep2' union all
select 'D','dep1' union all
select 'B','dep3'
go


select
px=ROW_NUMBER()over(partition by [dep] order by (select 1)),
[name],
[dep]
into
#tb
from
test

declare @str varchar(max)
set @str=''
select
@str=@str+',[name'+LTRIM(px)+']=max(case when px='+LTRIM(px)+' then [name] else '+''''''+' end)'
from
#tb
group by
px
exec('select [dep]'+@str+' from #tb group by [dep]')

----------------结果----------------------------
/*
dep name1 name2 name3
dep1 A B D
dep2 C A
dep3 B
*/

我腫了 2012-11-02
  • 打赏
  • 举报
回复
USE test
GO


--if object_id('table') is not null
-- drop table [table]
--Go
----test data
--Create table [table]([name] nvarchar(1),[dep] nvarchar(4))
--Insert into [table]
--Select N'A',N'dep1'
--Union all Select N'A',N'dep2'
--Union all Select N'B',N'dep1'
--Union all Select N'C',N'dep2'
--Union all Select N'D',N'dep1'
--Union all Select N'B',N'dep3'


IF object_id('tempdb..#')IS NOT NULL
DROP TABLE #
Go
SELECT N'name'+LTRIM(ROW_NUMBER()OVER(PARTITION BY dep ORDER BY name)) AS col,name,dep INTO # from [table]



DECLARE @Sql NVARCHAR(MAX)

SELECT @Sql=ISNULL(@Sql+',','')+N'MAX(CASE WHEN col=N'''+col+N''' THEN name ELSE '''' END) As '+col FROM #

GROUP BY col

EXEC ( N'SELECT dep,'+@Sql+N' FROM # GROUP BY dep' )

/*
dep name1 name2 name3
---- ----- ----- -----
dep1 A B D
dep2 A C
dep3 B
*/

Go

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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