多くの注文レコードを含むテーブルがあり、各レコードには、ユーザーを示すために使用される phone という名前の列があります。
私がやりたいことは、特定の日付範囲で個別の電話番号を数えることです。そして私のSQLは次のようなものです:
select count(distinct phone),
count(distinct case when order_date < '2013-1-19' then phone end)
from some_table
where order_date >= '2013-1-1' and order_date < '2013-2-1'
結果は奇妙です:
194410 0
ステートメントcount(distinct case when order_date < '2013-1-19' then phone end)
の結果は ZERO です。ただし、次の sql を使用して手動で結果を確認した場合:
select count(distinct phone)
from some_table
where order_date >= '2013-1-1' and order_date < '2013-1-19'
条件より171300件の電話会議があるとのことでした。
そして、日付範囲を [2013-1-1, 2013-1-16) に減らすとcount(distinct case when order_date < '2013-1-19' then phone end)
、正しい結果が得られました。そして、今回は 161415 個の固有の電話しかありません。
私の最初のSQLの何が問題になっていますか?
ありがとう。