• 数据库>SQL Server2005>第4季SQL从入门到提高>13练习1


    表结构如下:

    相关查询:

    --所有数据中通话时间最长的5条记录
    select top 5 * from CallRecords1
    order by dateDiff(second,StartDateTime,EndDateTime) desc

    --输出所有数据中拨打长途号码(对方号码以0开头)的总时长。like,sum
    select sum(dateDiff(second,StartDateTime,EndDateTime)) from CallRecords1
    where TelNum like '0%'
    --输出本月拨打电话次数最多的前三个呼叫员的编号
    select * from CallRecords1

    --分组出,每个呼叫员打的电话次数
    select CallerNumber,count(*) from CallRecords1
    group by CallerNumber

    --根据每个呼叫员打的电话次数进行排序,然后取前三
    select top 3 CallerNumber,count(*) as countsNumber from CallRecords1
     group by CallerNumber order by countsNumber desc
    --汇总(市内号码总时长)(长途号码总时长)
    select * from CallRecords1

    --市内号码,TelNum开头不带0,的每个拨打时长秒数
    select datediff(second,StartDateTime,EndDateTime) from CallRecords1
    where TelNum not like '0%'

    --进行汇总
    select sum(datediff(second,StartDateTime,EndDateTime)) as 市内号码
    from CallRecords1
    where TelNum not like '0%'

    --对长途号码,也如下统计
    select sum(datediff(second,StartDateTime,EndDateTime)) as 长途
    from CallRecords1
    where TelNum like '0%'

    --两者结合在一起
    select (
    select sum(datediff(second,StartDateTime,EndDateTime)) as 市内号码
    from CallRecords1
    where TelNum not like '0%'
    ) as 市内号码,
    (
    select sum(datediff(second,StartDateTime,EndDateTime)) as 长途
    from CallRecords1
    where TelNum like '0%'
    )as 长途号码

    --第二种方法汇总
    select sum((
    case
    when TelNum not like '0%' then datediff(second,StartDateTime,EndDateTime)
    else 0
    end
    ))as 市内号码,
    sum((
    case
    when TelNum like '0%' then datediff(second,StartDateTime,EndDateTime)
    else 0
    end
    ))as 长途号码
    from CallRecords1
     
    --呼叫员编号,对方号码,通话时长,加汇总
    select CallerNumber,TelNum,datediff(second,StartDateTime,EndDateTime) from CallRecords1
    union all
    select N'汇总',(
    select convert(varchar(50),sum(datediff(second,StartDateTime,EndDateTime)))
    from CallRecords1
    where TelNum not like '0%'
    ),
    (
    select sum(datediff(second,StartDateTime,EndDateTime))
    from CallRecords1
    where TelNum like '0%'
    )
    --如果没进行convert转换,你看下查询结果,思考有啥不同

    合乎自然而生生不息。。。
  • 相关阅读:
    左连接,右连接,内连接及全连接语法及区别
    System.getProperty("user.dir");
    mysql count统计多列值相同的条数
    mybatis 查询返回参数包含list的映射写法
    idea 永久破解
    springboot读取自定义配置文件及乱码问题
    linux CentOS7 firewall
    关于@Valid 不生效的问题
    简单记录springboot项目访问静态资源的配置
    maven 远程部署到tomcat服务器
  • 原文地址:https://www.cnblogs.com/samwu/p/2083149.html
Copyright © 2020-2023  润新知