Multiple table links left join

Source: Internet
Author: User

An example I wrote: A multi-table connection, with a total of 3 tables connected. Using the aggregate function SUM, the GROUP by

[Userid]B.[Name],Sum(c.[Money]+C.[Bank])AsTotalmoney
FromTable1 A (NOLOCK)
LeftJOINTable2 B (NOLOCK)OnA.[Userid]=B.[Userid]
Left JOINTable3 C (NOLOCK)OnB.[Userid]=C.[Userid]

WHEREA.[Userid]=B.[Userid]andA.[Userid]=C.[Userid]andA.[Time]>=‘2005-01-01‘andA.[Time]<=‘2006-12-31 ' &NBSP;

group  by a.< Span style= "color: #ff0000;" >[userid],b.[name]

order by  A.[time] desc&NBSP;


Optimize for a bit

[Userid]B.[Name],Sum(c.[Money]+C.[Bank])AsTotalmoney
FromTable1 A (NOLOCK)
LeftJOINTable3 C (NOLOCK)OnA.[Userid]=C.[Userid], Table2 B (NOLOCK)

WHEREA.[Userid]=B.[Userid]andA.[Time]>=‘2005-01-01‘andA.[Time]<=‘2006-12-31 ' &NBSP;

group  by a.< Span style= "color: #ff0000;" >[userid],b.[name]

order by  A.[time] desc&NBSP;



================================================================================
Left Join Syntax:

*From
Table1LeftJoinTable2 on condition 1
Left join table3 on condition 2
Left join table4 on condition 3
where condition 4


GROUP by Description:

GROUP BY

You can use the GROUP BY clause in a SELECT statement to divide rows into smaller groups, and then use a clustered function to return summary information for each group, and you can use the HAVING clause to limit the result set that is returned. The GROUP BY clause groups query results and returns summary information for rows Oracle groups The query results by the value of the expression specified in the GROUP BY clause.

In a query statement with a GROUP BY clause, the column specified in the select list is either the column specified in the GROUP BY clause or contains a clustered function

Select Max (SAL), Job emp Group by job;
(Note Max (SAL), job job does not have to appear, but it is meaningful)

Select and group by of the query statement, the HAVING clause is the only place where the cluster function appears, and the clustering function cannot be used in the WHERE clause.

Select Deptno,sum (SAL) from EMP where sal>1200 GROUP by DEPTNO have sum (SAL) >8500 order by Deptno;

When the HAVING clause is used in the GROPU by clause, only the groups that satisfy the having condition are returned in the query results. There can be a WHERE clause and a HAVING clause in an SQL statement. Having is similar to the WHERE clause, which is used to set the qualification conditions

The purpose of the WHERE clause is to remove rows that do not conform to the where condition before grouping the results of the query, that is, to filter the data before grouping, the condition cannot contain a clustering function, and the Where condition is used to display a particular row.
The HAVING clause is used to filter the groups that meet the criteria, that is, to filter the data after grouping, often including clustering functions, to display specific groups using the having condition, or to group by using multiple grouping criteria.

Query the number of employees for each position in each department
Select Deptno,job,count (*) from the EMP group by Deptno,job;

Multiple table links left join

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.