简体   繁体   English

根据列选择不同的行

[英]Selecting distinct rows based on column

A particular query of mine results data this way. 通过这种方式对矿山结果数据进行特定查询。

 Id    Size  
 123     1
 123     1
 123     2
 123     2
 134     1
 134     1
 134     2

I want the results get me the count eliminating the duplicate size like 我希望结果使我获得消除重复大小的计数

 Id    Size  
 123     1
 123     2
 134     1
 134     2

Above was result of joining two tables. 以上是连接两个表的结果。 Problem is I cant use distinct in this case. 问题是我不能在这种情况下使用distinct。

Here is how tables are 这是表格的样子

Table1:
   Id Created ... .. .. ..
   123 date1 ....
   134 date2 ....

Table2:     
 Id    Size  
 123     1
 123     2
 134     1
 134     2

I have my query that select from Table1 based on CreatedDate, its like this 我有基于CreatedDate从表1中选择的查询,它像这样

 select count(*)
 from table1

 join table2
 on table1.id = table2.id

 where table1.creates between '' and ''.

How do you get the distinct sizes. 您如何获得不同的尺寸。

If I use select count(distinct table2.size), it only returns 1 and 2 for all rows. 如果我使用select count(distinct table2.size),则它仅对所有行返回1和2。

SELECT DISTINCT Id, Size
    FROM table1

This should give you a list of distinct Id and Size combinations. 这应为您提供不同ID和大小组合的列表。

 select count(distinct table1.id, table2.size)
 from table1

 join table2
 on table1.id = table2.id

 where table1.creates between '' and ''

Sometimes the solution is so obvious... :) 有时解决方案是如此明显... :)

UPDATE: another way 更新:另一种方式

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM