高难度数据查询

So_CooL 2009-09-22 02:36:27
ordNo node isAction isActionDate subnode userName
2000 1 1 2009-09-21 0 tanxiaofen
2000 2 null null 0 yhqing
2000 2 null null 0 yangming
2000 3 null null 0 thomas
2000 4 null null 0 kp

1230 1 null null 0 tanxiaofen
1230 2 null null 0 yhqing
1230 2 null null 0 yangming
1230 3 null null 0 thomas
1230 4 null null 0 kp

如果isAction 和 isActionDate不为空,则查询出来比它的node大1的值的userName的值做为一个新的字段newuserName的值
如果isAction 和 isActionDate为空,则把node的最小值的userName的值作为newuserName的值
即我输入一个用户名可以得到他的当前下一个节点的用户
结果如下:

ordNo node isAction isActionDate subnode userName nextUserName
2000 1 1 2009-09-21 0 tanxiaofen yhqing
1230 1 null null 0 tanxiaofen tanxiaofen
...全文
150 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
yangdingyu8686 2009-09-22
  • 打赏
  • 举报
回复
SQL CODE

create view v_tb as
select *,case when isAction is not null and isActionDate is not null then
(select top 1 userName from tb b where a.ordNo=b.ordNo and a.node<>b.node order by node )
else
(select top 1 username from tb c where a.ordno =c.ordno order by node)

end nextUserName ,
case when isAction is not null and isActionDate is not null
then '正在处理'
else '待处理'
end status
from tb a

select * from v_tb a
where not exists (select 1 from v_tb b where a.ordno=b.ordno and a.node>b.node )

搞定
楼主的数据像是电信数据,不知道对不对
luoyoumou1202 2009-09-22
  • 打赏
  • 举报
回复
--楼主:要按ordNo分组,对吧?
luoyoumou1202 2009-09-22
  • 打赏
  • 举报
回复
DROP TABLE test2;

CREATE TABLE test2(
ordNo INT,
node INT,
isAction INT,
isActionDate DATETIME,
subnode INT,
userName VARCHAR(20)
);

INSERT INTO test2(ordNo, node, isAction, isActionDate, subnode, userName)
SELECT 2000,1,1,'2009-09-21',0,'luoyoumou' UNION ALL
SELECT 2000,2,NULL,NULL,0,'yhqing' UNION ALL
SELECT 2000,2,NULL,NULL,0,'yangming' UNION ALL
SELECT 2000,3,NULL,NULL,0,'thomas' UNION ALL
SELECT 2000,4,NULL,NULL,0,'kp' UNION ALL
SELECT 1230,1,NULL,NULL,0,'tanxiaofen' UNION ALL
SELECT 1230,2,NULL,NULL,0,'yhqing' UNION ALL
SELECT 1230,2,NULL,NULL,0,'yangming' UNION ALL
SELECT 1230,3,NULL,NULL,0,'thomas' UNION ALL
SELECT 1230,4,NULL,NULL,0,'kp';

--楼主:你要的查询语句如下:(去掉你不需要的字段)
SELECT t1.ordNo, t1.node, t1.isAction, t1.isActionDate,
t1.subnode, t1.userName, t2.userName AS newuserName
FROM test2 t1 JOIN test2 t2
ON t1.ordNo=t2.ordNo AND t1.node=t2.node-1
WHERE (t1.isAction IS NOT NULL AND t1.isActionDate IS NOT NULL)
UNION ALL
SELECT t1.ordNo, t1.node, t1.isAction, t1.isActionDate,
t1.subnode, t1.userName, t2.userName AS newuserName
FROM test2 t1 JOIN test2 t2
ON t1.ordNo=t2.ordNo AND t2.node=(SELECT MIN(node) FROM test2 t3 WHERE t3.ordNo=t2.ordNo)
WHERE (t1.isAction IS NULL AND t1.isActionDate IS NULL)
luoyoumou1202 2009-09-22
  • 打赏
  • 举报
回复
CREATE TABLE test2(
ordNo INT,
node INT,
isAction INT,
isActionDate DATETIME,
subnode INT,
userName VARCHAR(20)
);

INSERT INTO test2(ordNo, node, isAction, isActionDate, subnode, userName)
SELECT 2000,1,1,'2009-09-21',0,'luoyoumou' UNION ALL
SELECT 2000,2,NULL,NULL,0,'yhqing' UNION ALL
SELECT 2000,2,NULL,NULL,0,'yangming' UNION ALL
SELECT 2000,3,NULL,NULL,0,'thomas' UNION ALL
SELECT 2000,4,NULL,NULL,0,'kp' UNION ALL
SELECT 1230,1,NULL,NULL,0,'tanxiaofen' UNION ALL
SELECT 1230,2,NULL,NULL,0,'yhqing' UNION ALL
SELECT 1230,2,NULL,NULL,0,'yangming' UNION ALL
SELECT 1230,3,NULL,NULL,0,'thomas' UNION ALL
SELECT 1230,4,NULL,NULL,0,'kp';

