2016-03-23 3 views
0

null値を扱うケースステートメントがあります。私はさまざまなテストを行い、これがうまくいくと思っていますが、そうではありません。postgresのcase文でnull値を検索する方法

ユーザー12345にはクリックがありませんが、この場合は20plusが返されます。何か案は?あなたはCASE表現をこのように記述する必要があり

おかげ

select count(inid2), sum(clickcount) as Clicks, 
CASE clickcount when clickcount=1 then '1' 
when clickcount>=2 and clickcount<=5 then '2to5' 
when clickcount>=6 and clickcount<=10 then '6to10' 
when clickcount>=11 and clickcount<=20 then '11 to 20' 
when coalesce (clickcount, 0) =0 then 'none' 
else '20plus' 
END as clickbuckets 
from reportingsandbox.clicktest3 
where inid2 = '12345' 
group by clickbuckets 
+0

これは(構文エラー以外の)動作していない理由は明確ではありません。構文エラーもなくテストデータも含めた完全なコードを投稿してください。 –

+1

サンプルテーブルのデータと期待される結果を追加します。 (私はあなたがしようとしていることを理解できません...) – jarlh

+0

私は、クリック数に基づいてユーザーをバケートしたかったのです。 (ヌルを含む)。構文エラーは唯一の問題でした – ewan123uk

答えて

0

select count(inid2), sum(clickcount) as Clicks, 
     CASE 
      when clickcount=1 then '1' 
      when clickcount>=2 and clickcount<=5 then '2to5' 
      when clickcount>=6 and clickcount<=10 then '6to10' 
      when clickcount>=11 and clickcount<=20 then '11 to 20' 
      when coalesce (clickcount, 0) =0 then 'none' 
      else '20plus' 
     END as clickbuckets 
from reportingsandbox.clicktest3 
where inid2 = '12345' 
group by clickbuckets 
+1

postgresqlはclickcountをSUM(clickcount)のようにミックスできますか? – jarlh

+0

投稿者は投稿した内容に構文エラーがありましたが、Postgresはそのことについて語っていたでしょうから、問題ではないと思います。 –

+0

ありがとう、これは働いた。ケースの最初のクリック数を削除しました。私は1人のユーザーしかテストしていませんでした。それが他の人のために働いた理由です(私は思っています) – ewan123uk

関連する問題