在配置单元中,需要从表中的值与3个不同的表

时间:2017-09-12 10:30:04

标签: database hive

需要将一个表与不同的表进行比较:

在Hive中,我需要一个查询来比较一个表和三个不同的查找表。

如果记录与所有3个查找表匹配,则记录应更新为"通过"

如果记录中的任何一个记录因任何表格不匹配而失败,则记录应更新并标记为"失败原因"并应显示正确的值

说:

主表

EMPNO EMPNAME Class School Marks1 Marks2 Marks3 
101   Scott   3     MOV    50     70     80 
102   Tiger   6     MVM    60     70     80
103   Rayon   7     COLORS 90     90     90

查找表:

EMPLOYEE:

EMPNO EMPNAME
101 Scott
102 Tiger
103 Spangler
104 Mike
105 Aligarh

地址:

Class School Location  PhoneNumber

4 MVM    Idaho     120232
6 TEM    Texas     120394
3 MOV    Edinburgh 120479
6 PRAM   Vatican   12098
7 LEXI   SALEM     12092
7 Colors SALEM     12092
9 Ray    Shimla    13490

商标:

M1 M2 M3
50 60 80
50 70 80
80 74 79
90 90 90
30 50 45

此处,Master Table的第一条记录将与Employee Table,Address table&标记表

1→主表中的EmpNo 101和EmpName Scott与Employee查找表中的第一条记录匹配,与地址查找表中的第三条记录匹配。 Marks查找表中的第二条记录 - 应该在所有表中传递

2→ EmpNo 102和Empname Tiger与Employee查找表中的第二条记录匹配,但与地址查找表中的任何记录都不匹配,与Marks表不匹配 它应该更新与地址和标记表

不匹配

3→ Empno 103和Empname Rayon与Employee查找表中的任何记录都不匹配,但与地址查找表中的第六条记录匹配,并与Marks查找表中的第四条记录匹配

主表中的记录1应在所有3个表中更新为通过 主表中的记录2应更新为地址和标记查找表中的失败 主表中的记录3应更新为员工查找表中的失败

+-------+---------+-------+---------+--------+--------+--------+----------------------------------------------------------------------+----------------------------------------------------+
| EMPNO | EMPNAME | CLASS | SCHOOL  | MARKS1 | MARKS2 | MARKS3 |                                Result                                |                       Reason                       |
+-------+---------+-------+---------+--------+--------+--------+----------------------------------------------------------------------+----------------------------------------------------+
|   101 | SCOTT   |     3 | MOV     |     50 |     70 |     80 | Matched all 3 tables                                                 | NA                                                 |
|   102 | TIGER   |     6 | MVM     |     60 |     70 |     80 | Did not match in  Address and Mark Table                             | School value should be MVM  & Marks 1 should be 60 |
|   103 | RAYON   |     7 | COLORS  |     90 |     90 |     90 | Did not match in Employee look up table but other tables are matched | EMPNAME SHOULD BE RAYON                            |
+-------+---------+-------+---------+--------+--------+--------+----------------------------------------------------------------------+----------------------------------------------------+

1 个答案:

答案 0 :(得分:0)

这给出了基本的表示,你可以随意进一步操作它。

select      t.*
           ,case when e.EMPNO is null then 0 else 1 end     as EMPLOYEE
           ,case when a.Class is null then 0 else 1 end     as Address
           ,case when m.M1    is null then 0 else 1 end     as Marks


from                    Master      t

            left join   EMPLOYEE    e

            on          e.EMPNO     =
                        t.EMPNO

                    and e.EMPNAME   =
                        t.EMPNAME

            left join   Address     a

            on          a.Class     =
                        t.Class

                    and a.School    =
                        t.School

            left join   Marks       m

            on          m.M1        =
                        t.Marks1

                    and m.M2        =
                        t.Marks2

                    and m.M3        =
                        t.Marks3
;
+--------+----------+--------+---------+---------+---------+---------+-----------+----------+--------+
| empno  | empname  | class  | school  | marks1  | marks2  | marks3  | employee  | address  | marks  |
+--------+----------+--------+---------+---------+---------+---------+-----------+----------+--------+
| 101    | Scott    | 3      | MOV     | 50      | 70      | 80      | 1         | 1        | 1      |
| 102    | Tiger    | 6      | MVM     | 60      | 70      | 80      | 1         | 0        | 0      |
| 103    | Rayon    | 7      | COLORS  | 90      | 90      | 90      | 0         | 0        | 1      |
+--------+----------+--------+---------+---------+---------+---------+-----------+----------+--------+