subquery (nested query) can use columns from the tables listed in the main query

Source: Internet
Author: User
Tags getdate join
In short, a nested query works IndependentOf the enclosing SQL statement and can make use of any of the column values From the tables listed in the enclosing statement ' s FROM clause. You can use the nested queries to perform multi-table operations without have to JOIN rows in multiple related tables. However, if you need the data values from multiple tables, or if you want individual column values and aggregate function Valu Es in the same row in the results table, you can nest a subquery with the aggregate function so you need in the SELECT C Lause of a multi-table query or JOIN.
Select
    trade_date,
    symbol,
    shares * Price as "total trade",
    (
        select
            COUNT (*)
            from Trades
        WHERE
            trade_date > GETDATE ()-365 and
        cust_id = CID--cid is in Customer not the trades which is T He is table in this
            --subquery's FROM clause
    ) as ' Count ',
    (SELECT sum (price
            ) * SUM (shares) C16/>from
            Trades
        WHERE
            trades.trade_date >= GETDATE ()-365 and
        cust_id = CID
    ) as                       ' Total $ Volume) ',
    CID as                     ' Cust ID ',
    TRIM (f_name) + ' +l_name as ' Customer ' from
    EI. CUSTOMERS
JOIN EI. TRADES
on
    CID = cust_cid
WHERE
    shares * Price >= 100000 and
trade_date >= GETDATE () -365< C34/>order by
    customer;


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.