我正在使用以下带有SQL的SUM查询
SELECT SUM(cost) as total FROM sales where passport =
如何从计算中排除某些记录,例如pay = 1?
答案 0 :(得分:1)
你的意思是这样吗?
SELECT SUM(cost) as total FROM sales where (passport = $myPassport AND paid <> 1)
答案 1 :(得分:1)
简单地
SELECT SUM(cost) as total
FROM sales
WHERE passport = ?
AND paid <> 1
您可能也在这里寻找一个小组
SELECT passport, SUM(cost) as total
FROM sales
WHERE passport = ?
AND paid <> 1
GROUP BY passport
答案 2 :(得分:0)
SELECT SUM(cost) as total FROM sales where passport = "12345" and paid <> 1
答案 3 :(得分:0)
SELECT SUM(cost) as total
FROM sales
WHERE passport = 12345
AND paid <> 1
答案 4 :(得分:0)
你的意思是只得到总数等于1吗?
使用:HAVING total = 1
答案 5 :(得分:0)
有时使用这样的IF语句很有用:
CREATE PROCEDURE [dbo].[ManipulateValues]
-- Add the parameters for the stored procedure here
@Parameter float
AS
BEGIN
DECLARE @break_var as int
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
set @break_var = 0
IF NOT(ISNUMERIC(@Parameter)=1)
BEGIN
SET @Parameter = NULL
set @break_var = 1
END
IF @Parameter < 0.03 AND @break_var = 0
BEGIN
SET @Parameter = 0.029
set @break_var = 1
END
IF @Parameter > 0.03 AND @break_var = 0
BEGIN
SET @Parameter = 0.031
set @break_var = 1
END
IF @Parameter < -0.03 AND @break_var = 0
BEGIN
SET @Parameter = -0.031
set @break_var = 1
END
IF @Parameter = 0.12 AND @break_var = 0
BEGIN
SET @Parameter = 0.119
set @break_var = 1
END
IF @Parameter < 1 AND @break_var = 0
BEGIN
SET @Parameter = 0.99
set @break_var = 1
END
SELECT @Parameter
END
以这种方式,当状态字段等于1时,成本将乘以0并从SUM中排除,但不会排除计数函数的行。
另一种方法是在SUM中使用select,但它不会那么有用。