SQL Server merges two tables queried by two SQL query statements into a single table

Source: Internet
Author: User
Tags getdate

First SQL statement

Select CompanyName GSMC,ZB Zhibiao  from  Left Join T_companycode  on T_GSNDZB.GSBH=T_companycode.companyid

Query Result:

A second SQL statement

SELECT min(CompanyName) GSMC,cast(round(sum(T_xstj.hsje)/10000,2) asNumeric -,2)) Ndje fromT_xstj Left JoinT_companycode onT_companycode.companyid=T_XSTJ.GSBHwhere DateDiff( YearSjgetdate())=0 Group  byGsbh

Query Result:

Statements that are merged into a single table:

Select IsNull(A.GSMC,B.GSMC) GSMC,IsNull(A.zhibiao,0) Zhibiao,IsNull(B.ndje,0) Ndje from (      --results of the second table query--      SelectCompanyName GSMC,ZB Zhibiao fromT_gsndzb Left JoinT_companycode onT_gsndzb.gsbh=T_companycode.companyid) A Full JOIN (          --results of the second table query--             SELECT min(CompanyName) GSMC,cast(round(sum(T_xstj.hsje)/10000,2) asNumeric -,2)) Ndje fromT_xstj Left JoinT_companycode onT_companycode.companyid=T_XSTJ.GSBHwhere DateDiff( YearSjgetdate())=0 Group  byGSBH) b onA.gsmc=B.gsmc

Results:

SQL Server merges two tables queried by two SQL query statements into a single table

Related Article

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.