简体   繁体   中英

Getting row count with other columns

I need to get some columns which are LinkID , ReplyCount and the most important one is TotalRowCount .

This is my code:

SELECT 
   TOP(10) link.LinkID, mesaj.ReplyCount
FROM 
   TBL_UserIcerikler AS link 
INNER JOIN 
   TBL_UserMesajlar AS mesaj  ON link.FromUserID = mesaj.UserID
WHERE 
   link.PublishDate >='2013-03-12 19:46:45.000' 
ORDER BY
   link.PublishDate DESC

It is not running anymore when I add Count(*) AS a ".

I get this message instead. How can I get row count? Does anyone have any information about this topic?

Msg 208, Level 16, State 1, Line 1
Invalid object name 'TBL_UserIcerikler'

Count(*) is an aggregate function which returns the number of rows which have been summarised (not the number of rows returned by the query), so you must GROUP BY something and specify only the fields by which you group (or just return COUNT(*)).

It doesn't make a lot of sense to mix COUNT() and TOP().

For example :

SELECT link.LinkID, mesaj.ReplyCount, COUNT(*)
  FROM TBL_UserIcerikler AS link 
  INNER JOIN TBL_UserMesajlar AS mesaj ON link.FromUserID = mesaj.UserID
  WHERE link.PublishDate >='2013-03-12 19:46:45.000'
  GROUP BY link.LinkID, mesaj.ReplyCount;

I know it's not quite what you want, but you haven't given quite enough explanation as to what you want to get out of your database.

That said, I think you might have forgotten a comma in the expression list.

Why not post your modified query.

请阅读 MSDN的分组依据说明,您将了解为什么需要它来获取总计数。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM