两表数据简单合并的sql语句,怎么写? 有table1Fld1 Fld2D1 100有table2Fld1 Fld2D1 1D1 2D1 3写一个SQ语句L,得到如下数据:D1 1 100D1 2 0D1 3 0 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 select a.Fld1 ,b.Fld2,a.Fld2 from table1 a,table2 b where b.Fld1 = a.Fld1(+) select a.Fld1 ,b.Fld2,nvl(a.Fld2,0)from table1 a,table2 b where b.Fld1 = a.Fld1(+) 有table1Fld1 Fld2D1 100D2 200有table2Fld1 Fld2D1 1D1 2D1 3D2 1D2 2D2 3写一个SQ语句L,得到如下数据:D1 1 100D1 2 0D1 3 0D2 1 200D2 2 0D2 3 0 不对啊,返回的是:D1 1 100D1 2 100D1 3 100 下面是建表和初始记录create table table1 ( FLD1 VARCHAR2(111), FLD2 NUMBER(20,2))create table table2( FLD1 VARCHAR2(111), FLD2 NUMBER(20,2))insert into table1 (FLD1, FLD2) values ('D1', 100);insert into table2 (FLD1, FLD2) values ('D1', 1);insert into table2 (FLD1, FLD2) values ('D1', 2);insert into table2 (FLD1, FLD2) values ('D1', 3); tb2没组都有1 然后显示tb1的fld2? 如果都确定是1的话 可以这么写create table tb1 (Fld1 varchar2(5),Fld2 number(10));insert into tb1 values ('D1',100);insert into tb1 values ('D2',200);create table tb2 (Fld1 varchar2(5),Fld2 number(10));insert into tb2 values ('D1',1);insert into tb2 values ('D1',2);insert into tb2 values ('D1',3);insert into tb2 values ('D2',1);insert into tb2 values ('D2',2);insert into tb2 values ('D2',3);select tb2.fld1,case when tb2.fld2=1 then tb1.fld2 else 0 end fld2from tb1,tb2where tb1.fld1=tb2.fld1 fld1 fld2-------------------------1 D1 1002 D1 03 D1 04 D2 2005 D2 06 D2 0 table2 也不确定,也可以是D1 1.33D1 1.32D1 1.45D2 1.5 那手动排序 是不是fld2最小的赋值为tb1.fld2呢? select tb2.fld1,case when tb2.fld2=1 then tb1.fld2 else 0 end fld2from tb1,tb2where tb1.fld1=tb2.fld1把tb2.fld2值,固定为=1,不对。等于1只是一样演示例子。 create table table1 ( FLD1 VARCHAR2(111), FLD2 NUMBER(20,2))create table table2( FLD1 VARCHAR2(111), FLD2 NUMBER(20,2))insert into table1 (FLD1, FLD2) values ('D1', 100);insert into table2 (FLD1, FLD2) values ('D1', 1);insert into table2 (FLD1, FLD2) values ('D1', 2);insert into table2 (FLD1, FLD2) values ('D1', 3);insert into table1 (FLD1, FLD2) values ('D2', 200);insert into table2 (FLD1, FLD2) values ('D2', 1);insert into table2 (FLD1, FLD2) values ('D2', 2);insert into table2 (FLD1, FLD2) values ('D2', 3);select t1.fld1, t2.fld2, decode(row_number() over(partition by t1.fld1 order by t2.fld2), 1, t1.fld2, 0) from table1 t1, table2 t2 where t1.fld1 = t2.fld1;FLD1 FLD2 DECODE(ROW_NUMBER()OVER(PARTIT-------------------------------------------------------------------------------- ---------------------- ------------------------------D1 1.00 100D1 2.00 0D1 3.00 0D2 1.00 200D2 2.00 0D2 3.00 0 6 rows selected select tb2.fld1,tb2.fld2,case when row_number() over(partition by tb2.fld1 order by tb2.fld2)=1 then tb1.fld2 else 0 end fld2from tb1,tb2where tb1.fld1=tb2.fld1; fld1 fld2 fld2-------------------------1 D1 1.32 1002 D1 1.33 03 D1 1.45 04 D2 1.22 2005 D2 1.33 06 D2 1.55 0 订单主表 table_order_mst 订单编号 订单类型 订单总金额 成本价 数量 D1 折扣订单 10000 0 0 D2 成本订单 20000 1000 10订单主表 table_order_det订单编号 数量 单价 金额 D1 4 1000 4000 D1 6 1000 6000 D2 6 0 0 D2 7 0 0最后得到:订单编号 订单类型 订单数量 订单单价 订单金额 D1 折扣订单 4 1000 4000D1 折扣订单 6 1000 6000D2 成本订单 6 0 20000D2 成本订单 7 0 0订单金额合计是 30000. 各位大佬,都理解简单了,tab1的fld2的值,不一定是1 或许剑走偏锋,走火入魔。 换种简单写法,把折扣类订单写一个视图,成本类订单写一个视图,最后再写一个总金额视图, 然后把三个视图union all一下,再排序就好了。 create table table_order_mst (订单编号 varchar2(20),订单类型 varchar2(20),订单总金额 number(10),成本价 number(10),数量 number(10));insert into table_order_mst values ('D1','折扣订单',10000,0,0);insert into table_order_mst values ('D2','成本订单',20000,1000,10);create table table_order_det (订单编号 varchar2(5),数量 number(10),单价 number(10),金额 number(10));insert into table_order_det values ('D1',4,1000,4000);insert into table_order_det values ('D1',4,1000,6000);insert into table_order_det values ('D2',6,0,0);insert into table_order_det values ('D2',7,0,0); select tb2.订单编号,tb2.数量,tb2.单价, case when row_number() over(partition by tb2.订单编号 order by tb2.订单编号)=1 and tb2.金额=0 then tb1.订单总金额 else tb2.金额 end 金额from table_order_mst tb1,table_order_det tb2where tb1.订单编号=tb2.订单编号; 订单编号 数量 单价 金额---------------------------------------------1 D1 4 1000 40002 D1 6 1000 60003 D2 6 0 200004 D2 7 0 0 select table2.fld1,table2.fld2,decode(table2.fld2,1,table1.fld2,0)from table1,table2 where table1.fld1=table2.fld1; 一个排序的sql oracle 全文索引同步问题 Oracle中关于求索引表中数据最大值的问题~~~ 数据库设计的时候,能否允许存在关系为一对一的表格? oracle 11g 中文输入问题 关于SQL用join的问题,困扰了我很久了,请高手帮助 ORACLE中有“组”(GROUP)的概念吗? DBLink传输同名数据库问题 对oracle基本操作!详情请看..... 【求助】关于一个SQL分组合并取值的语句,如果写?请大牛给出思路 values放字符串 Oracle 10g 安装 “Checking swap space” Failed
Fld1 Fld2
D1 100
D2 200有table2
Fld1 Fld2
D1 1
D1 2
D1 3D2 1
D2 2
D2 3写一个SQ语句L,得到如下数据:
D1 1 100
D1 2 0
D1 3 0D2 1 200
D2 2 0
D2 3 0
不对啊,返回的是:
D1 1 100
D1 2 100
D1 3 100
create table table1 (
FLD1 VARCHAR2(111),
FLD2 NUMBER(20,2)
)
create table table2(
FLD1 VARCHAR2(111),
FLD2 NUMBER(20,2)
)
insert into table1 (FLD1, FLD2) values ('D1', 100);insert into table2 (FLD1, FLD2) values ('D1', 1);
insert into table2 (FLD1, FLD2) values ('D1', 2);
insert into table2 (FLD1, FLD2) values ('D1', 3);
insert into tb1 values ('D1',100);
insert into tb1 values ('D2',200);create table tb2 (Fld1 varchar2(5),Fld2 number(10));
insert into tb2 values ('D1',1);
insert into tb2 values ('D1',2);
insert into tb2 values ('D1',3);
insert into tb2 values ('D2',1);
insert into tb2 values ('D2',2);
insert into tb2 values ('D2',3);select tb2.fld1,case when tb2.fld2=1 then tb1.fld2 else 0 end fld2
from tb1,tb2
where tb1.fld1=tb2.fld1
fld1 fld2
-------------------------
1 D1 100
2 D1 0
3 D1 0
4 D2 200
5 D2 0
6 D2 0
也可以是
D1 1.33
D1 1.32
D1 1.45D2 1.5
from tb1,tb2
where tb1.fld1=tb2.fld1把tb2.fld2值,固定为=1,不对。等于1只是一样演示例子。
FLD1 VARCHAR2(111),
FLD2 NUMBER(20,2)
)
create table table2(
FLD1 VARCHAR2(111),
FLD2 NUMBER(20,2)
)
insert into table1 (FLD1, FLD2) values ('D1', 100);
insert into table2 (FLD1, FLD2) values ('D1', 1);
insert into table2 (FLD1, FLD2) values ('D1', 2);
insert into table2 (FLD1, FLD2) values ('D1', 3);
insert into table1 (FLD1, FLD2) values ('D2', 200);
insert into table2 (FLD1, FLD2) values ('D2', 1);
insert into table2 (FLD1, FLD2) values ('D2', 2);
insert into table2 (FLD1, FLD2) values ('D2', 3);select t1.fld1,
t2.fld2,
decode(row_number() over(partition by t1.fld1 order by t2.fld2),
1,
t1.fld2,
0)
from table1 t1, table2 t2
where t1.fld1 = t2.fld1;FLD1 FLD2 DECODE(ROW_NUMBER()OVER(PARTIT
-------------------------------------------------------------------------------- ---------------------- ------------------------------
D1 1.00 100
D1 2.00 0
D1 3.00 0
D2 1.00 200
D2 2.00 0
D2 3.00 0
6 rows selected
select tb2.fld1,tb2.fld2,case when row_number() over(partition by tb2.fld1 order by tb2.fld2)=1 then tb1.fld2 else 0 end fld2
from tb1,tb2
where tb1.fld1=tb2.fld1;
fld1 fld2 fld2
-------------------------
1 D1 1.32 100
2 D1 1.33 0
3 D1 1.45 0
4 D2 1.22 200
5 D2 1.33 0
6 D2 1.55 0
D1 折扣订单 10000 0 0
D2 成本订单 20000 1000 10订单主表 table_order_det订单编号 数量 单价 金额
D1 4 1000 4000
D1 6 1000 6000 D2 6 0 0
D2 7 0 0最后得到:
订单编号 订单类型 订单数量 订单单价 订单金额
D1 折扣订单 4 1000 4000
D1 折扣订单 6 1000 6000D2 成本订单 6 0 20000
D2 成本订单 7 0 0订单金额合计是 30000.
换种简单写法,把折扣类订单写一个视图,成本类订单写一个视图,最后再写一个总金额视图,
然后把三个视图union all一下,再排序就好了。
create table table_order_mst (订单编号 varchar2(20),订单类型 varchar2(20),订单总金额 number(10),成本价 number(10),数量 number(10));
insert into table_order_mst values ('D1','折扣订单',10000,0,0);
insert into table_order_mst values ('D2','成本订单',20000,1000,10);create table table_order_det (订单编号 varchar2(5),数量 number(10),单价 number(10),金额 number(10));
insert into table_order_det values ('D1',4,1000,4000);
insert into table_order_det values ('D1',4,1000,6000);
insert into table_order_det values ('D2',6,0,0);
insert into table_order_det values ('D2',7,0,0); select tb2.订单编号,tb2.数量,tb2.单价,
case when row_number() over(partition by tb2.订单编号 order by tb2.订单编号)=1 and tb2.金额=0 then tb1.订单总金额 else tb2.金额 end 金额
from table_order_mst tb1,table_order_det tb2
where tb1.订单编号=tb2.订单编号; 订单编号 数量 单价 金额
---------------------------------------------
1 D1 4 1000 4000
2 D1 6 1000 6000
3 D2 6 0 20000
4 D2 7 0 0
decode(table2.fld2,1,table1.fld2,0)
from table1,table2
where table1.fld1=table2.fld1;