0

我正在编写一个程序来检查数据库中 15 个信用卡读卡器的交易。该计划的重点是检查阅读器在预定义日期范围内的销售情况,以确保我们没有遇到机械问题。一般而言,如果阅读器未在预定义的日期范围内记录销售,则应检查阅读器的操作。问题是,由于我正在“计算”交易数量,它会得出“0”而不是 NULL,如果没有任何交易,我只想收到一封电子邮件......我该如何更改如果是这种情况,从“0”计数到 NULL?我的查询如下:

SELECT        COUNT(sTerminal) AS Terminal
FROM            CC
WHERE        (dtCreated BETWEEN @startdate AND @enddate) AND (sTerminal = 'Swiper 1')
4

3 回答 3

6

如果您希望计数为 NULL,您可以这样做:

SELECT        NULLIF(COUNT(sTerminal), 0) AS Terminal
FROM            CC
WHERE        (dtCreated BETWEEN @startdate AND @enddate) AND (sTerminal = 'Swiper 1')

如果您根本不想返回任何东西,这可能更有用,您可以执行以下操作HAVING

SELECT        COUNT(sTerminal) AS Terminal
FROM            CC
WHERE        (dtCreated BETWEEN @startdate AND @enddate) AND (sTerminal = 'Swiper 1')
HAVING COUNT(sTerminal) > 0
于 2013-03-13T01:13:21.970 回答
3
 select CASE WHEN COUNT(sTerminal) = 0 THEN NULL ELSE COUNT(sTerminal) END AS Terminal
 from ....
 where ....
于 2013-03-13T01:11:15.487 回答
2

为什么不在 C# 中处理这个逻辑:

var numberOfSales = GetNumberOfSales(); // Your sql will execute

if (numberOfSales > 0)
{
    SendEmails();
}
于 2013-03-13T01:12:20.620 回答