--楼主:你要的查询语句如下:
SELECT t1.ordNo t1_ordNo, t2.ordNo t2_ordNo,
t1.node t1_node, t2.node t2_node,
t1.isAction, t1.isActionDate, t1.subnode, t1.userName,
t2.userName AS newuserName
FROM test2 t1 JOIN test2 t2
ON t1.ordNo=t2.ordNo AND t1.node=t2.node-1
WHERE (t1.isAction IS NOT NULL AND t1.isActionDate IS NOT NULL)
UNION ALL
SELECT t1.ordNo t1_ordNo, t2.ordNo t2_ordNo,
t1.node t1_node, t2.node t2_node,
t1.isAction, t1.isActionDate, t1.subnode, t1.userName,
t2.userName AS newuserName
FROM test2 t1 JOIN test2 t2
ON t1.ordNo=t2.ordNo AND t2.node=(SELECT MIN(node) FROM test2 t3 WHERE t3.ordNo=t2.ordNo)
WHERE (t1.isAction IS NULL AND t1.isActionDate IS NULL)
dawugui 2009-09-22
  • 打赏
  • 举报
回复
/*
标题:按某字段合并字符串之一(简单合并)
作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开)
时间:2008-11-06
地点:广东深圳

描述:将如下形式的数据按id字段合并value字段。
id value
----- ------
1 aa
1 bb
2 aaa
2 bbb
2 ccc
需要得到结果:
id value
------ -----------
1 aa,bb
2 aaa,bbb,ccc
即:group by id, 求 value 的和(字符串相加)
*/
--1、sql2000中只能用自定义的函数解决
create table tb(id int, value varchar(10))
insert into tb values(1, 'aa')
insert into tb values(1, 'bb')
insert into tb values(2, 'aaa')
insert into tb values(2, 'bbb')
insert into tb values(2, 'ccc')
go

create function dbo.f_str(@id int) returns varchar(100)
as
begin
declare @str varchar(1000)
set @str = ''
select @str = @str + ',' + cast(value as varchar) from tb where id = @id
set @str = right(@str , len(@str) - 1)
return @str
end
go

--调用函数
select id , value = dbo.f_str(id) from tb group by id

drop function dbo.f_str
drop table tb


--2、sql2005中的方法
create table tb(id int, value varchar(10))
insert into tb values(1, 'aa')
insert into tb values(1, 'bb')
insert into tb values(2, 'aaa')
insert into tb values(2, 'bbb')
insert into tb values(2, 'ccc')
go

select id, [value] = stuff((select ',' + [value] from tb t where id = tb.id for xml path('')) , 1 , 1 , '')
from tb
group by id

drop table tb


--3、使用游标合并数据
create table tb(id int, value varchar(10))
insert into tb values(1, 'aa')
insert into tb values(1, 'bb')
insert into tb values(2, 'aaa')
insert into tb values(2, 'bbb')
insert into tb values(2, 'ccc')
go
declare @t table(id int,value varchar(100))--定义结果集表变量
--定义游标并进行合并处理
declare my_cursor cursor local for
select id , value from tb
declare @id_old int , @id int , @value varchar(10) , @s varchar(100)
open my_cursor
fetch my_cursor into @id , @value
select @id_old = @id , @s=''
while @@FETCH_STATUS = 0
begin
if @id = @id_old
select @s = @s + ',' + cast(@value as varchar)
else
begin
insert @t values(@id_old , stuff(@s,1,1,''))
select @s = ',' + cast(@value as varchar) , @id_old = @id
end
fetch my_cursor into @id , @value
END
insert @t values(@id_old , stuff(@s,1,1,''))
close my_cursor
deallocate my_cursor

select * from @t
drop table tb
So_CooL 2009-09-22
  • 打赏
  • 举报
回复
再帮我改下那个查询出来的结果:
ordNo node isAction isActionDate subnode userName nextUserName status
2000 1 1 2009-09-21 0 tanxiaofen yhqing,yangming 正在处理
1230 1 null null 0 tanxiaofen tanxiaofen 待处理
因为以下3条记录第一条已处理 第2和第3是同一个级别的,所以要把他们的nextUser用逗号连接起来
那个状态是根据isAction、isActionDate 来做判断的如果都不为空 则显示为已完成,如果都为空则表示待处理,如果有一条记录则表示处理中
2000 1 1 2009-09-21 0 tanxiaofen
2000 2 null null 0 yhqing
2000 2 null null 0 yangming
dawugui 2009-09-22
  • 打赏
  • 举报
