尝试在INNER JOIN上使用带有DISTINCT的Count(*)

时间:2014-10-13 10:07:36

标签: sql sql-server

寻求一些帮助,我基本上只需要从以下查询中返回一个计数:

USE MediaProcessingQueue;

SELECT DISTINCT dbo.MessageQueue.messageId,dbo.MessageQueue.state,dbo.MessageQueue.wsaMessageId,dbo.MessageQueue.refId,dbo.StatusUpdate.messageId
FROM dbo.MessageQueue
INNER JOIN dbo.StatusUpdate
ON dbo.MessageQueue.messageId=dbo.StatusUpdate.messageId
WHERE dbo.MessageQueue.state ='1' AND dbo.StatusUpdate.percentComplete > 12

任何帮助都非常感激。

4 个答案:

答案 0 :(得分:0)

Count放在DISTINCT列附近。

Use MediaProcessingQueue
GO
select Count(DISTINCT dbo.MessageQueue.messageId,dbo.MessageQueue.state,dbo.MessageQueue.wsaMessageId,dbo.MessageQueue.refId,dbo.StatusUpdate.messageId)
FROM dbo.MessageQueue
INNER JOIN dbo.StatusUpdate
ON dbo.MessageQueue.messageId=dbo.StatusUpdate.messageId
WHERE dbo.MessageQueue.state ='1' AND dbo.StatusUpdate.percentComplete > 12

答案 1 :(得分:0)

SELECT Count(*)
FROM   (
        <insert your existing query here>
       ) As a_subquery

答案 2 :(得分:0)

如果你想获得只计数使用这个

Select Count(DISTINCT dbo.MessageQueue.messageId,dbo.MessageQueue.state,dbo.MessageQueue.wsaMessageId,dbo.MessageQueue.refId,dbo.StatusUpdate.messageId)

如果您想获得计数以及查询输出,请使用此

select @@ROWCOUNT 

选择查询后的下一行。

答案 3 :(得分:0)

你也可以这样做:

带有Count(*)

Sum(1)Group By

USE MediaProcessingQueue;

SELECT Sum(1)
  FROM dbo.MessageQueue
    INNER JOIN dbo.StatusUpdate
      ON dbo.MessageQueue.messageId=dbo.StatusUpdate.messageId
  WHERE dbo.MessageQueue.state ='1' AND dbo.StatusUpdate.percentComplete > 12
  GROUP BY dbo.MessageQueue.messageId, dbo.MessageQueue.state, dbo.MessageQueue.wsaMessageId, dbo.MessageQueue.refId, dbo.StatusUpdate.messageId