我尝试此查询操作数的数据类型为varchar是和运营商
Select
S.Name,S.No,
SUM(Case when s.Model='Cultus' then total else 0 end) as Cultus,
SUM(Case when s.Model ='vigo' then total else 0 end) as vigo,
SUM(total) total_v ,
s.MA,MAX(S.Speed) Speed
from (
Select
RVU.Name,RVU.No,VV.Model,count(VV.Model) as total, RVU.MA as MA,RVU.Speed
from
VV
inner join RVU
on VV.MID=RVU.ID
inner join RU on RVU.RID= RU.RID
WHERE
RU.StartDate>= '2016-04-01 00:00:00' and
RU.EndDate<='2016-04-30 23:59:59' and
RU.Region= 'Paris'
and RVU.No= '651' AND Model <> ''
Group By RVU.Name,RVU.RegNo,VV.Model,RVU.MA,RVU.Speed) S
GROUP BY
s.RegNo,s.Name,S.MA
输出继电器
Name No Cultus vigo total_v MA Speed
David 651 2 0 2 1048 124
David 651 3 0 3 597 345
David 651 1 0 1 606 101
David 651 3 2 5 992 110
无效的,当我尝试这个
Sum(MA) MA,
这个节目ERROR
Ø对于sum运算符,perand数据类型varchar无效。
我想造成这样的
Name No Cultus vigo total_v MA Speed
David 651 9 2 11 1799 345
UPDATE
当我尝试这个
SUM(CASE ISNULL(MA,'') WHEN '' THEN 0 ELSE CAST(MA AS INT) END),
而且这个
然后输出
Name No Cultus vigo total_v MA Speed
David 651 2 0 2 4192 124
David 651 3 0 3 2388 345
David 651 1 0 1 2424 101
David 651 3 2 5 4960 110
更新2:
当我用同样的数据
Select
RVU.Name,RVU.No,VV.Model,count(VV.Model) as total, RVU.MA as MA,RVU.Speed
from
VV
inner join RVU
on VV.MID=RVU.ID
inner join RU on RVU.RID= RU.RID
WHERE
RU.StartDate>= '2016-04-01 00:00:00' and
RU.EndDate<='2016-04-30 23:59:59' and
RU.Region= 'Paris'
and RVU.No= '651' AND Model <> ''
Group By RVU.Name,RVU.RegNo,VV.Model,RVU.MA,RVU.Speed
貌似值只执行这部分本次展会的多个记录完全改变
是您的MA列是一个VARCHAR数据类型? –
@JimMacaulay是 – user6628729
在这种情况下,您无法对列进行聚合,只对整数列执行聚合。我会告诉你如何聚合字符串列 –