2016-09-16 78 views
1

我嘗試此查詢操作數的數據類型爲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 

貌似值只執行這部分本次展會的多個記錄完全改變

+0

是您的MA列是一個VARCHAR數據類型? –

+0

@JimMacaulay是 – user6628729

+0

在這種情況下,您無法對列進行聚合,只對整數列執行聚合。我會告訴你如何聚合字符串列 –

回答

0

似乎你的一個MA列值是varchar的類型,請檢查你的數據

如果你的數據類型爲varchar然後強制轉換爲int

SUM(CASE ISNULL(MA,'') WHEN '' THEN 0 ELSE CAST(MA AS INT) END) 

DECLARE @tblTest as Table(
    Name VARCHAR(10), 
    No INT, 
    Cultus INT, 
    vigo INT, 
    total_v INt, 
    MA VARCHAR(5), 
    Speed INT 
) 

INSERT INTO @tblTest VALUES 
('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) 

SELECT 
    Name,[No],SUM(Cultus), 
    SUM(vigo) AS vigo ,SUM(total_v) AS total_v, 
    SUM(CASE ISNULL(MA,'') WHEN '' THEN 0 ELSE CAST(MA AS INT) END) AS MA, 
    MAX(Speed) AS Speed 
FROM @tblTest 
GROUP BY Name,[No] 
+0

我檢查沒有varchar值 – user6628729

+0

什麼是您的列的數據類型?它不能是字符串類型/ varchar/nvarchar –

+0

數據類型是varchar – user6628729

0
create table #t 
(
id varchar(20)) 



insert into #t 
select '2' 
union all 
select '3' 

select sum(id) from #t--this fails 

你需要做的

select sum(cast (id as int))--cast from 2008 onwards 
from #t 

在你的情況MA列的類型爲VARCHAR

+0

當我嘗試你的解決方案MA值的變化。例如103更改爲其他值爲別人打開 – user6628729

+0

我不明白你的意思,請更新問題 – TheGameiswar

+0

檢查更新問題 – user6628729

0

列RVU.MA中可能有字符串值。試着用下面的查詢來找出這樣的錯誤。

SELECT * 
    FROM RVU 
    WHERE ISNUMERIC (RVU.MA)!=1 
0


你可以使用下面的語法來得到你的答案。

select sum(cast(columnname as int)) from TableName 

嘗試應用這種語法,

select sum(cast(s.MA as int)) 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 

檢查這個查詢,你是否能選擇你的記錄,並在查詢中應用此邏輯。

您的最終查詢的樣子,

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 , 
sum(cast(s.MA as int)),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 
+0

此次顯示錯誤結果 – user6628729

+0

檢查updated2問題 – user6628729

相關問題