在配置单元中,需要一个表中的值来与3个不同的表进行比较

djp7away  于 2021-06-26  发布在  Hive
关注(0)|答案(1)|浏览(222)

需要将一个表与不同的表进行比较:
在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

查找表:
员工:

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

这里,主表中的第一条记录将与employee表、address表和markts表进行比较
1->主表中的empno101和empname scott与employee lookup表中的第一条记录匹配,与address lookup表中的第三条记录匹配,与marks lookup表中的第二条记录匹配-它应该在所有表中传递时更新
2->empno 102和empname tiger与employee lookup table中的第二条记录匹配,但与address lookup table中的任何记录都不匹配,与marks table不匹配应该更新它与address和marks table不匹配
3->empno103和empname rayon与employee lookup表中的任何记录都不匹配,但与address lookup表中的第六条记录匹配,与marks lookup表中的第四条记录匹配
主表中的记录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                            |
+-------+---------+-------+---------+--------+--------+--------+----------------------------------------------------------------------+----------------------------------------------------+
uqjltbpv

uqjltbpv1#

这提供了基本的表示,您可以根据需要进一步操作它。

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      |
+--------+----------+--------+---------+---------+---------+---------+-----------+----------+--------+

相关问题