Count(distinct(兩個欄位))??

看板Database作者 (surewin)時間16年前 (2010/02/07 17:13), 編輯推噓2(204)
留言6則, 4人參與, 最新討論串1/1
請問一下 我在sql 2005 需要下一個sql command ex: select count(distinct(a1,a2)) from table where a2='xx' 不能run 好像distinct裡面不可以接受 兩個欄位 但是我的資料因為 a1 有可能是 null 所以 select count(distinct(a1) from table where a2='xx' 會算錯 所以我需要 a2 去帶出來 但那又不能run 請問有甚麼替代的 語法嗎 ?? 想很久一直解決不了 -- ~~~哇尚無甲意"贏"ㄟ感覺~~~ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 117.19.114.141

02/07 21:14, , 1F
條件是a2='xx' 那distinct(a1,a2)和distinct(a1)應該一樣吧
02/07 21:14, 1F

02/07 21:35, , 2F
a1 null為什麼會算錯?不要算 null就 where a1 is not null
02/07 21:35, 2F

02/07 21:35, , 3F
distinct(a1) a1有可能是 null 所以才要把a2加進去
02/07 21:35, 3F

02/07 21:41, , 4F
null對我來說也是一筆資料 直接distinct(a1)他部會算null
02/07 21:41, 4F

02/08 00:23, , 5F
DISTINCT(ISNULL(a1, a2)) 試試看!?
02/08 00:23, 5F

02/08 22:43, , 6F
distinct(isnull(a1, 不可能出現在a1的值)) int←→char
02/08 22:43, 6F
文章代碼(AID): #1BReGrdt (Database)