国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 數(shù)據(jù)庫(kù) > 文庫(kù) > 正文

Hive SQL去重a b和b a類(lèi)別

2024-09-07 22:12:57
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
  昨天開(kāi)發(fā)找到我們DBA,要我們寫(xiě)一條Hive SQL。
 
  需求:
 
  有一個(gè)t表,主要有機(jī)場(chǎng)名稱(chēng)airport,機(jī)場(chǎng)的經(jīng)緯度distance這兩個(gè)列組成,想得到所有距離小于100的兩個(gè)機(jī)場(chǎng)名。
 
  其實(shí)寫(xiě)這個(gè)SQL的邏輯并不是很困難,難點(diǎn)是如何去重復(fù)值,
 
  我用MySQL模擬的一個(gè)表,其實(shí)Hive語(yǔ)法和SQL差不多,插入了三條數(shù)據(jù),a, b, c 分別代表三個(gè)機(jī)場(chǎng)名稱(chēng),結(jié)構(gòu)如下:
 
  mysql> show create table t/G
  *************************** 1. row ***************************
         Table: t
  Create Table: CREATE TABLE `t` (
    `airport` varchar(10) DEFAULT NULL,
    `distant` int(11) DEFAULT NULL
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8
  1 row in set (0.00 sec)
 
  mysql> select * from t;
  +---------+---------+
  | airport | distant |
  +---------+---------+
  | a       |     130 |
  | b       |     140 |
  | c       |     150 |
  +---------+---------+
  3 rows in set (0.00 sec)
  通過(guò)!=篩選掉本機(jī)場(chǎng)自己之間的比較,用abs函數(shù)取絕對(duì)值得到位置小于100的兩個(gè)機(jī)場(chǎng)
 
  mysql> select t1.airport, t2.airport from t t1,t t2 where t1.airport != t2.airport and abs(t1.distant-t2.distant) < 100;
  +---------+---------+
  | airport | airport |
  +---------+---------+
  | b       | a       |
  | c       | a       |
  | a       | b       |
  | c       | b       |
  | a       | c       |
  | b       | c       |
  +---------+---------+
  6 rows in set (0.00 sec)
  但是問(wèn)題來(lái)了,(b,a) 與(a,b),(c,a)與(a,c),(c,b)與(b,c)這里被我們視為重復(fù)值,我們只需要得到其中某一行的數(shù)據(jù),就知道是哪兩個(gè)機(jī)場(chǎng)名了,那么,如何去掉這個(gè)重復(fù)值呢?
 
  貌似distinct,group by都派不上用場(chǎng)了,最后咨詢(xún)了一位資深的SQL高手,找到了這么一個(gè)函數(shù)hex(),可以把一個(gè)字符轉(zhuǎn)化成十六進(jìn)制,Hive也有對(duì)應(yīng)的函數(shù),效果如下:
 
  mysql> select t1.airport,hex(t1.airport), t2.airport,hex(t2.airport) from t t1,t t2 where t1.airport != t2.airport and abs(t1.distant-t2.distant) < 100;
  +---------+-----------------+---------+-----------------+
  | airport | hex(t1.airport) | airport | hex(t2.airport) |
  +---------+-----------------+---------+-----------------+
  | b       | 62              | a       | 61              |
  | c       | 63              | a       | 61              |
  | a       | 61              | b       | 62              |
  | c       | 63              | b       | 62              |
  | a       | 61              | c       | 63              |
  | b       | 62              | c       | 63              |
  +---------+-----------------+---------+-----------------+
  6 rows in set (0.00 sec)
  這樣我們就可以通過(guò)比較機(jī)場(chǎng)1和機(jī)場(chǎng)2的大小,來(lái)去掉重復(fù)值了
 
  mysql> select t1.airport, t2.airport from t t1,t t2 where t1.airport != t2.airport and hex(t1.airport) < hex(t2.airport) and abs(t1.distant-t2.distant) < 100;
  +---------+---------+
  | airport | airport |
  +---------+---------+
  | a       | b       |
  | a       | c       |
  | b       | c       |
  +---------+---------+
  3 rows in set (0.00 sec)
  最后再優(yōu)化一下,結(jié)果如下:
 
  mysql> select t1.airport, t2.airport from t t1,t t2 where hex(t1.airport) < hex(t2.airport) and abs(t1.distant-t2.distant) < 100;
  +---------+---------+
  | airport | airport |
  +---------+---------+
  | a       | b       |
  | a       | c       |
  | b       | c       |
  +---------+---------+
  3 rows in set (0.00 sec)

(編輯:武林網(wǎng))

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 安阳县| 长岭县| 获嘉县| 晋城| 简阳市| 新疆| 恩平市| 顺义区| 杨浦区| 上思县| 泉州市| 中宁县| 门头沟区| 浙江省| 赤壁市| 历史| 潞城市| 兴城市| 玉溪市| 城步| 夏邑县| 普陀区| 山东| 瑞金市| 满洲里市| 汉源县| 靖江市| 得荣县| 偏关县| 马鞍山市| 张家口市| 宁远县| 峨眉山市| 临潭县| 台湾省| 吕梁市| 龙岩市| 文成县| 淮南市| 富蕴县| 昆山市|