oracle SQL 如何显示group by 聚合结果count 为0的查询
2012-11-29 16:43
656 查看
How to display 0 in conditional [group by] report in Count() SQL Query
![](http://lichao.net/images/blogs/eblog/Howtodisplay0inconditionalgroupbyreporti_9606/sqlinjectiondatabase_thumb.jpg)
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.
Region_ID | Region_Name |
1 | East |
2 | West |
3 | North |
4 | South |
Sales_ID | City | Region_id | 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_ID, Count(Amount) AS Total
From Sales
Where Amount > 1000
Group By Region_ID
Region_ID | 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 Jointotal 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, in Microsoft SQL it is IsNull, 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_ID, Count(Sales.Amount) As Total
From Sales
Where Amount > 1000
Group By Region_ID) SubTotal
On Region.Region_id = SubTotal.Region_ID
With the SQL query above, we have the following result:
Region_Name | SalesTotal |
East | 1 |
West | 1 |
North | 1 |
South | 0 |
转自 http://lichao.net/eblog/how-to-display-0-in-conditional-group-by-report-in-count-sql-query-200808164.html
相关文章推荐
- 含有GROUP BY子句的查询中如何显示COUNT()为0的结果
- 含有GROUP BY子句的查询中如何显示COUNT()为0的结果( SELECT语句完整的执行顺序)
- 【SQL心得】:在含有GROUP BY的SELECT语句中如何显示COUNT()为0的结果
- MySql含有GROUP BY子句的查询中如何显示COUNT()为0的结果
- 含有GROUP BY子句的查询中如何显示COUNT()为0的结果
- 【SQL】在含有GROUP BY的SELECT语句中如何显示COUNT()为0的结果
- 如何使SQLPlus的查询结果排列整齐(Oracle 10g)
- 在使用Hibernate时,因为一个查询需要更多的表连接而要使用SQL来解决性能问题。然而返回的结果集中包含了没有映射的Entity类中的表字段,在这个SQL中还有使用如何将层次关系的父子结点显示为横行
- sql查询显示count结果为0的数据
- 含有GROUPBY子句的查询中如何显示COUNT()为0的结果
- Oracle--sqlplus如何设置SQLPlus结果显示的宽度
- SQL中如何显示COUNT结果为0的行
- 含有GROUP BY子句的查询中如何显示COUNT()为0的成果(分享)
- 数据库_MySQL_SQL语句的组装顺序 和 GROUP BY的SELECT语句中显示COUNT()为0的结果
- SQL中如何将表的查询结果纵向显示(个人思路总结)
- Sql分组查询,如何把纵向显示的结果变成横向显示
- 如何在sql/plus中把select语句查询显示出来的结果中的显示字段长度缩小
- oracle 下 时间戳 timeStamp 如何将查询结果显示为普通日期类型
- Oracle--sqlplus如何设置SQLPlus结果显示的宽度
- SQL心得:在含有GROUP BY的SELECT语句中显示COUNT()为0的结果