zoukankan      html  css  js  c++  java
  • Question[SQL]: How can I list all book with prices greather than the average price of books of the same type?

    Question: How can I list all book with prices greather than the average price of books of the same type?
    In database pubs, have a table named titles , its column named price mean the price of the book, and another named type mean the type of books.
    Now I want to get the result as below:

     程序代码
    type         title                                                                            price                
    ------------ -------------------------------------------------------------------------------- ---------------------
    business     The Busy Executive's Database Guide                                              19.9900



    Answer:

    select a.[type], a.[title], a.[price]
        
    from [pubs].[dbo].[titles] a,
        (
    select [type][price]=AVG([price]from [pubs].[dbo].[titles] group by [type]) b
        
    where a.[type]=b.[type] and a.[price]>b.[price]

    这里还有一个类拟的试题:

    第一题比较简单,查询出销售表中,销售额大于本地区平均水平的记录,用一条sql语句就搞定了。

    Sales

    OrderID

    Region

    Total

    1

    A

    100.00

    2

    C

    80.00

    3

    A

    130.00

    4

    B

    90.00

    5

    B

    100.00

    6

    C

    120.00

    7

    A

    90.00

    8

    C

    90.00

    9

    B

    80.00

    Sql语句:

    select * from sales as s 
      
    inner join 
       (
    select avg(total) as avge,region from sales group by region) avgtable 
      
    on s.region = avgtable.region 
      
    where total > avgtable.avge


  • 相关阅读:
    SkylineGlobe for web开发是否支持IE11?
    OGC标准服务 WMS WCS WFS WPS
    SkylineGlobe TerraExplorer Pro 遇到模型显示有锯齿怎么办?
    SkylineGlobe TerraExplorer Pro 7.0 Web 控件版 第一行示例代码
    Cesium3DTileset示例
    Win7 64位系统,IE11,如何让IE的Tab强制运行64位内核?
    SkylineGlobe系列软件对机器配置要求
    VS 2015 搭建Google Test
    7种排序算法的c++实现
    哈希表应用实例
  • 原文地址:https://www.cnblogs.com/chenjunbiao/p/1760183.html
Copyright © 2011-2022 走看看