进行SQL查询

时间:2017-06-19 08:39:13

标签: sql hive

输入数据

+---------+----------------+-----------+-----------------+--------------+------+
| country | tgtschema_name | tablename | tgttableversion | col_function | flag |
+---------+----------------+-----------+-----------------+--------------+------+
| zz      | abc_zz         | table1    | 01              | TBD1         | no   |
+---------+----------------+-----------+-----------------+--------------+------+
| bs      | abc_bs         | table2    | 01              | TBD1         | yes  |
+---------+----------------+-----------+-----------------+--------------+------+
| bs      | abc_bs         | table3    | 01              | TBD1         | yes  |
+---------+----------------+-----------+-----------------+--------------+------+
| bs      | abc_bs         | table4    | 01              | TBD1         | yes  |
+---------+----------------+-----------+-----------------+--------------+------+
| bs      | abc_bs         | table4    | 02              | TBD2         | no   |
+---------+----------------+-----------+-----------------+--------------+------+
| do      | abc_do         | table5    | 01              | TBD1         | yes  |
+---------+----------------+-----------+-----------------+--------------+------+
| do      | abc_do         | table6    | 01              | TBD1         | yes  |
+---------+----------------+-----------+-----------------+--------------+------+
| do      | abc_do         | table7    | 01              | TBD1         | yes  |
+---------+----------------+-----------+-----------------+--------------+------+

输出所需数据

+---------+----------------+-----------+-----------------+--------------+------+
| country | tgtschema_name | tablename | tgttableversion | col_function | flag |
+---------+----------------+-----------+-----------------+--------------+------+
| zz      | abc_zz         | table1    | 01              | TBD1         | no   |
+---------+----------------+-----------+-----------------+--------------+------+
| bs      | abc_bs         | table2    | 01              | TBD1         | yes  |
+---------+----------------+-----------+-----------------+--------------+------+
| bs      | abc_bs         | table3    | 01              | TBD1         | yes  |
+---------+----------------+-----------+-----------------+--------------+------+
| bs      | abc_bs         | table4    | 02              | TBD2         | no   |
+---------+----------------+-----------+-----------------+--------------+------+
| do      | abc_do         | table5    | 01              | TBD1         | yes  |
+---------+----------------+-----------+-----------------+--------------+------+
| do      | abc_do         | table6    | 01              | TBD1         | yes  |
+---------+----------------+-----------+-----------------+--------------+------+
| do      | abc_do         | table7    | 01              | TBD1         | yes  |
+---------+----------------+-----------+-----------------+--------------+------+

如果我在国家/地区有多个记录, tgtschema_name tablename 列,那么我需要记录哪个标记是"否"

1 个答案:

答案 0 :(得分:0)

编辑:提供答案时不知道具体的数据库。

假设你有id字段,这样的事情应该有效:

select * 
from t t1 
where not exists (
  select country 
  from t t2 
  where t1.id != t2.id
     t1.country = t2.country and 
     (...add the rest here...) and 
     t2.flag = 'No'
)

这个想法是你获得所有数据,除非你有第二个具有相同属性的条目,并带有标记'否'你想省略另一个。