zoukankan      html  css  js  c++  java
  • Subqueries: Any/All

    1. Subquery Any

    Syntax:

    -- any
    -- remove inner order by
    select EnglishProductName, ListPrice from DimProduct
    where ListPrice > any(select avg(ListPrice) from DimProduct
    group by ProductSubcategoryKey)
    order by ListPrice asc

    Analysis:

    It’s asking for a list of products whose price is greater than any of the average list prices from the subquery. In this case, the lowest average is 7.95. Therefore it’s showing all of those products with a ListPrice of more than 7.95.

    2. Subquery All

    Syntax:

    -- all with nulls
    select EnglishProductName, ListPrice from DimProduct
    where ListPrice > all(select avg(ListPrice) from DimProduct
    group by ProductSubcategoryKey)
    order by ListPrice asc

    Analysis:

    It’s looking for all products with a price greater than all of the average prices from the subquery. In other words, it’s looking for all those productes whose price is greater than the highest of the subcategory average prices.

    Caution:

    Do not let null value join into the comparison, otherwise, you will get incorrect results.

    But there was an average price with a null value, A null value, if you like, is indeterminate --- so it cannot be guaranteed to really be the maximum. The maximum is unknown, so the outer query fails to show any records.

    Solution:

    Adding a Having clause to the subquery --- this is going to eliminate all average prices with null values.

    -- all without nulls
    select EnglishProductName, ListPrice from DimProduct
    where ListPrice > all(select avg(ListPrice) from DimProduct group by 
    ProductSubcategoryKey having avg(ListPrice) is not null)
    order by ListPrice asc
  • 相关阅读:
    GDI+学习笔记2
    GDI+学习笔记1- 概述
    Java网络编程和NIO详解1:JAVA 中原生的 socket 通信机制
    Java网络编程和NIO详解开篇:Java网络编程基础
    我在阿里工作的这段时间里,都学到了哪些东西
    在大公司做凤尾,还是在小公司做鸡头?
    蚂蚁金服财富技术部,诚招Java研发工程师。校招内推!!!
    测试课程
    新笔记
    阅读书籍电技术
  • 原文地址:https://www.cnblogs.com/aot/p/3148735.html
Copyright © 2011-2022 走看看