[英]Selecting distinct rows based on column
通过这种方式对矿山结果数据进行特定查询。
Id Size
123 1
123 1
123 2
123 2
134 1
134 1
134 2
我希望结果使我获得消除重复大小的计数
Id Size
123 1
123 2
134 1
134 2
以上是连接两个表的结果。 问题是我不能在这种情况下使用distinct。
这是表格的样子
Table1:
Id Created ... .. .. ..
123 date1 ....
134 date2 ....
Table2:
Id Size
123 1
123 2
134 1
134 2
我有基于CreatedDate从表1中选择的查询,它像这样
select count(*)
from table1
join table2
on table1.id = table2.id
where table1.creates between '' and ''.
您如何获得不同的尺寸。
如果我使用select count(distinct table2.size),则它仅对所有行返回1和2。
SELECT DISTINCT Id, Size
FROM table1
这应为您提供不同ID和大小组合的列表。
select count(distinct table1.id, table2.size)
from table1
join table2
on table1.id = table2.id
where table1.creates between '' and ''
有时解决方案是如此明显... :)
更新:另一种方式
select count(*) from (
select distinct table1.id, table2.size
from table1
join table2
on table1.id = table2.id
where table1.creates between '' and ''
) sq
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.