表结构如下:
name num type
公司1 20 出境
公司1 12 进境
公司2 30 出境
公司2 32 进境
公司3 40 出境
公司3 45 进境
公司4 21 出境
公司4 19 进境怎样让查询出来的结果,变成下面的样子呢
name num type
公司1 20 出境
公司1 20 进境
公司2 30 出境
公司2 30 进境
公司3 40 出境
公司3 40 进境
公司4 21 出境
公司4 21 进境
意思是说让同一公司的进境次数变得跟出境出数一样。
name num type
公司1 20 出境
公司1 12 进境
公司2 30 出境
公司2 32 进境
公司3 40 出境
公司3 45 进境
公司4 21 出境
公司4 19 进境怎样让查询出来的结果,变成下面的样子呢
name num type
公司1 20 出境
公司1 20 进境
公司2 30 出境
公司2 30 进境
公司3 40 出境
公司3 40 进境
公司4 21 出境
公司4 21 进境
意思是说让同一公司的进境次数变得跟出境出数一样。
select name, num, type from tbl where type = '出境'
union all
select name, num, '进境' from tbl where type = '出境'
order by name;
FROM (WITH t1 AS
(SELECT NAME, num, TYPE
FROM table1
WHERE TYPE = '出境')
SELECT NAME, num, TYPE
FROM t1
UNION ALL
SELECT NAME, num, '进境' TYPE
FROM t1)
ORDER BY NAME, TYPE
select name
,decode(type,'出境',num,(select num from t t2 where t2.name =t1.name and t2.type='进境')) type
from t t1
where ...
(select num from t t2 where t2.name =t1.name and t2.type='进境')你括号中的这一块数据都是从t2中查找的,而且条件是t2.name =t1.name and t2.type='进境', 那返回的值肯定是name相同,type='进境'的num值。
(
select '公司1' a,20 b,'出境' c from dual
union all
select '公司1',12,'进境' from dual
union all
select '公司2',30,'出境' from dual
union all
select '公司2',32,'进境' from dual
union all
select '公司3',40,'出境' from dual
union all
select '公司3',45,'进境' from dual
union all
select '公司4',21,'出境' from dual
union all
select '公司4',19,'进境' from dual
)
select a,nvl(d,b),c from
(
select a,b,c,lag(b)over(partition by a order by a) d from t
)--result:
公司1 20 出境
公司1 20 进境
公司2 30 出境
公司2 30 进境
公司3 40 出境
公司3 40 进境
公司4 21 出境
公司4 21 进境