用户名 动作时间 动作名称
123 2012-07-23 开通服务
123 2012-07-24 停止服务
怎么把数据查出来
用户名 开通时间 停止时间
123 2012-07-23 2012-07-24
11 个解决方案
#1
s
with aa
as
(select 123 user_name, '2012-07-23' action_time, '开通服务' action_name from dual
union
select 123 user_name, '2012-07-24' action_time, '停止服务' action_name from dual
)
select user_name 用户名,
wm_concat(decode(action_name,'开通服务',action_time)) 开通时间,
wm_concat(decode(action_name,'停止服务',action_time)) 停止时间
from aa
group by user_name;
#2
如果一个用户 有多次开通 停止动作,这个sql 语句就会把一个用户完全显示在一条记录里面去了如下图:
#3
如果多次开通,你准备是如何显示,显示最后一个嘛?
#4
就是显示多条记录,每条记录只显示一次完成完整该动作的记录,一次开通-到结束,
#5
你原来的数据和你要显示的结果数据,一起写一下!
#6
没看明白楼主想说的什么的意思
#7
--用户名 动作时间 动作名称
WITH t AS
( SELECT '123' AS cust, DATE'2012-07-23' AS actDate, '开通服务' AS act FROM dual
UNION ALL
SELECT '123', DATE'2012-07-24', '停止服务' FROM dual
union all
SELECT '122', DATE'2012-07-01', '开通服务' FROM dual
union all
SELECT '122', DATE'2012-07-22', '停止服务' FROM dual
)
SELECT * FROM t
pivot(min(actdate) FOR act IN ('开通服务','停止服务'));
CUST '开通服务' '停止服务'
---- ------------------------- -------------------------
122 2012-07-01 00:00:00 2012-07-22 00:00:00
123 2012-07-23 00:00:00 2012-07-24 00:00:00
WITH t AS
( SELECT '123' AS cust, DATE'2012-07-23' AS actDate, '开通服务' AS act FROM dual
UNION ALL
SELECT '123', DATE'2012-07-24', '停止服务' FROM dual
union all
SELECT '122', DATE'2012-07-01', '开通服务' FROM dual
union all
SELECT '122', DATE'2012-07-22', '停止服务' FROM dual
)
SELECT * FROM t
pivot(min(actdate) FOR act IN ('开通服务','停止服务'));
CUST '开通服务' '停止服务'
---- ------------------------- -------------------------
122 2012-07-01 00:00:00 2012-07-22 00:00:00
123 2012-07-23 00:00:00 2012-07-24 00:00:00
#8
数据库版本不支持 pivot 函数 我们使用的是10g
#9
不知道楼主真正的意图是什么,两条一起查出来应该是很简单的问题
#10
with aa
as
(select 123 user_name, '2012-07-23' action_time, '开通服务' action_name from dual
union
select 123 user_name, '2012-07-24' action_time, '停止服务' action_name from dual
union
select 123 user_name, '2012-07-25' action_time, '开通服务' action_name from dual
union
select 123 user_name, '2012-07-26' action_time, '停止服务' action_name from dual
union
select 133 user_name, '2012-07-25' action_time, '开通服务' action_name from dual
union
select 133 user_name, '2012-07-26' action_time, '停止服务' action_name from dual
union
select 133 user_name, '2012-07-27' action_time, '开通服务' action_name from dual
)
select user_name, action_time 开通服务,end_time 开通服务 from (
select user_name,action_time,LEAD(action_time)over(partition by user_name order by action_time) end_time,
row_number()over(partition by user_name order by action_time)
a from aa)
where mod(a,2)=1
as
(select 123 user_name, '2012-07-23' action_time, '开通服务' action_name from dual
union
select 123 user_name, '2012-07-24' action_time, '停止服务' action_name from dual
union
select 123 user_name, '2012-07-25' action_time, '开通服务' action_name from dual
union
select 123 user_name, '2012-07-26' action_time, '停止服务' action_name from dual
union
select 133 user_name, '2012-07-25' action_time, '开通服务' action_name from dual
union
select 133 user_name, '2012-07-26' action_time, '停止服务' action_name from dual
union
select 133 user_name, '2012-07-27' action_time, '开通服务' action_name from dual
)
select user_name, action_time 开通服务,end_time 开通服务 from (
select user_name,action_time,LEAD(action_time)over(partition by user_name order by action_time) end_time,
row_number()over(partition by user_name order by action_time)
a from aa)
where mod(a,2)=1
#11
select a.userName "用户名", a.beginTime "开通时间",b.endTime "结束时间" from A a,A b
where a.username=b.username
and a.action!=b.action
and a.action='开通服务'
where a.username=b.username
and a.action!=b.action
and a.action='开通服务'
#1
s
with aa
as
(select 123 user_name, '2012-07-23' action_time, '开通服务' action_name from dual
union
select 123 user_name, '2012-07-24' action_time, '停止服务' action_name from dual
)
select user_name 用户名,
wm_concat(decode(action_name,'开通服务',action_time)) 开通时间,
wm_concat(decode(action_name,'停止服务',action_time)) 停止时间
from aa
group by user_name;
#2
如果一个用户 有多次开通 停止动作,这个sql 语句就会把一个用户完全显示在一条记录里面去了如下图:
#3
如果多次开通,你准备是如何显示,显示最后一个嘛?
#4
就是显示多条记录,每条记录只显示一次完成完整该动作的记录,一次开通-到结束,
#5
你原来的数据和你要显示的结果数据,一起写一下!
#6
没看明白楼主想说的什么的意思
#7
--用户名 动作时间 动作名称
WITH t AS
( SELECT '123' AS cust, DATE'2012-07-23' AS actDate, '开通服务' AS act FROM dual
UNION ALL
SELECT '123', DATE'2012-07-24', '停止服务' FROM dual
union all
SELECT '122', DATE'2012-07-01', '开通服务' FROM dual
union all
SELECT '122', DATE'2012-07-22', '停止服务' FROM dual
)
SELECT * FROM t
pivot(min(actdate) FOR act IN ('开通服务','停止服务'));
CUST '开通服务' '停止服务'
---- ------------------------- -------------------------
122 2012-07-01 00:00:00 2012-07-22 00:00:00
123 2012-07-23 00:00:00 2012-07-24 00:00:00
WITH t AS
( SELECT '123' AS cust, DATE'2012-07-23' AS actDate, '开通服务' AS act FROM dual
UNION ALL
SELECT '123', DATE'2012-07-24', '停止服务' FROM dual
union all
SELECT '122', DATE'2012-07-01', '开通服务' FROM dual
union all
SELECT '122', DATE'2012-07-22', '停止服务' FROM dual
)
SELECT * FROM t
pivot(min(actdate) FOR act IN ('开通服务','停止服务'));
CUST '开通服务' '停止服务'
---- ------------------------- -------------------------
122 2012-07-01 00:00:00 2012-07-22 00:00:00
123 2012-07-23 00:00:00 2012-07-24 00:00:00
#8
数据库版本不支持 pivot 函数 我们使用的是10g
#9
不知道楼主真正的意图是什么,两条一起查出来应该是很简单的问题
#10
with aa
as
(select 123 user_name, '2012-07-23' action_time, '开通服务' action_name from dual
union
select 123 user_name, '2012-07-24' action_time, '停止服务' action_name from dual
union
select 123 user_name, '2012-07-25' action_time, '开通服务' action_name from dual
union
select 123 user_name, '2012-07-26' action_time, '停止服务' action_name from dual
union
select 133 user_name, '2012-07-25' action_time, '开通服务' action_name from dual
union
select 133 user_name, '2012-07-26' action_time, '停止服务' action_name from dual
union
select 133 user_name, '2012-07-27' action_time, '开通服务' action_name from dual
)
select user_name, action_time 开通服务,end_time 开通服务 from (
select user_name,action_time,LEAD(action_time)over(partition by user_name order by action_time) end_time,
row_number()over(partition by user_name order by action_time)
a from aa)
where mod(a,2)=1
as
(select 123 user_name, '2012-07-23' action_time, '开通服务' action_name from dual
union
select 123 user_name, '2012-07-24' action_time, '停止服务' action_name from dual
union
select 123 user_name, '2012-07-25' action_time, '开通服务' action_name from dual
union
select 123 user_name, '2012-07-26' action_time, '停止服务' action_name from dual
union
select 133 user_name, '2012-07-25' action_time, '开通服务' action_name from dual
union
select 133 user_name, '2012-07-26' action_time, '停止服务' action_name from dual
union
select 133 user_name, '2012-07-27' action_time, '开通服务' action_name from dual
)
select user_name, action_time 开通服务,end_time 开通服务 from (
select user_name,action_time,LEAD(action_time)over(partition by user_name order by action_time) end_time,
row_number()over(partition by user_name order by action_time)
a from aa)
where mod(a,2)=1
#11
select a.userName "用户名", a.beginTime "开通时间",b.endTime "结束时间" from A a,A b
where a.username=b.username
and a.action!=b.action
and a.action='开通服务'
where a.username=b.username
and a.action!=b.action
and a.action='开通服务'