study how to express some SQL query in english------OUT JOIN
2008-08-25 10:07
543 查看
Challenge:
Let me assume this: we have two tables, one is Regions table with all region information (East, West, etc. ) and we have another table with Sales information. Now, we need to display a report with region information and how many sales conducted in each region with greater than $1000 value in each sale. It sounds simple at the beginning since a Count(*) with Group By can do this trick.
But what if I need to still show the region which does not have any sales greater than $1000 on the report? Using Count(*) itself in the query, the region without any sales greater than $1000 will not show in the query result, period.
I tried to use some Count() tricks (like Count(NULL), COUNT(1), COUNT(0)) to accomplish this task, but found myself no luck at all. Till I decide to use a more complex SQL query to do a Outer Join with the Count() result query.
Here is what I worked out.
Solutions:
To help you understand my processes, let me draw these two tables with some dumb data.
The first table is the Region table as the below.
Then we have the Sales table now
Now, if we just use a simple Count() query, we will have the following result:
SELECT Region, Count(Amount) AS Total
From Sales
Where Amount > 1000
Group By Region
As you can tell from the above, the Region ID 4 - South did not show in the result since its sales amount is only $200. But I need to show this region too with 0 as the sales total.
What I need to take care is two details.
First, still use the above sql query as SubQuery to create the Count() sales total table (or view), then use the Region table to Outer Join total table to show region even if its sales amount did not meet the query conditions. In our example, I will use Left Join to do such thing.
Use logic function to show 0 if the value is Null from the outer join tables. In Oracle, it should be NVL, and in Microsoft Access, it will be NZ. For other databases, you can refer to their own manual for the references.
Finally, I will make the query above to the following considering the mentioned two details. This time, I also use Region Name for a good display.
SELECT Region.Region_Name, NVL(SubTotal.Total,0) As SalesTotal
From Region
Left Join
(SELECT Region, Count(Sales.Amount) As Total
From Sales
Where Amount > 1000
Group By Region) SubTotal
On Region.Region_id = SubTotal.Region
With the SQL query above, we have the following result:
Great! We had it! Of course, my initial challenge was NOT such dumb Sales and Region tables. I just used them for the simplicity purpose on my blog. But we still can get it with the SubQuery strategy above. So even you have more complex reports requirement, you still can use the above as your starting point.
Have fun on SQLing.
Let me assume this: we have two tables, one is Regions table with all region information (East, West, etc. ) and we have another table with Sales information. Now, we need to display a report with region information and how many sales conducted in each region with greater than $1000 value in each sale. It sounds simple at the beginning since a Count(*) with Group By can do this trick.
But what if I need to still show the region which does not have any sales greater than $1000 on the report? Using Count(*) itself in the query, the region without any sales greater than $1000 will not show in the query result, period.
I tried to use some Count() tricks (like Count(NULL), COUNT(1), COUNT(0)) to accomplish this task, but found myself no luck at all. Till I decide to use a more complex SQL query to do a Outer Join with the Count() result query.
Here is what I worked out.
Solutions:
To help you understand my processes, let me draw these two tables with some dumb data.
The first table is the Region table as the below.
Region_ID | Region_Name |
1 | East |
2 | West |
3 | North |
4 | South |
Sales_ID | City | Region | Amount |
1 | Atlanta | 1 | 5000 |
2 | Miami | 4 | 200 |
3 | New York | 3 | 2000 |
4 | Los Angeles | 2 | 1500 |
5 | San Francisco | 2 | 700 |
SELECT Region, Count(Amount) AS Total
From Sales
Where Amount > 1000
Group By Region
Region | Total |
1 | 1 |
2 | 1 |
3 | 1 |
What I need to take care is two details.
First, still use the above sql query as SubQuery to create the Count() sales total table (or view), then use the Region table to Outer Join total table to show region even if its sales amount did not meet the query conditions. In our example, I will use Left Join to do such thing.
Use logic function to show 0 if the value is Null from the outer join tables. In Oracle, it should be NVL, and in Microsoft Access, it will be NZ. For other databases, you can refer to their own manual for the references.
Finally, I will make the query above to the following considering the mentioned two details. This time, I also use Region Name for a good display.
SELECT Region.Region_Name, NVL(SubTotal.Total,0) As SalesTotal
From Region
Left Join
(SELECT Region, Count(Sales.Amount) As Total
From Sales
Where Amount > 1000
Group By Region) SubTotal
On Region.Region_id = SubTotal.Region
With the SQL query above, we have the following result:
Region_Name | SalesTotal |
East | 1 |
West | 1 |
North | 1 |
South | 0 |
Have fun on SQLing.
相关文章推荐
- How do I write a LINQ to Entities query which has the equivalent of the SQL “in” keyword?
- How to change the language of oracle sqlplus from chinese to english in windows(如何将oracle sql plus中的中文变成英文版)
- how to execute the sql query in scala
- 92 Examine the output of the query that you executed to list the objects in the recycle bin: SQL> SE
- How to Kill All Processes That Have Open Connection in a SQL Server Database[关闭数据库链接 最佳方法] -摘自网络
- How to grant access to SQL logins on a standby database when the guest user is disabled in SQL Serve
- How To Split column In SQL
- how to write order by and limit query in jpa [duplicate]
- How to Remove Table Partitioning in SQL Server
- Windows Phone 7.5 - Local SQL Database:How to read the DB schema in windows phone 7
- How to check backend SQL query from a OAF pages
- How to get details about patch applied in Oracle Applications by OAM & SQL?
- How to Switch the Driving Table in a Hash Join (文档 ID 171940.1)
- java.sql.SQLException: QueryRunner requires a DataSource to be invoked in this way, or a Connection
- How to run debug SQL with in front end(EBS)?
- How can I connect Unity to an SQL database in order to implement an MMO?
- How to Interpret the "SQL ordered by Physical Reads (UnOptimized)" Section in AWR (文档 ID 1466035.1)
- How to determine which checkboxes are checked in postback (sqlstr="delete spprod where id in ("&request("sid")&")")
- Save results to different files when executing multi SQL statements in DB Query Analyzer 7.01
- Some thoughts about how to learn english well