QD                   ZD                   SJ
-------- -------------------- --------------------
田林                 桂林                 8:00
田林                 桂林                 9:00
桂林                 田林                 8:00
桂林                 田林                 8:00
田林                 宜山                 8:00
宜山                 田林                 8:006 rows selected
统计这个表里面 的线路条数 
起点和终点 一致的算一条
现在结果是两条

解决方案 »

  1.   

    select count(*) from 表名 group by QD,ZD
      

  2.   

    select count(*) from (select qd,zd from tb group by qd,zd having count(*) > 1) t
      

  3.   

    select count(distinct(case when QD > ZD then QD||ZD else ZD||QD end) )
    from table
      

  4.   

    已經建表測試過的SQL:select count(*)/2 from (
    select distinct QD,ZD from tbl_luxian
    union 
    select distinct ZD,QD from tbl_luxian
    ) a
    功能雖實現,效率可能不太好~ 期待高手
      

  5.   

    这个效率应该挺好了。就是如果出现单程情况的会有问题。抛砖引玉发个笨方法
    select
     (select count(*) from (select distinct  QD,ZD from lsb_tmp)  )-
     (select count(*)/2 from (select distinct  QD,ZD from lsb_tmp) a ,(select distinct  QD,ZD from lsb_tmp) b where a.QD=b.ZD and b.QD=a.ZD ) as result from dual ;
      

  6.   

    经测试可行:
    select count(*) from (select rank() over(partition by QD,ZD
     order by id) rank from 表名) n where n.rank = '2'order by 后面 跟其他不会重复的字段。这样就是起点和终点一致的数据有n条也算作1条
      

  7.   

    总结了大家的:
    第一种:select count(distinct(case
                            when QD > ZD then
                             QD || ZD
                            else
                             ZD || QD
                          end)) as 总线路
      from route;第二种:
    select count(*)
      from (SELECT a.qd, a.zd
              FROM route a
              JOIN route b ON a.qd = b.zd
                          AND a.zd = b.qd
             WHERE a.qd < b.qd
             GROUP BY a.qd, a.zd
             );第三种:
    select count(*) / 2
      from (select distinct QD, ZD
              from route
            union
            select distinct ZD, QD from route) a;第三种:
            select count(*) / 2
      from (select distinct QD, ZD
              from route
            union
            select distinct ZD, QD from route) a;第四种:
    select (select count(*) from (select distinct QD, ZD from route)) -
           (select count(*) / 2
              from (select distinct QD, ZD from route) a,
                   (select distinct QD, ZD from route) b
             where a.QD = b.ZD
               and b.QD = a.ZD) as result
      from dual;其中第二种,出现单程统计不到了。