选择计数到多列 SQL

发布于 2025-01-15 01:59:48 字数 1238 浏览 0 评论 0原文

我想知道是否可以对许多列中不同数量的不同项目进行唯一的选择。到目前为止,这工作正常,但只按列进行一次计数,例如:

SELECT  provincia,count(provincia) as total_provincia
FROM museos
group by provincia ORDER BY provincia

Select for uno count

然后,如果尝试使用其他字段,看起来仍然很好,正如我所需要的:

SELECT  localidad,count(localidad) as total_localidad
FROM museos
group by localidad ORDER BY localidad

另一个选择查询的结果

但是,当我试图在一个 Select 查询中获取两列及其计数,它应该是这样的:

SELECT  provincia,localidad,count(localidad) as total_localidad,
        count(provincia) as total_provincia
FROM museos
group by provincia,localidad ORDER BY provincia,localidad

然后我得到:

我一直在堆栈和网站上到处寻找示例,不幸的是我找不到与我尝试做的类似的东西,而且我真的没有得到为什么只看起来有效的答案计数之一,并且始终为小粒度数据,在本例中为“localidad”。同时,“provincia”的其他计数被忽略,其名为 Total 的列只是第一个列值的副本,如我们所见。那么,是否可以进行一个选择查询,返回对不同列进行的两个或多个计数,以获得这种响应:

Hopefull Select 查询预期

我的意思是,最终所需表的组织结果是一个树形方案,其中像“provincia”这样的数据是主体或根,其毛细管数据将是树叶。以这种方式构建查询有点奇怪,但我认为根本不是不可能的。因此,任何帮助或评论我将不胜感激。

I wondering if I can do a unique Select to diferent count of distinct items into many columns. By now this works fine, but doing only one count by column like:

SELECT  provincia,count(provincia) as total_provincia
FROM museos
group by provincia ORDER BY provincia

Result of Select for uno count

Then if a try with other field, still looks good as I needed too:

SELECT  localidad,count(localidad) as total_localidad
FROM museos
group by localidad ORDER BY localidad

Result of another Select Query

But, when I trying to get both column with its count at one Select query, it's suppose to be something like this:

SELECT  provincia,localidad,count(localidad) as total_localidad,
        count(provincia) as total_provincia
FROM museos
group by provincia,localidad ORDER BY provincia,localidad

Then I got:

Select for both count

i've been looking everywhere around Stack and websites with examples, unfortunately I couldn't find something similar of what I tryied to do, and I realy got no answer for why only seems to work one of the count, and always for the small granular data, in this case, "localidad". At the same time the other count of "provincia" is ignored, and its column named as total is just a copie of the values of the first one, as we can see. So, is it possible to make a Select query that return two or more count made on diferent columns, in order to get this kind of response:

Hopefull Select query expected

I mean, finaly the organization of the required table result in a tree scheme, where data like "provincia" are the body or the root, and its capillary data would be the leaves. It's kind of weird built a Query this way, but I think is not impossible at all. So any help or coment I'll be greatful.

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

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

发布评论

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

评论(1

岁吢 2025-01-22 01:59:48

您可以使用over(partition by)

select distinct
    provincia, count(provincia) over(partition by provincia) 'count_provincia'
    ,localidad, count(localidad) over(partition by localidad) 'count_localidad'
from museos
order by provincia;

You can use over(partition by):

select distinct
    provincia, count(provincia) over(partition by provincia) 'count_provincia'
    ,localidad, count(localidad) over(partition by localidad) 'count_localidad'
from museos
order by provincia;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文