SQL joint query | SQL cross query | the order by clause is invalid in views, nested functions, derived tables, and subqueries unless top is specified at the same time.

Source: Internet
Author: User
  • Select * from (
    Select shengname, ID as Sid, 1 as ord from szsheng
    Union all
    Select shiname, shengid, 2 as ord from szshi)
    Order by A. Sid ASC, ord ASC

    You can get the above results, but directly use this statement to manually create a view in the T-SQL without a prompt
    The order by clause is invalid in views, nested functions, derived tables, and subqueries unless top is specified at the same time.

    Put this statement in the Enterprise Manager to correct the error. The correct statement is as follows:
    Select Top 100 percent *
    From (select shengname, ID as Sid, 1 as ORD
    From szsheng
    Union
    Select shiname, shengid, 2 as ORD
    From szshi)
    Order by SID, ORD

    The following table structure and expected results

     

    Table 1
    1 Beijing
    2 Shanghai
    3 Guangdong

    Table 2
    1 Beijing zone 1
    2 Beijing Zone 2
    3 Shanghai zone 1
    4 Shanghai Zone 2
    5 Guangzhou
    6 Shenzhen

    Then you want the query result to be as follows:
    Beijing
    Beijing zone 1
    Beijing Region 2
    Shanghai
    Shanghai zone 1
    Shanghai Region 2
    Guangdong
    Guangzhou
    Shenzhen

  • 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.