sql - Sql Server CASE 计数/不计数
问题描述
我正在尝试选择已通过我们数据库的订单数量。
从订单中,我要检索 3 个部分
- 总订单数
- 可销售订单的订单数量
- 仅包含免费样品的订单数量
如果订单仅包含免费样品,则该订单的总价值自然为0
,如果是正常订单,则总价值自然为>0
总字段为十进制格式。
这就是我目前正在尝试做的事情。
COUNT(gross) as 'TotalOrders',
COUNT(case when gross = 0.00 THEN null else gross end) as 'OrderCount',
COUNT(case when gross > 0.00 THEN gross else null end) as 'Samples',
如果订单具有正值,我如何使COUNT()
函数仅增加 1 gross
?
我正在这样做SQLServer
我的结果显示TotalOrders
is 1
but OrderCount
并且Samples
正在显示为0
谢谢。
解决方案
这是你想要的吗?
sum(case when gross >= 0.00 then 1 else 0 end) as TotalOrders,
sum(case when gross = 0.00 then 1 else 0 end) as OrderCount,
sum(case when gross > 0.00 then 1 else 0 end) as Samples
你可能会想要> 0.00
,但那TotalOrders
会是一样的Samples
。
推荐阅读
- ios - 找不到“ReactNativeNavigation/ReactNativeNavigation.h”文件
- r - 为什么 R raster::writeRaster() 生成的图片在 Win10 中无法显示?
- nginx - 静态编译的js的prerender只得到
- android - Android - Play 商店 - 更改订阅的到期日期
- ruby-on-rails - 参数问题
- nsis - nsis 从 txt 文件中读取目录?
- java - 如何使用 ", /*, \* 设置 java 分隔符
- mysql - 是否可以选择具有不同数量属性的多个表?
- python - 正则表达式匹配月份名称日,年
- python - Python 3.6 - 在 soup.findAll() 字符串中查找单词