假设一个表有6个字段c1,c2,c3,c4,c5,c6,有如下的sql语句:
select c1,count(distinct(c6)) from tbl where c3>1 group by c3;
python中asq模块可以实现类似的查询,以下用一个示例说明。
#!/usr/bin/env python2.7 #-*- encoding: utf-8 -*- import os from datetime import * import time from asq.initiators import query def test1(): src_list = [[6,6,6,3,7,9],[3,1,1,45,6,34],[2,9,5,435,56,34],[8,6,8,23,4,32],[2,6,8,435,2,8],[1,3,6,432,5,22],[1,3,5,6,2,435],[2,4,6,34,2,12]] res_list = query(src_list).where(lambda p: p[2]>1).group_by(lambda p:p[2]).select(lambda p:[p[0][0],p.distinct(lambda p: p[5]).count()]).to_list() print res_list if __name__ == '__main__': test1()
运行结果:
[[6, 3], [2, 2], [8, 2]]
[Finished in 0.1s]
asq函数的逻辑顺序依次是: where-->group by --> select --> distinct -->返回结果列表。