简体   繁体   English

向SQL Server查询中添加count(*)

[英]Adding count(*) to SQL Server query

I am trying to find a way to add a count to the output of my query: 我正在尝试找到一种向我的查询的输出添加计数的方法:

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;

The way I tried doing it was: 我尝试这样做的方式是:

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;

The error is this: 错误是这样的:

Column 'marker.id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. 选择列表中的“ marker.id”列无效,因为它既不包含在聚合函数中,也不包含在GROUP BY子句中。

How can I go about doing this correctly? 我该如何正确执行此操作?

Use the analytic version of COUNT , and make sure to put it in your outer query so it will respect the WHERE distance < 30 . 使用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;

Count(*) is going to make your query an aggregate and so, unless you use a group by, the result will be a single row. Count(*)将使您的查询成为一个聚合,因此,除非您使用group by,否则结果将是一行。 The error message is saying that you cant't use the other fields and still generate a single row 错误消息是说您不能使用其他字段,而仍会生成一行

You can not use COUNT(*) without GROUP BY or OVER (PARTITION BY ...) 没有GROUP BY或OVER(PARTITION BY ...),则不能使用COUNT(*)

Try to alter your query to 尝试将查询更改为

COUNT(*) OVER () AS rCount,

OR 要么

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

If you need to count the number of times the same row occours, just add a group by clause with all the fields you need to select, so: 如果需要计算同一行发生的次数,只需添加一个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;

Otherwise, if you need the total number of rows, the best thing to do is to count them with the script language (PHP for instance); 否则,如果您需要总行数,那么最好的办法就是使用脚本语言(例如PHP)对它们进行计数; or if you strictly need to do it with SQL you need to add a window on which calculate the count, so add 或者,如果您严格需要使用SQL来执行此操作,则需要添加一个窗口来计算计数,因此添加

 count(*) over ()

but in this case you have the count value repeated for each row returned. 但是在这种情况下,您需要为返回的每一行重复计数值。

You have to add a GROUP BY to the elements that are not in the COUNT(1) . 您必须将GROUP BY添加到不在COUNT(1)的元素中。 For your query, you need to decide what you are counting. 对于查询,您需要确定要计数的内容。 Adding a count, but grouping by the primary key, will guarantee each record grouping is queue, so the COUNT will always be 1. If you just need a COUNT by for instance venueName, then your query could use the query below and adjust it to the elements that need to be counted within the grouping (such as add venue lat/long, etc). 添加一个计数,但按主键进行分组,将确保每个记录的分组都是队列,因此COUNT始终为1。如果您仅需要一个按例如场所名称的COUNT,则您的查询可以使用下面的查询并将其调整为分组中需要计算的元素(例如添加场所纬度/经度等)。

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

Why do you not use @@ROWCOUNT to get the resulting row count from the query. 为什么不使用@@ ROWCOUNT从查询中获得结果行数。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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