回复
create TABLE tb([ordNo] INT, [node] INT, [isAction] INT, [isActionDate] DATETIME, [subnode] INT, [userName] VARCHAR(10))
INSERT tb
SELECT 2000, 1, 1, '2009-09-21', 0, 'tanxiaofen' UNION ALL
SELECT 2000, 2, null, null, 0, 'yhqing' UNION ALL
SELECT 2000, 2, null, null, 0, 'yangming' UNION ALL
SELECT 2000, 3, null, null, 0, 'thomas' UNION ALL
SELECT 2000, 4, null, null, 0, 'kp' UNION ALL
SELECT 1230, 1, null, null, 0, 'tanxiaofen' UNION ALL
SELECT 1230, 2, null, null, 0, 'yhqing' UNION ALL
SELECT 1230, 2, null, null, 0, 'yangming' UNION ALL
SELECT 1230, 3, null, null, 0, 'thomas' UNION ALL
SELECT 1230, 4, null, null, 0, 'kp'

select * ,
case when isAction is not null and isActionDate is not null then
(select top 1 username from tb where ordno = t.ordno and node = t.node + 1)
else
(select top 1 username from tb where ordno = t.ordno order by node)
end newuserName
from tb t

drop table tb

/*
ordNo node isAction isActionDate subnode userName newuserName
----------- ----------- ----------- ------------------------------------------------------ ----------- ---------- -----------
2000 1 1 2009-09-21 00:00:00.000 0 tanxiaofen yhqing
2000 2 NULL NULL 0 yhqing tanxiaofen
2000 2 NULL NULL 0 yangming tanxiaofen
2000 3 NULL NULL 0 thomas tanxiaofen
2000 4 NULL NULL 0 kp tanxiaofen
1230 1 NULL NULL 0 tanxiaofen tanxiaofen
1230 2 NULL NULL 0 yhqing tanxiaofen
1230 2 NULL NULL 0 yangming tanxiaofen
1230 3 NULL NULL 0 thomas tanxiaofen
1230 4 NULL NULL 0 kp tanxiaofen

(所影响的行数为 10 行)

*/
soft_wsx 2009-09-22
  • 打赏
  • 举报
回复
佩服!
SQL77 2009-09-22
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 csdyyr 的回复:]
引用 5 楼 so_cool 的回复:
结果好象是对的,但是我看不懂是什么意思

我也是猜的,不知能不能满足要求,呵呵
[/Quote]
答题基本靠猜,晕
lihan6415151528 2009-09-22
  • 打赏
  • 举报
回复
数据还得需要我们猜
华夏小卒 2009-09-22
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 csdyyr 的回复:]
引用 5 楼 so_cool 的回复:
结果好象是对的,但是我看不懂是什么意思

我也是猜的,不知能不能满足要求,呵呵
[/Quote]
csdyyr 2009-09-22
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 so_cool 的回复:]
结果好象是对的,但是我看不懂是什么意思
[/Quote]
我也是猜的,不知能不能满足要求,呵呵
So_CooL 2009-09-22
  • 打赏
  • 举报
回复
结果好象是对的,但是我看不懂是什么意思
So_CooL 2009-09-22
  • 打赏
  • 举报
回复
2楼真厉害
chuifengde 2009-09-22
  • 打赏
  • 举报
回复
举例说明不全
csdyyr 2009-09-22
  • 打赏
  • 举报
回复
DECLARE @TB TABLE([ordNo] INT, [node] INT, [isAction] INT, [isActionDate] DATETIME, [subnode] INT, [userName] VARCHAR(10))
INSERT @TB
SELECT 2000, 1, 1, '2009-09-21', 0, 'tanxiaofen' UNION ALL
SELECT 2000, 2, null, null, 0, 'yhqing' UNION ALL
SELECT 2000, 2, null, null, 0, 'yangming' UNION ALL
SELECT 2000, 3, null, null, 0, 'thomas' UNION ALL
SELECT 2000, 4, null, null, 0, 'kp' UNION ALL
SELECT 1230, 1, null, null, 0, 'tanxiaofen' UNION ALL
SELECT 1230, 2, null, null, 0, 'yhqing' UNION ALL
SELECT 1230, 2, null, null, 0, 'yangming' UNION ALL
SELECT 1230, 3, null, null, 0, 'thomas' UNION ALL
SELECT 1230, 4, null, null, 0, 'kp'

SELECT *,(SELECT TOP 1 userName FROM @TB WHERE ordNo=T.ordNo AND (isAction IS NULL OR (isAction IS NOT NULL AND node>1)) ORDER BY node)
FROM @TB AS T
WHERE node=1
/*
ordNo node isAction isActionDate subnode userName
----------- ----------- ----------- ------------------------------------------------------ ----------- ---------- ----------
2000 1 1 2009-09-21 00:00:00.000 0 tanxiaofen yangming
1230 1 NULL NULL 0 tanxiaofen tanxiaofen
*/
billpu 2009-09-22
  • 打赏
  • 举报
回复
帮顶下 慢慢看

34,590

社区成员

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

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