Sql server 左外联接操作后如何选择不同的值

Sql server 左外联接操作后如何选择不同的值,sql-server,Sql Server,我想从三个具有聚合函数但其中一列中没有重复项的表中选择一些值,例如: select t3.ValueDesc as FeatureType, count(t2.Strategic) as TotalCount ,t2.RequestID,t1.StoryID --these are not needed, but put for better vision from tblRequests t2 left outer join (select *

我想从三个具有聚合函数但其中一列中没有重复项的表中选择一些值,例如:

select t3.ValueDesc as FeatureType,  
        count(t2.Strategic) as TotalCount
        ,t2.RequestID,t1.StoryID    --these are not needed, but put for better vision

from  tblRequests t2  
left outer join (select * from tblAgileMultiDD where Type=18) t3
    on t3.FormulaValue = t2.Strategic 
left outer join tblAgileStory t1 
    on t1.Feature = t2.RequestID  

where t2.RequestID > 0  
and t1.DemoStatus = 1  

group by t3.ValueDesc
     ,t2.RequestID, t1.StoryID    --these are not needed but put for better vision
order by t3.ValueDesc
然后它返回给我这样的东西:

FeatureType     TotalCount  RequestID   StoryID
Protect Base    1           311         1629
Protect Base    1           311         1630
Protect Base    1           312         1631
Protect Base    1           312         1637
New Market      1           313         1640
New Market      1           313         1645
FeatureType     TotalCount
Protect Base    2          (for RequestID = 311, 312)
New Market      1          (for RequestID = 313)
如果我用“t2.RequestID,t1.StoryID”注释掉行,它会给出结果:

FeatureType     TotalCount
Protect Base    4         
New Market      2
因此,对于RequestID和StoryID的每个唯一组合,它都会返回新行。如何使其仅为每个唯一的RequestID返回新行,而不考虑StoryID? 因此,我希望此查询的结果如下:

FeatureType     TotalCount  RequestID   StoryID
Protect Base    1           311         1629
Protect Base    1           311         1630
Protect Base    1           312         1631
Protect Base    1           312         1637
New Market      1           313         1640
New Market      1           313         1645
FeatureType     TotalCount
Protect Base    2          (for RequestID = 311, 312)
New Market      1          (for RequestID = 313)
在开头加上“独特”一词对它没有效果。 你能帮我吗

select distinct FeatureType,TotalCount from (
    select t3.ValueDesc as FeatureType,  
            count(t2.Strategic) as TotalCount
            ,t2.RequestID
            -- ,t1.StoryID    --these are not needed, but put for better vision

    from  tblRequests t2  
    left outer join (select * from tblAgileMultiDD where Type=18) t3
        on t3.FormulaValue = t2.Strategic 
    left outer join tblAgileStory t1 
        on t1.Feature = t2.RequestID  

    where t2.RequestID > 0  
    and t1.DemoStatus = 1  

    group by t3.ValueDesc
         ,t2.RequestID
          -- , t1.StoryID    --these are not needed but put for better vision
) as T
按t3.ValueDesc订购


你能试试这个吗。

count(Distinct t2.Strategic)呢,或者更确切地说,count Distinct基于请求ID我想你已经找到了一个解决方案:从
SELECT
groupby
子句中删除
t2.RequestID
t1.StoryID