当前位置: 代码迷 >> Java Web开发 >> 这个SQL如何写
  详细解决方案

这个SQL如何写

热度:115   发布时间:2016-04-17 10:38:03.0
这个SQL怎么写?
QD ZD SJ
-------- -------------------- --------------------
田林 桂林 8:00
田林 桂林 9:00
桂林 田林 8:00
桂林 田林 8:00
田林 宜山 8:00
宜山 田林 8:00

6 rows selected
统计这个表里面 的线路条数 
起点和终点 一致的算一条
现在结果是两条

------解决方案--------------------
select count(*) from 表名 group by QD,ZD

------解决方案--------------------
select count(*) from (select qd,zd from tb group by qd,zd having count(*) > 1) t
------解决方案--------------------
select count(distinct(case when QD > ZD then QD||ZD else ZD||QD end) )
from table
------解决方案--------------------
已經建表測試過的SQL:
SQL code
select count(*)/2 from (select distinct QD,ZD from tbl_luxianunion select distinct ZD,QD from tbl_luxian) a
------解决方案--------------------
探讨
已經建表測試過的SQL:

SQL code


select count(*)/2 from (
select distinct QD,ZD from tbl_luxian
union
select distinct ZD,QD from tbl_luxian
) a



功能雖實現,效率可能不太好~ 期待高手

------解决方案--------------------
经测试可行:
select count(*) from (select rank() over(partition by QD,ZD
 order by id) rank from 表名) n where n.rank = '2'

order by 后面 跟其他不会重复的字段。这样就是起点和终点一致的数据有n条也算作1条
  相关解决方案