2017-06-19 28 views
0

輸入數據使SQL查詢

+---------+----------------+-----------+-----------------+--------------+------+ 
| 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表名列然後我需要有一個記錄其標誌爲「否」。

+0

它的Hive數據庫 – Atul

回答

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' 
) 

的想法是,你得到的所有數據,除非你用標誌具有相同屬性的第二項「不」要省略另一個。

+0

我在輸入中包含了一個ID(序列號)列。執行後,我得到以下錯誤:「行6:8不支持的子查詢表達式'id':子查詢表達式引用父和子查詢表達式,並不是有效的連接條件。」 – Atul

+0

我不確定如何使用Hive執行此操作,因爲它提供HiveQL,但是也許可以使左連接並排除某些元素在不存在時找到的元素不起作用? –