- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
给定这张表:
create temp table stats (
name text, country text, age integer
)
insert into stats values
('eric', 'se', 1),
('eric', 'dk', 4),
('johan', 'dk', 6),
('johan', 'uk', 7),
('johan', 'de', 3),
('dan', 'de', 3),
('dan', 'de', 3),
('dan', 'de', 4)
我想知道国家或年龄与键相同的不同名称的数量。
country age count
se 1 1
de 3 2
de 4 3
dk 4 3
dk 6 2
uk 7 1
有 3 个不同的名字有 country = dk
(eric, johan) 或 age = 4
(eric,dan)
所以我的问题是,编写此查询的最佳方式是什么?
我有这个解决方案,但我觉得它很丑!
with country as (
select count(distinct name), country
from stats
group by country
),
age as (
select count(distinct name), age
from stats
group by age
),
country_and_age as(
select count(distinct name), age, country
from stats
group by age, country
)
select country, age, c.count+a.count-ca.count as count from country_and_age ca join age a using(age) join country c using(country)
有什么更好的方法吗?
最佳答案
您也可以加入原始表:
SELECT
s1.country,
s1.age,
COUNT(distinct s2.name)
FROM stats s1
JOIN stats s2 ON s1.country=s2.country OR s1.age=s2.age
GROUP by 1, 2;
关于sql - Count distinct where 属性是任一键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27961799/
我有一个包含电子邮件、IP、州、城市、时间戳、ID 列的表 我需要按州分组计算电子邮件和 IP 的不同位置 所以当我运行 MYSQL 查询时, select State, City ,count(di
我试过 select distinct ID from DB.TABLE; 它返回所有记录中的唯一 ID。 select distinct * from DB.TABLE; 它将通过比较所有列
我正在尝试在 Postgresql 中编写一个查询,该查询提取一组有序数据并按不同的字段对其进行过滤。我还需要从同一表行中提取其他几个字段,但需要将它们排除在不同的评估之外。示例: SELECT
我有一个使用以下语句创建的 Postgres 表。该表由另一个服务的数据转储填充。 CREATE TABLE data_table ( date date DEFAULT NULL,
我在一个名为 products 的表中有 4 列 id|p_name| p_img | 1 | Xs | xsmax.png | 2 | Xs | xr.png |
当它的状态仅为"is"时,我想从“num”中选择不同的值,而不是立即包括“否”? 表: +--------+-----+--------+ | id | num | status | +---
全部!今天我有一个棘手的问题要给你,我想使用 select DISTINCT 语句来选择一个需要不同的行,但也在同一个语句中(或者我尝试过的方式?)一个没有的行't/不能区分。我想要的结果是每个类名中
我有一个正在使用 Distinct() 的 linq 查询。如果我只是调用 Distinct() 而没有转换为列表,那么它不会返回不同的列表 - 它仍然包含重复项。 但是,如果我转换为 List 并然
说到性能,我应该使用 .ToList().Distinct() 还是 .Distinct().ToList() ? 两种扩展方法是否生成相同的 SQL 查询? 看起来第二种方法应该表现更好,但这是真的
如何在不支持 SQL Server 2008R2 的 SQL 实现中重写包含标准 IS DISTINCT FROM 和 IS NOT DISTINCT FROM 运算符的表达式? 最佳答案 IS DI
有一张 table (在 HIVE) 示例 - meanalytics.key2_master_ids 该表有 6 列(cmpgn_id、offr_id、exec_id、creatv_id、cmpl_
SELECT * FROM `amc_info` WHERE department =' ( SELECT DISTINCT department ) into outfile = 'Differe
如何在Elasticsearch中计算“不同的平均值”?我有一些这样的非规范化数据: { "record_id" : "100", "cost" : 42 } { "record_id" : "200
关注这个question我有... ID SKU PRODUCT ======================= 1 FOO-23 Orange 2 BAR
我有这个 mysql 查询: SELECT DISTINCT post.postId,hash,previewUrl,lastRetrieved FROM post INNER JOIN (tag a
http://sqlfiddle.com/#!2/37dd94/17 如果我执行 SELECT DISTINCT,我得到的结果与只执行 SELECT 的结果相同。 在查询结果中,您将看到两个包含 Di
我有一列包含空条目,例如此列中的可能值为 None, 1, 2, 3 当我使用 session.query(func.count(distinct(Entry.col))).scalar() 计算列中
这是否可能从表列中选择不同的行并计算单个查询中每个不同字段的重复行 $sql = "SELECT DISTINCT location and COUNT(DISTINCT location)
我在 MySQL 数据库中有一个包含 1100 万行的表。其中一列是个人身份证号码。人们在表中被多次列出,我想知道有多少个唯一的个人 ID 号码。然后创建一个包含这些唯一数字的表格。当我计算列中不同的
我刚刚注意到我的 Informix SQL 列(在同一个表中)的某些 上有些奇怪。当我执行此查询时 SELECT DISTINCT colName FROM myTable 例如,我得到 40 行。但
我是一名优秀的程序员,十分优秀!