GROUP BY子句 - 结果太多

时间:2012-03-22 13:11:33

标签: sql-server group-by

我在MSSQL中遇到GROUP BY子句的问题。

我正在尝试仅使用最新的idNotation(最大发布时间)获得结果,但它显示带有dublicated idNotation的结果......

这是我的SQL

SELECT idAnalysis, idNotation, max(PublishedTime) AS PublishedTime, Created, 
       T2.Link, T3.Name AS Rec, V.NAME_SECURITY, V.PRICE
FROM Analysis T1
INNER JOIN Templates T2 ON T1.idTemplates = T2.idTemplates 
LEFT JOIN Recommendations T3 ON T1.idRecommendation = T3.idRecommendations
INNER JOIN RFD.dbo.vLiveQuotes V ON T1.idNotation = V.ID_NOTATION
WHERE PublishedTime is not null
      AND
      T2.idTemplates = 4
GROUP BY idNotation, idAnalysis, [Level], Goal, StopLoss, Header, [Resume],
         T2.Name, Created, T2.Link, T3.Name, V.NAME_SECURITY, V.PRICE
ORDER BY PublishedTime DESC

结果:

25  7239884 Akkumuler   DANSKE BANK A/S DKK10   93
24  7239884 Sælg    DANSKE BANK A/S DKK10   934.500
22  7490572 Sælg    A.P. MOLLER - MAERSK SER`B`DKK1000  43
23  7239884 Hold    DANSKE BANK A/S DKK10   93
18  2027313 Køb GENMAB AS DKK1 (BEARER) 41

但结果应该是:

25  7239884 Akkumuler   DANSKE BANK A/S DKK10   93
22  7490572 Sælg    A.P. MOLLER - MAERSK SER`B`DKK1000  43
18  2027313 Køb GENMAB AS DKK1 (BEARER) 41

2 个答案:

答案 0 :(得分:2)

您正在Level, Goal, StopLoss, Header, Resume, T2.Name进行分组,但这些列不在选择列表中。从group by

中删除它们

答案 1 :(得分:1)

如果我正确地读取了您的查询,您需要来自T1,T2和T3的数据,并且只需要来自V的最后一个出版物。为了缩短内容,T1,T2和T3将被视为单个表T.几个选择。检查它们并使用在您的情况下运行速度最快的那个。

使用row_number

select *
from T
inner join 
(
   select ID_NOTATION, V.NAME_SECURITY, V.PRICE, V.PublishedTime
          row_number() over (partition by ID_NOTATION 
                             order by PublishedTime desc) AS RowNumber
     from RFD.dbo.vLiveQuotes V
) V
  ON T.idNotation = V.ID_NOTATION
  and V.RowNumber = 1

使用apply

select *
from T
cross apply
(
   select TOP 1 *
     from RFD.dbo.vLiveQuotes V
    where V.idNotation = T.ID_NOTATION
    order by PublishedTime desc
) V

如果您只限于较旧的Sql Server,您可以尝试这样做:

select *
from T
inner join 
(
   select * 
     from RFD.dbo.vLiveQuotes V
     inner join
     (
         select ID_NOTATION, max(V.PublishedTime) PublishedTIme
           from RFD.dbo.vLiveQuotes
          group by ID_NOTATION
     ) lastPublishedTime
       on V.ID_NOTATION = lastPublishedTime.ID_NOTATION
       and V.PublishedTime = lastPublishedTime.PublishedTIme
) V
  ON T.idNotation = V.ID_NOTATION

如果您希望V中没有匹配的行,则内连接可能是左连接并且交叉应用外应用。

更新:了解到PusblishedTime生活在分析中。

最简单的方法是删除max / group by,添加idNotations的派生表和max(PublishedTime)并加入回Analysis:

SELECT idAnalysis, t1.idNotation, T1.PublishedTime AS PublishedTime, Created, 
       T2.Link, T3.Name AS Rec, V.NAME_SECURITY, V.PRICE
FROM Analysis T1
INNER JOIN Templates T2 ON T1.idTemplates = T2.idTemplates 
LEFT JOIN Recommendations T3 ON T1.idRecommendation = T3.idRecommendations
INNER JOIN RFD.dbo.vLiveQuotes V ON T1.idNotation = V.ID_NOTATION
inner join
(
  select idNotation, max(V.PublishedTime) PublishedTIme
    from Analysis
   group by idNotation
) lastPublishedTime
  on T1.idNotation = lastPublishedTime.idNotation
  and t1.PublishedTime = lastPublishedTime.PublishedTime
WHERE PublishedTime is not null
      AND
      T2.idTemplates = 4
ORDER BY PublishedTime DESC

您可以使用任何其他选项,但需要重新编写查询。