关于计算表中是否存在不同值的SQL问题 [英] SQL question about counting the existence of a distinct value in a table

查看:49
本文介绍了关于计算表中是否存在不同值的SQL问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。


这是一个SQL问题,所以我希望这是一个正确的问题。


我' 插入压缩一个表到一个字段或几个字段的不同值,并且具有另一个字段,其中包含每个不同值的存在量,具体如下:


原始表:

Fname ..... LnameInitial

----------....-----------------

John ......... D。

John ......... B。

乔治.. ...... C。

Sam ......... D。

Sam ......... D。

汤姆.......... A。

汤姆.......... A。

汤姆...... ...... A。


压缩表或查询:

Fname ... LnameInitial..Amount

---------...---------- -----..-----------

约翰........ D. ................ 1

John ........ B. ................ 1

乔治.... C. ................ 1

Sam ........ D. ................ 2

Tom ......... A。 ............... 3

我不知道如何创建这样的查询。通过不同的SQL命令我找不到任何适合这种任务的东西。在我看来,它似乎是一个有用的查询,知道如何创建。


此外,如果问题不是太多,我很有兴趣创造完全相反的查询将从压缩的表创建原始表。


感谢您的关注,Michael。

Hi all.

This is an SQL question, so I hope this is the right place to ask it.

I''m intersted in "compressing" a table to distinct values of a field or several fields, and having another field with the amount of existence of each distinct value in the following way:

original table:
Fname.....LnameInitial
----------....-----------------
John.........D.
John.........B.
George.....C.
Sam.........D.
Sam.........D.
Tom..........A.
Tom..........A.
Tom..........A.

"compressed" table or query:
Fname...LnameInitial..Amount
---------...---------------..-----------
John........D. ................1
John........B. ................1
George....C. ................1
Sam........D. ................2
Tom.........A. ............... 3

I have no idea on how to create such a query. Going through different SQL commands I haven''t found anything appropriate for this kind of task. It does seem to me like a useful query to know how to create.

Also, if it''s not too much to ask, I''m intersted in creating exactly the opposite query that will create the original table from the compressed one.

Thanks for you attention, Michael.

推荐答案

试试这个...

Try this ...

展开 | 选择 | Wrap | 行号



试试这个.. 。

Try this ...

展开 | 选择 | 换行 | 行号



非常感谢,玛丽。有用。我不明白为什么count函数完全计算不同的行。
Thanks a lot, Mary. It works. What I don''t understand Is why the count function count exactly the distinct rows.



这是Group by的来源。按Fname分组然后按LnameInitial使值不同,然后Count计算每个组的LnameInitial数。 br />

This is where the Group by comes in. Group by Fname and then by LnameInitial makes the values distinct and the Count then counts the number of LnameInitial for each group.


如何将第二个草图中的表转换为第一个草图中的表的相反SQL命令?它可能吗?
What about an opposite SQL command that will transform the table in second sketch to a table in a first sketch? Is it at all possible?



您究竟在寻找什么?

What exactly are you looking for ?


这篇关于关于计算表中是否存在不同值的SQL问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