一次对多个字段进行分组和计数

发布于 2024-11-10 12:14:48 字数 501 浏览 2 评论 0原文

是否可以合并这些查询,以便搜索条件仅执行一次?

SELECT category, count(*)
FROM   ads
WHERE  [search criteria]
GROUP BY category

SELECT state, count(*)
FROM   ads
WHERE  [search criteria]
GROUP BY state

SELECT price_range, count(*)
FROM   ads
WHERE  [search criteria]
GROUP BY price_range

...

Plus about 10 more COUNTS

我的目标是创建一个类似于此网站<中使用的过滤器/a>.

我正在考虑使用某种搜索引擎(例如 Lucene)而不是关系数据库。欢迎新想法。

Is it possible to merge these queries so the search criteria is executed only once?

SELECT category, count(*)
FROM   ads
WHERE  [search criteria]
GROUP BY category

SELECT state, count(*)
FROM   ads
WHERE  [search criteria]
GROUP BY state

SELECT price_range, count(*)
FROM   ads
WHERE  [search criteria]
GROUP BY price_range

...

Plus about 10 more COUNTS

My aim is to create a filter similar to that is used in this website.

I'm thinking about using some kind of search engine (eg. Lucene) instead of relational database. New ideas are welcome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

染墨丶若流云 2024-11-17 12:14:48

什么是数据库管理系统?在 Oracle 中,您可以使用分析函数来做到这一点:

SELECT category, state, price_range,
       COUNT( 1 ) OVER ( PARTITION BY category ) AS category_count,
       COUNT( 1 ) OVER ( PARTITION BY state) AS state_count,
       COUNT( 1 ) OVER ( PARTITION BY price_range ) AS price_count
  FROM ads

What's the DBMS? In Oracle you can do that with analytic functions:

SELECT category, state, price_range,
       COUNT( 1 ) OVER ( PARTITION BY category ) AS category_count,
       COUNT( 1 ) OVER ( PARTITION BY state) AS state_count,
       COUNT( 1 ) OVER ( PARTITION BY price_range ) AS price_count
  FROM ads
隔纱相望 2024-11-17 12:14:48

爱德华多,
您可以使用存储过程吗?如果是这样,您可以先执行公共部分。

SELECT ... 
FROM   ads WHERE  [search criteria]

然后您可以在较小的表上执行多个查询。您还可以存储结果并将整个结果作为一个包含三列的表返回:

  1. 类型(类别、州、价格范围等)
  2. 名称(实际上是类别/州/等值)
  3. 计数

Eduardo,
Are you allowed to use a stored procedure? If so, you can execute the common part first.

SELECT ... 
FROM   ads WHERE  [search criteria]

Then you can do your multiple queries on the smaller table. You can also store the results and return the whole thing as one table with three columns:

  1. type (category, state, price range, etc)
  2. name (actually category/state/etc value)
  3. count
无悔心 2024-11-17 12:14:48

如果您的用例除了列出的查询之外还包括全文搜索,Solr 可能值得一看在。结合了对结构化数据和文本数据的分面搜索,并且可以相当有效地进行您想要的各种计数。但这在一定程度上取决于您拥有多少数据以及您的用例。

If your use case includes full-text search in addition to the queries you listed, Solr might be worth looking at. In incorporates faceted search over structured and text data, and can do the kinds of counts you want reasonably efficiently. But it depends in part on how much data you have, and what your use cases are.

我也只是我 2024-11-17 12:14:48

您可以通过使用嵌套查询和一些技巧来做到这一点;)

  select category,countCategory,state,countState,price_range,countPrice_range from 
    (SELECT 1 pcol,category, count(*) countCategory
    FROM   ads
    WHERE  [search criteria]
    GROUP BY category) 
    a
    inner  join
    (
    SELECT 1 pcol,state, count(*) countState
    FROM   ads
    WHERE  [search criteria]
    GuROUP BY state
    ) b
    on a.pcol=b.pcol
    inner join
    (SELECT 1 pcol,price_range, count(*) countPrice_range
    FROM   ads
    WHERE  [search criteria]
    GROUP BY price_range)
    )
    on a.pcol=b.pcol

you can do by using nested query and some tricks ;)

  select category,countCategory,state,countState,price_range,countPrice_range from 
    (SELECT 1 pcol,category, count(*) countCategory
    FROM   ads
    WHERE  [search criteria]
    GROUP BY category) 
    a
    inner  join
    (
    SELECT 1 pcol,state, count(*) countState
    FROM   ads
    WHERE  [search criteria]
    GuROUP BY state
    ) b
    on a.pcol=b.pcol
    inner join
    (SELECT 1 pcol,price_range, count(*) countPrice_range
    FROM   ads
    WHERE  [search criteria]
    GROUP BY price_range)
    )
    on a.pcol=b.pcol
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文