SQL查询中的求和

时间:2016-08-15 12:05:55

标签: sql sql-server sum

我尝试此查询以获取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个

4 个答案:

答案 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,也不要将DISTINCTGROUP 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组成;