oracle SQL 如何显示group by 聚合结果count 为0的查询

How to display 0 in conditional [group by] report in Count() SQL Query

sql-injection-databaseChallenge:
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_IDRegion_Name
1East
2West
3North
4South

Then we have the Sales table now

Sales_IDCityRegion_idAmount
1Atlanta15000
2Miami4200
3New York32000
4Los Angeles21500
5San Francisco2700

Now, if we just use a simple Count() query, we will have the following result:

SELECT Region_ID, Count(Amount) AS Total
From Sales
Where Amount > 1000
Group By Region_ID

Region_IDTotal
11
21
31

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 as the sales total.

What I need to take care is two details.

  1. 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.
  2. 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_NameSalesTotal
East1
West1
North1
South0

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.

 转自 http://lichao.net/eblog/how-to-display-0-in-conditional-group-by-report-in-count-sql-query-200808164.html

转载于:https://www.cnblogs.com/younes/archive/2012/11/29/2794933.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值