如何从表中优先获取列

时间:2018-09-28 04:33:00

标签: sql oracle

我有三个表,其中SELECT语句中的select列优先。

假设表A有一些列,例如:

表A:

A_ID     |name
---------|-------
1        |name1

表B:

purchase |A_ID |type | market | group | rate    |   max  |   min
---------|-----|-----|--------|-------|---------|--------|---------
    1    |  1  |  1  |   1    |   1   |  0.12   |  1000  |   500
    1    |  1  |  2  |   1    |   1   |  0.3    |  2000  |   1500
    0    |  1  |  3  |   1    |   1   |  0.2    |  5000  |   800
    0    |  1  |  4  |   1    |   1   |  0.6    |  8000  |   2800
    0    |  1  |  6  |   1    |   1   |  0.7    |  null  |   2800

表C:

purchase |A_ID |type | market | group | rate    |   max  |   min
---------|-----|-----|--------|-------|---------|--------|---------
    1    |  1  |  1  |   1    | null  |  0.2    |  null  |   null
    1    |  1  |  2  |   1    | null  |  null   |  5000  |   3000
    0    |  1  |  3  |   1    | null  |  0.5    |  3000  |   1000
    0    |  1  |  5  |   1    | null  |  0.4    |  3800  |   2000
    0    |  1  |  6  |   1    | null  |  null   |  null  |   3000

所需结果:

purchase |A_ID |type | market | rate    |   max  |   min
---------|-----|-----|--------|---------|--------|---------
    1    |  1  |  1  |   1    |  0.2    |  1000  |   500
    1    |  1  |  2  |   1    |  0.3    |  5000  |   3000
    0    |  1  |  3  |   1    |  0.5    |  3000  |   1000
    0    |  1  |  5  |   1    |  0.4    |  3800  |   2000
    0    |  1  |  4  |   1    |  0.6    |  8000  |   2800
    0    |  1  |  6  |   1    |  0.7    |  null  |   3000

从列中获取价值的规则:

1- Table C的优先级高于Table B,这意味着如果两个变量在同一列中都具有值,则结果将从Table C中提取,除非该值为null

2-结果可以是GROUP BY上的purchase, type, market

3-结果具有FULL JOIN,这意味着如果某行在另一侧具有等效的行,则使用优先级获取值,如果不是整行都进入结果

4-列选择值的优先级(比率|最大|最小):

  • 如果rate中的列TABLE C有值,而没有考虑TABLE B上的值 ==>从TABLE C
  • 中选择结果
  • 如果rate中的列TABLE Cnull,但在TABLE B中有值==>从TABLE B中选择结果

2 个答案:

答案 0 :(得分:1)

这是使用sql server语法的,我确定您可以根据需要进行更改:

首先设置样本数据:

declare @a table(purchase int,A_ID int,[type] int,market int,[group] int,rate decimal(5,2),[max] int,[min] int)
insert @a values (1,1,1,1,1,0.12,1000,500)
,(1,1,2,1,1,0.3,2000,1500)
,(0,1,3,1,1,0.2,5000,800)
,(0,1,4,1,1,0.6,8000,2800)
,(0,1,6,1,1,0.7,null,2800)

declare @b table(purchase int,A_ID int,[type] int,market int,[group] int,rate decimal(5,2),[max] int,[min] int)
insert @b values 
(1,1,1,1,null,0.2,null,null)
,(1,1,2,1,null,null,5000,3000)
,(0,1,3,1,null,0.5,3000,1000)
,(0,1,5,1,null,0.4,3800,2000)
,(0,1,6,1,null,null,null,3000)

然后查询:

select coalesce(b.purchase,a.purchase) purchase,
    coalesce(b.A_ID,a.A_ID) A_ID,
    coalesce(b.[type],a.[type]) [type],
    coalesce(b.market,a.market) market,
    coalesce(b.rate,a.rate) rate,
    coalesce(b.[max],a.[max]) [max],
    coalesce(b.[min],a.[min]) [min]
from @a a
full outer join @b b on b.purchase=a.purchase and b.[type]=a.[type] and b.market=a.market
order by rate

添加任何您需要的排序。

答案 1 :(得分:0)

使用Coalesce()函数始终返回列表中的第一个非空值

select coalesce(b.purchase,a.purchase) purchase,
       coalesce(b.A_ID,a.A_ID) A_ID,
       coalesce(b.[type],a.[type]) [type],
       coalesce(b.market,a.market) market,
       coalesce(b.rate,a.rate) rate,
       coalesce(b.[max],a.[max]) [max],
       coalesce(b.[min],a.[min]) [min]
from tableB a full outer join tableC b
on a.purchase=b.purchase and a.[type]=b.[type]