我尝试此查询以获取VName总和。在表VName数据类型是varchar
Select distinct tblRV.ID as ID,
tblRV.OName,
tblRV.RegNo,
tblvv.VName,
count( tblvv.VName) as total,
tblRV.MA,
tblRV.MSpeed
from tblRe
inner join tblRV
On tblReG.RID = tblRV.RID
inner join tblvv
on tblRV.ID=tblVV.MID
WHERE
tblRe.StartDate >= '2016-07-01 00:00:00.000' AND
tblRe.EndDate <= '2016-07-31 23:59:59.000' and
tblRe.Region = ‘UK’ and
VName <> ''
group by
tblRV.ID ,
tblRV.OName,
tblRV .RegNo,
tblRV.MA,
tblRV.MSpeed,
tblRV.VName
order by
tblRV.OName,
tblRV.ID
当我在上面尝试查询时,显示结果如此
ID OName RegNo VName total MA MSpeed
26626 john BE MV 3 754 130
26765 john BE MV 3 90 140
24569 john BE DDSB 6 4 19
27569 john BE MV 2 62 129
21231 john BE MV 3 66 136
现在我想省略VName并想要MA和MSpeed的所有VName和MAX值的总和
即
ID OName RegNo total MA MSpeed
26626 john BE 17 754 136
我在这个查询中尝试@The Shooter查询查询我只添加top(1)所以当我添加这个节目总计3而我想要总共17
SELECT
S.ID
,S.OName
,S.RegNo
,SUM(Total) vNameSum
,MAX(S.MA) MaxMA
,MAX(S.MSpeed) MaxMSpeed
FROM
(
SELECT top (1)
tblRV.ID as ID,
tblRV.OName,
tblRV.RegNo,
tblvv.VName,
COUNT(tblvv.VName) as total,
tblRV.MA,
tblRV.MSpeed
FROM tblRe
INNER JOIN tblRV
ON tblReG.RID = tblRV.RID
INNER JOIN tblvv
ON tblRV.ID=tblVV.MID
WHERE
tblRe.StartDate >= '2016-07-01 00:00:00.000'
AND tblRe.EndDate <= '2016-07-31 23:59:59.000'
AND tblRe.Region = ‘UK’
AND VName <> ''
GROUP BY
tblRV.ID
,tblRV.OName
,tblRV.RegNo
,tblRV.MA
,tblRV.MSpeed
,tblRV.VName
ORDER BY
tblRV.OName
,tblRV.ID
) S
GROUP BY
S.ID
,S.OName
,S.RegNo
以上查询显示结果如此
ID OName RegNo VName total MA MSpeed
26626 john BE MV 3 754 130
我希望总共17个而不是3个
答案 0 :(得分:1)
您可能打算COUNT()
或COUNT(DISTINCT)
而不是SUM()
。您已经拥有GROUP BY
,因此请勿使用SELECT DISTINCT
:
Select tblRV.ID, tblRV.oName, tblRV.RegNo,
count(distinct tblvv.VName) as total, -- number of different names
max(tblRV.Speed),
-------^ Your comment suggests that this should be `SUM()`
tblRV.MA
from tblRe inner join
tblRV
On tblRe.RID = tblRV.RID inner join
tblvv
on tblRV.ID = tblVV.MID
where tblRegion_Uni.StartDate >= '2016-07-01' AND
tblRegion_Uni.EndDate < '2016-07-29' and
tblRegion_Uni.Region = 'uk' and
VName <> ''
group by tblRV.ID, tblRV.OName, tblRV.RegNo, tblRV.MA
order by tblRV.OName, tblRV.ID
答案 1 :(得分:1)
试试这个(我刚修改了你的查询):
SELECT
S.OName
,S.RegNo
,SUM(Total) vNameSum
,MAX(S.MA) MaxMA
,MAX(S.MSpeed) MaxMSpeed
FROM
(
SELECT
tblRV.OName,
tblRV.RegNo,
tblvv.VName,
COUNT(tblvv.VName) as total,
tblRV.MA,
tblRV.MSpeed
FROM tblRe
INNER JOIN tblRV
ON tblReG.RID = tblRV.RID
INNER JOIN tblvv
ON tblRV.ID=tblVV.MID
WHERE
tblRe.StartDate >= '2016-07-01 00:00:00.000'
AND tblRe.EndDate <= '2016-07-31 23:59:59.000'
AND tblRe.Region = ‘UK’
AND VName <> ''
GROUP BY
tblRV.OName
,tblRV.RegNo
,tblRV.MA
,tblRV.MSpeed
,tblRV.VName
) S
GROUP BY
S.OName
,S.RegNo
ORDER BY
tblRV.OName
答案 2 :(得分:0)
我认为您希望COUNT
不是SUM
,也不要将DISTINCT
与GROUP BY
一起使用。
旁注:tblRegion_Uni
不属于FROM
部分?
附注2:AND tblvv.VName <> ''
可AND tblvv.VName IS NOT NULL
SELECT
tblRV.ID AS ID,
tblRV.oName,
tblRV.RegNo,
COUNT(tblvv.VName) AS total,
MAX(tblRV.Speed),
tblRV.MA
FROM tblRe
INNER JOIN tblRV ON tblRe.RID = tblRV.RID
INNER JOIN tblvv ON tblRV.ID = tblVV.MID
WHERE tblRegion_Uni.StartDate >= '2016-07-01 00:00:00.000'
AND tblRegion_Uni.EndDate <= '2016-07-28 23:59:59.000'
AND tblRegion_Uni.Region = 'uk'
AND tblvv.VName <> ''
GROUP BY tblRV.ID, tblRV.OName, tblRV.RegNo, tblRV.MA
ORDER BY tblRV.OName, tblRV.ID
答案 3 :(得分:0)
选择ID,OName,RegNo,count(vname)为total,Max(MA)为MA,Max(MSpeed)为Mspeed 来自table_name 由OName组成;