oracle optp,oracle三张关联表,表1单记录对表2表3多记录,需要关联表1、2、3进行数据的有序导出,导出效果见附件图...

匿名用户

1级

2016-08-25 回答

with t1 as

(

select 'ttt' optpmst_id, 'Task1' optpmst_sht from dual union

all

select 'ppp' optpmst_id, 'Task2' optpmst_sht from dual

),

t2

as

(

select 'ttt' optpmst_id, '1' optplin_seq, 'CC1' optptsk_sht from dual

union all

select 'ttt' optpmst_id, '2' optplin_seq, 'CC2' optptsk_sht from

dual union all

select 'ttt' optpmst_id, '3' optplin_seq, 'CC3' optptsk_sht

from dual union all

select 'ppp' optpmst_id, '1' optplin_seq, 'CC1'

optptsk_sht from dual union all

select 'ppp' optpmst_id, '2' optplin_seq,

'CC2' optptsk_sht from dual union all

select 'ppp' optpmst_id, '3'

optplin_seq, 'CC3' optptsk_sht from dual union all

select 'ppp' optpmst_id,

'4' optplin_seq, 'CC4' optptsk_sht from dual

),

t3 as

(

select

'ttt' optpmst_id, '1' dgr_seq, 'DG1' dgr_sht, 'RK1' dgr_dsc from dual union

all

select 'ttt' optpmst_id, '2' dgr_seq, 'DG2' dgr_sht, 'RK2' dgr_dsc from

dual union all

select 'ttt' optpmst_id, '3' dgr_seq, 'DG3' dgr_sht, 'RK3'

dgr_dsc from dual union all

select 'ttt' optpmst_id, '4' dgr_seq, 'DG4'

dgr_sht, '' dgr_dsc from dual union all

select 'ttt' optpmst_id, '5' dgr_seq,

'DG5' dgr_sht, '' dgr_dsc from dual union all

select 'ppp' optpmst_id, '1'

dgr_seq, 'DG1' dgr_sht, 'RK1' dgr_dsc from dual union all

select 'ppp'

optpmst_id, '2' dgr_seq, 'DG2' dgr_sht, 'RK2' dgr_dsc from dual

)

select t1.optpmst_sht,t2.optpmst_id,t2.optplin_seq, t2.optptsk_sht,

t3.dgr_seq, t3.dgr_sht, t3.dgr_dsc

from t1 inner join t2 on

t1.optpmst_id=t2.optpmst_id

full join t3 on t2.optpmst_id=t3.optpmst_id and

t2.optplin_seq=t3.dgr_seq

;

追问:

亲 这个效果挺好的!但是需要以orcl数据库 sql的形式,去查询对应的三张表内的数据。

追答:

上面三个临时表是测试用的。

你把

select t1.optpmst_sht,t2.optpmst_id,t2.optplin_seq, t2.optptsk_sht,

t3.dgr_seq, t3.dgr_sht, t3.dgr_dsc

from t1 inner join t2 on

t1.optpmst_id=t2.optpmst_id

full join t3 on t2.optpmst_id=t3.optpmst_id and

t2.optplin_seq=t3.dgr_seq

这个改成你自己的表和字段就可以了

追问:

我调整的时候,必须是表2记录比表3记录条目多,才能保证表3会完整导出,否则是已表2最大化为准导出的!

追答:

那这样的话,你的图片的结果是不对的。ttt的危险点4和5不应该显示出来。

select t1.optpmst_sht,t2.optpmst_id,t2.optplin_seq, t2.optptsk_sht,

t3.dgr_seq, t3.dgr_sht, t3.dgr_dsc

from t1 inner join t2 on

t1.optpmst_id=t2.optpmst_id

left join t3 on t2.optpmst_id=t3.optpmst_id and

t2.optplin_seq=t3.dgr_seq

追问:

bbdff78b591ca5e434d2cf9c0aa00154.png

图片想要的效果是对的!是我再你给的语句加了二个过滤条件,导致表3行不能全部显示了。能看下问题?

追答:

首先,之前我测试用的三个表是按照你的图片中创建的。结果没问题。

然后,你增加了过滤条件造成不能全部显示,这就需要看你这个条件跟你想要的数据的关系,从目前你提供的Sql看,你想要的三行的rmoptpmst.optyp_no不是46,所以出不来。因为 rmoptpmst 是查询的主表。即

select * from rmoptpmst where rmoptpmst.optyp_no='46';

结果不会出现你想要的哪行的optpmst_no.

最后,如果想调整你最后提供的SQL,需要提供这个SQL里用到的三个表格结构和数据以及三个表的管理关系。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个用 C 语言实现背包问题的代码,它能够输出背包能装下的最大价值和所有组成最大价值物品的重量的索引: ```c #include <stdio.h> #define MAX 100 // 背包问题函数 int knapsack(int n, int W, int w[], int v[], int x[], int optp[]) { int i, j, p, optv[MAX][MAX] = {0}, sol[MAX][MAX] = {0}; for (j = 0; j <= W; j++) { for (i = 1; i <= n; i++) { if (w[i] > j) { optv[i][j] = optv[i - 1][j]; } else { if (optv[i - 1][j] >= optv[i - 1][j - w[i]] + v[i]) { optv[i][j] = optv[i - 1][j]; sol[i][j] = sol[i - 1][j]; } else { optv[i][j] = optv[i - 1][j - w[i]] + v[i]; for (p = 1; p <= i; p++) { sol[i][j] = sol[i - 1][j - w[i]]; if (sol[i][j] & (1 << (p - 1))) { sol[i][j] ^= (1 << (p - 1)); } } sol[i][j] |= (1 << (i - 1)); } } } } *optp = optv[n][W]; for (i = 1, j = 0; i <= n; i++) { if (sol[n][W] & (1 << (i - 1))) { x[++j] = i; } } return j; } int main() { int n, W, w[MAX], v[MAX], x[MAX], optp, i, j, k = 0; // 输入物品数量和背包容量 printf("Enter the number of items: "); scanf("%d", &n); printf("Enter the capacity of knapsack: "); scanf("%d", &W); // 输入每个物品的重量和价值 printf("Enter the weight and value of each item:\n"); for (i = 1; i <= n; i++) { printf("Item %d: ", i); scanf("%d %d", &w[i], &v[i]); } // 求解背包问题 int num = knapsack(n, W, w, v, x, &optp); // 输出结果 printf("The opt.val is %d\nThe opt.sol(s) are: ", optp); for (i = 1; i <= num; i++) { for (j = k + 1; j < x[i]; j++) { printf("%d ", j); } printf("%d ", x[i]); k = x[i]; } for (i = k + 1; i <= n; i++) { printf("%d ", i); } printf("\n"); return 0; } ``` 其中,`knapsack()` 函数用于解决背包问题,它的参数分别是物品数量 `n`、背包容量 `W`、每个物品的重量数组 `w[]`、价值数组 `v[]`、最优解数组 `x[]` 和最优价值指针 `optp`。函数返回值是最优解数组 `x[]` 中元素的数量。 该函数实现使用动态规划算法求解背包问题,它计算了所有可能的重量和价值组合,并记录了每种组合最优价值和最优解,最终返回最优价值和最优解。在计算过程中,用 `optv[i][j]` 示在前 `i` 个物品中,背包容量为 `j` 时的最优价值,用 `sol[i][j]` 示在前 `i` 个物品中,背包容量为 `j` 时的最优解。 在输出结果的部分,我们用一个循环遍历最优解数组 `x[]`,将其中每个元素示的物品编号输出,即可得到所有组成最大价值的物品的重量的索引。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值