我嘗試此查詢操作數的數據類型爲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
在這種情況下,您無法對列進行聚合,只對整數列執行聚合。我會告訴你如何聚合字符串列 –