向SQL Server查询添加count(*)

时间:2014-11-04 17:56:03

标签: sql sql-server database record-count

我正在尝试找到一种方法来向查询的输出添加计数:

SELECT *
FROM 
    (SELECT 
        id, 
        'Event Location' AS name, 
        venueName AS snippet, 
        venueLatLng AS coordinates, 
        (3959 
        * acos(cos(radians('xx.xxxxxx')) 
        * cos(radians(SUBSTRING(venueLatLng, 1, CHARINDEX(',', venueLatLng)-1))) 
        * cos(radians(SUBSTRING(venueLatLng, CHARINDEX(',', venueLatLng) + 1, 1000)) 
        - radians('-xx.xxxxxxx')) 
        + sin(radians('xx.xxxxxx')) 
        * sin(radians(SUBSTRING(venueLatLng, 1, CHARINDEX(',', venueLatLng)-1))))) AS distance 
     FROM marker) TMP 
WHERE 
    distance < 30 
ORDER BY 
    distance;

我尝试这样做的方式是:

SELECT *
FROM (
SELECT  id, 
        'Event Location' AS name, 
        venueName AS snippet, 
        venueLatLng AS coordinates, 
        COUNT(*) AS rCount, 
        (3959 
        * acos(cos(radians('xx.xxxxxx')) 
        * cos(radians(SUBSTRING(venueLatLng, 1, CHARINDEX(',', venueLatLng)-1))) 
        * cos(radians(SUBSTRING(venueLatLng, CHARINDEX(',', venueLatLng) + 1, 1000)) 
        - radians('-xx.xxxxxxx')) 
        + sin(radians('xx.xxxxxx')) 
        * sin(radians(SUBSTRING(venueLatLng, 1, CHARINDEX(',', venueLatLng)-1))))) AS distance 
FROM marker) TMP 
WHERE distance < 30 
ORDER BY distance;

错误是这样的:

  

Column&#39; marker.id&#39;在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。

我该如何正确地做到这一点?

6 个答案:

答案 0 :(得分:2)

使用COUNT的分析版本,并确保将其放在外部查询中,以便它尊重WHERE distance < 30

SELECT
  TMP.*,
  COUNT(*) OVER () AS RCount
FROM (
  SELECT  id, 
    'Event Location' AS name, 
    venueName AS snippet, 
    venueLatLng AS coordinates, 
    (3959 
    * acos(cos(radians('xx.xxxxxx')) 
    * cos(radians(SUBSTRING(venueLatLng, 1, CHARINDEX(',', venueLatLng)-1))) 
    * cos(radians(SUBSTRING(venueLatLng, CHARINDEX(',', venueLatLng) + 1, 1000)) 
    - radians('-xx.xxxxxxx')) 
    + sin(radians('xx.xxxxxx')) 
    * sin(radians(SUBSTRING(venueLatLng, 1, CHARINDEX(',', venueLatLng)-1))))) AS distance 
  FROM marker) TMP 
WHERE distance < 30 
ORDER BY distance;

答案 1 :(得分:1)

Count(*)将使您的查询成为聚合,因此,除非您使用group by,否则结果将是单行。错误消息表示您无法使用其他字段并仍然生成单行

答案 2 :(得分:0)

如果没有GROUP BY或OVER(PARTITION BY ...)

,则不能使用COUNT(*)

尝试将您的查询更改为

COUNT(*) OVER () AS rCount,

OR

COUNT(*) OVER (PARTITION BY ColumnName) AS rCount,

答案 3 :(得分:0)

如果您需要计算同一行出现的次数,只需添加一个group by子句,其中包含您需要选择的所有字段,所以:

SELECT *
FROM (
SELECT  id, 
    'Event Location' AS name, 
    venueName AS snippet, 
    venueLatLng AS coordinates, 
    COUNT(*) AS rCount, 
    (3959 
    * acos(cos(radians('xx.xxxxxx')) 
    * cos(radians(SUBSTRING(venueLatLng, 1, CHARINDEX(',', venueLatLng)-1))) 
    * cos(radians(SUBSTRING(venueLatLng, CHARINDEX(',', venueLatLng) + 1, 1000)) 
    - radians('-xx.xxxxxxx')) 
    + sin(radians('xx.xxxxxx')) 
    * sin(radians(SUBSTRING(venueLatLng, 1, CHARINDEX(',', venueLatLng)-1))))) AS distance 
FROM marker
group by id, 'Event Location', venueName, venueLatLng
) TMP 
WHERE distance < 30 
ORDER BY distance;

否则,如果您需要总行数,最好的办法是使用脚本语言(例如PHP)计算它们;或者如果你真的需要用SQL来做,你需要添加一个计算计数的窗口,所以添加

 count(*) over ()

但在这种情况下,您会为返回的每一行重复计数值。

答案 4 :(得分:0)

您必须向不在COUNT(1)中的元素添加GROUP BY。对于您的查询,您需要确定您在计算什么。添加计数,但按主键分组,将保证每个记录分组都是队列,因此COUNT将始终为1.如果您只需要一个例如venueName的COUNT,那么您的查询可以使用下面的查询并将其调整为需要在分组中计算的元素(例如添加地点纬度/长度等)。

SELECT venueName, COUNT(1) FROM marker GROUP BY venueName

答案 5 :(得分:0)

为什么不使用@@ ROWCOUNT从查询中获取结果行数。

http://msdn.microsoft.com/en-us/library/ms187316.aspx