2014-02-13 89 views
0

以這種方式查詢我的結果數據。根據列選擇不同的行

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 

上方是連接兩個表的結果。問題是我不能在這種情況下使用不同的。

這裏是表如何

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

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

我有我的查詢,從表1基於CreatedDate選擇,它這樣

select count(*) 
from table1 

join table2 
on table1.id = table2.id 

where table1.creates between '' and ''. 

你如何得到不同尺寸。

如果我使用select count(distinct table2.size),它只對所有行返回1和2。

回答

0
SELECT DISTINCT Id, Size 
    FROM table1 

這應該給你一個不同的Id和大小組合的列表。

+0

是的它應該工作。更準確地說, SELECT DISTINCT ID,Size FROM table1,table2 WHERE table1.Id = table2.Id AND'table1.creates'和'' – SNathan

0
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 
+0

count會導致它只顯示count,而不是行本身。 – Codeman

+0

感謝您的回覆。如上所述,我沒有看到count()的工作。 ','附近的語法不正確。我應該使用不同的語法嗎? – user3285499

+0

@ Pheonixblade9嗯,在問題中,它被問及數量,而不是行。 – fancyPants