选择计数到多列 SQL
我想知道是否可以对许多列中不同数量的不同项目进行唯一的选择。到目前为止,这工作正常,但只按列进行一次计数,例如:
SELECT provincia,count(provincia) as total_provincia
FROM museos
group by provincia ORDER BY provincia
然后,如果尝试使用其他字段,看起来仍然很好,正如我所需要的:
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 的列只是第一个列值的副本,如我们所见。那么,是否可以进行一个选择查询,返回对不同列进行的两个或多个计数,以获得这种响应:
我的意思是,最终所需表的组织结果是一个树形方案,其中像“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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
over(partition by)
:You can use
over(partition by)
: