当前位置: 代码迷 >> Oracle管理 >> 主子表查询解决方案
  详细解决方案

主子表查询解决方案

热度:126   发布时间:2016-04-24 04:16:36.0
主子表查询
TMSR_MEASUREDOCINFO 是计量主表,主键为c_measuredocid
tmsr_measuredata 是计量子表,外键为c_measuredocid
一条主表的计量数据对应2条子表计量数据(毛重计量和皮重计量)
主表保存净重计量结果


select  t.c_measuredocid,t.c_carrytoolmainid,t.i_msrfinishflag,d.n_tareweight/1000,d.c_taredatetime,d.n_grossweight/1000,d.c_grossdatetime,t.n_suttleweight/1000,t.c_suttleweightdate from 
TMSR_MEASUREDOCINFO t,tmsr_measuredata d
where d.c_measuredocid = t.c_measuredocid
and t.c_measuredocid = 'Q140801133358015'


查询结果为
Q140801133358015   豫EL5998   1    0                                                       67.28   2014-08-01 14:49:16    48.52   2014-08-01 14:49:16
Q140801133358015   豫EL5998   1    18.76    2014-08-01 13:33:58         0                                                48.52   2014-08-01 14:49:16

这车的皮重为18.76吨,时间为 2014-08-01 13:33:58。毛重为67.28 ,时间为 2014-08-01 14:49:16 ,净重48.52,时间和毛重时间相同

我想展示为下面的一条结果

 Q140801133358015      豫EL5998   1   18.76    2014-08-01 13:33:58   67.28   2014-08-01 14:49:16    48.52   2014-08-01 14:49:16

请问应该怎么做?不能用净重减毛重得皮重,要用语句实现。
------解决方案--------------------
引用:
TMSR_MEASUREDOCINFO 是计量主表,主键为c_measuredocid
tmsr_measuredata 是计量子表,外键为c_measuredocid
一条主表的计量数据对应2条子表计量数据(毛重计量和皮重计量)
主表保存净重计量结果


select  t.c_measuredocid,t.c_carrytoolmainid,t.i_msrfinishflag,d.n_tareweight/1000,d.c_taredatetime,d.n_grossweight/1000,d.c_grossdatetime,t.n_suttleweight/1000,t.c_suttleweightdate from 
TMSR_MEASUREDOCINFO t,tmsr_measuredata d
where d.c_measuredocid = t.c_measuredocid
and t.c_measuredocid = 'Q140801133358015'


查询结果为
Q140801133358015   豫EL5998   1    0                                                       67.28   2014-08-01 14:49:16    48.52   2014-08-01 14:49:16
Q140801133358015   豫EL5998   1    18.76    2014-08-01 13:33:58         0                                                48.52   2014-08-01 14:49:16

这车的皮重为18.76吨,时间为 2014-08-01 13:33:58。毛重为67.28 ,时间为 2014-08-01 14:49:16 ,净重48.52,时间和毛重时间相同

我想展示为下面的一条结果

 Q140801133358015      豫EL5998   1   18.76    2014-08-01 13:33:58   67.28   2014-08-01 14:49:16    48.52   2014-08-01 14:49:16

请问应该怎么做?不能用净重减毛重得皮重,要用语句实现。

上面那个有点问题:
select t.c_measuredocid,
       t.c_carrytoolmainid,
       t.i_msrfinishflag,
       t1.n_tareweight / 1000,
       t1.c_taredatetime,
       t2.n_grossweight / 1000,
       t2.c_grossdatetime,
       t.n_suttleweight / 1000,
       t.c_suttleweightdate
  from TMSR_MEASUREDOCINFO t,
       (select d.c_measuredocid, d.n_tareweight / 1000, d.c_taredatetime
          from tmsr_measuredata d
         where d.n_tareweight <> 0) t1,
       (select d.c_measuredocid, d.n_grossweight / 1000, d.c_grossdatetime
          from tmsr_measuredata d
         where d.n_grossweight <> 0) t2
 where t1.c_measuredocid = t.c_measuredocid
   and t2.c_measuredocid = t.c_measuredocid
   and t.c_measuredocid = 'Q140801133358015'
  相关解决方案