WEB开发网
开发学院数据库MSSQL Server SQL高手篇:精妙SQL语句介绍 阅读

SQL高手篇:精妙SQL语句介绍

 2007-11-11 04:41:07 来源:WEB开发网   
核心提示: ·一个sql语句,包含有几乎所有标准查询·经验分享交流:常用SQL语句技法·SQL语句和存储过程 查询语句的流程控·实战SQL语句收集(不断更新中--)·SQL语句性能优化--LECCO SQL Expert·查询同一表内多字段同时重
    ·一个sql语句,包含有几乎所有标准查询
    ·经验分享交流:常用SQL语句技法
    ·SQL语句和存储过程 查询语句的流程控
    ·实战SQL语句收集(不断更新中--)
    ·SQL语句性能优化--LECCO SQL Expert
    ·查询同一表内多字段同时重复记录的SQL
    ·数据库人员手边系列:SQL语句导入导出
    ·得出SQL语句的执行时间的方法
    ·使用一条SQL语句删除表中重复记录
    ·Transact_SQL小手册(各种sql语句大集
  说明: 从数据库中去一年的各单位电话费统计(电话费定额贺电化肥清单两个表来源)

  SQL:

  SELECT a.userper, a.tel, a.standfee, TO_CHAR(a.telfeedate, 'yyyy') AS telyear, SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '01', a.factration)) AS JAN, SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '02', a.factration)) AS FRI, SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '03', a.factration)) AS MAR, SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '04', a.factration)) AS APR, SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '05', a.factration)) AS MAY, SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '06', a.factration)) AS JUE,SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '07', a.factration)) AS JUL, SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '08', a.factration)) AS AGU, SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '09', a.factration)) AS SEP, SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '10', a.factration)) AS OCT, SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '11', a.factration)) AS NOV,SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '12', a.factration)) AS DEC FROM (SELECT a.userper, a.tel, a.standfee, b.telfeedate, b.factration FROM TELFEESTAND a, TELFEE b WHERE a.tel = b.telfax) a GROUP BY a.userper, a.tel, a.standfee, TO_CHAR(a.telfeedate, 'yyyy')   

  说明:四表联查问题:

  SQL: select * from a left inner join b on a.a=b.b right inner join c on a.a=c.c inner join d on a.a=d.d where .....   

  说明:得到表中最小的未使用的ID号

  SQL:

  SELECT (CASE WHEN EXISTS(SELECT * FROM Handle b WHERE b.HandleID = 1) THEN MIN(HandleID) + 1 ELSE 1 END) as HandleID FROM Handle WHERE NOT HandleID IN (SELECT a.HandleID - 1 FROM Handle a)  

  

上一页  1 2 

Tags:SQL 高手 精妙

编辑录入:coldstar [复制链接] [打 印]
赞助商链接