在这个选择中我需要和结果IIF表达式,但是当我执行这个查询时只获得第一个IIF语句。有什么建议??感谢
SELECT conto, desconto, date, codoperaio, SUM(IIF(totcasse ='1',SUM(totcasse),0)+
IIF(totcasse ='6',SUM(totcasse*3),0)+
IIF(totcasse ='8',SUM(totcasse*4),0)+
IIF(totcasse ='10',SUM(totcasse*5),0)) as Kilogrammi
FROM dbo.Import
where totcasse BETWEEN 1 and 10
Group by conto, desconto, date,codoperaio, totcasse
答案 0 :(得分:0)
如果没有错,你正在寻找这个
SELECT conto,
desconto,
date,
codoperaio,
Sum(CASE totcasse WHEN '1' THEN totcasse ELSE 0 END) +
Sum(CASE totcasse WHEN '6' THEN totcasse * 3 ELSE 0 END) +
Sum(CASE totcasse WHEN '8' THEN totcasse * 4 ELSE 0 END) +
Sum(CASE totcasse WHEN '10' THEN totcasse * 5 ELSE 0 END)
FROM dbo.Import
WHERE totcasse in (1,6,8,10)
GROUP BY conto,
desconto,
date,
codoperaio
或
SELECT conto,
desconto,
date,
codoperaio,
Sum(CASE totcasse
WHEN '1' THEN totcasse
WHEN '6' THEN totcasse * 3
WHEN '8' THEN totcasse * 4
WHEN '10' THEN totcasse * 5
END)
FROM dbo.Import
WHERE totcasse in (1,6,8,10)
GROUP BY conto,
desconto,
date,
codoperaio
答案 1 :(得分:0)
SELECT conto, desconto, date, codoperaio, IIF(totcasse ='1',SUM(totcasse),0)+
IIF(totcasse ='6',SUM(totcasse*3),0)+
IIF(totcasse ='8',SUM(totcasse*4),0)+
IIF(totcasse ='10',SUM(totcasse*5),0) as Kilogrammi
FROM dbo.Import
where totcasse BETWEEN 1 and 10
Group by conto, desconto, date,codoperaio, totcasse
答案 2 :(得分:0)
不要按照小组进行分组。 而且,在IIF之后不要使用SUM。我有一个较旧的SQL Server版本,所以我无法测试IIF,但我认为它应该是这样的:
SELECT conto,
desconto,
date,
codoperaio,
Sum(iif( totcasse = 1, totcasse,
iif( totcasse = 6, totcasse * 3,
iif( totcasse = 8, totcasse * 4,
iif( totcasse = 10, totcasse * 5, 0))))) as Kilogrammi
FROM dbo.Import
WHERE totcasse >= 1
AND totcasse <= 10
GROUP BY conto,
desconto,
date,
codoperaio
确保没有可能的值,例如3或9 totcasse。否则他们不会被计算出来。或者,用totcasse替换0。
答案 3 :(得分:0)
使用CASE
语句
SELECT conto, desconto, date, codoperaio,
SUM(CASE totcasse
WHEN '1' THEN totcasse
WHEN '6' THEN totcasse*3
WHEN '8' THEN totcasse*4
WHEN '10' THEN totcasse*5
ELSE 0 END
)as Kilogrammi
FROM dbo.Import
where totcasse BETWEEN 1 and 10
Group by conto, desconto, date,codoperaio, totcasse
答案 4 :(得分:0)
查询中有三个明显的问题。
SUM()
功能。 totcasse
中有group by
,但您似乎想将它们合并为一行。WHERE
子句将totcasse
视为数字而不是字符串。此外,case
是编写查询的更好方式,而不是iif()
。 (case
是ANSI标准SQL; iif()
在SQL Server中是为了向后兼容MS Access以及谁想要倒退?)
我猜这是您想要的查询查询:
SELECT conto, desconto, date, codoperaio,
SUM(CASE WHEN totcasse ='1' THEN totcasse
WHEN totcasse ='6' THEN totcasse * 3
WHEN totcasse ='8' THEN totcasse * 4
WHEN totcasse ='10' THEN totcasse * 5
ELSE 0
END) as Kilogrammi
FROM dbo.Import
where totcasse IN ('1', '2', '3', '4', '5', '6', '7', '8', '9', '10')
Group by conto, desconto, date, codoperaio;
请注意WHERE
子句。当您使用字符串时,表达式为:
where totcasse between '1' and '10'
基本上只返回以'10'
开头的值。不要在比较中混合类型。
答案 5 :(得分:0)
SELECT conto&#xA;,desconto&#xA;,date&#xA;,codoperaio&#xA;,SUM(CASE totcasse&#xA; WHEN 1 THEN totcasse&#xA; WHEN 6TEN totcasse * 3&#xA ;当8时那么* 4&#xA; 10时那么* 5&#xA; ELSE 0&#xA; END)AS Kilogrammi&#xA; FROM dbo.Import&#xA;其中包括1和10之间&#xA;按conto分组,desconto,date,codoperaio&#xA;
&#xA;