f**********n 发帖数: 828 | 1 假设有这个一个table:
state | phone_number | call_time | answer_time
一个entry可以是下面这个样子
WA | 123-456-7890 | 2013-07-08 08:00:00 | NULL
或者
WA | 123-456-7890 | 2013-07-08 08:00:00 | 2013-07-08 08:13:00
对于同一个电话号码,可能有好几个entry,因为call好几次。。
要求找出对于每一个state来说,所有(distinct)电话号码的总数,所有answer_time
至少有一个不是null的(distinct)电话号码总数。
好像不是一个难题。。。可是我越来越晕了。。。
谢谢!!!! |
a******n 发帖数: 11246 | 2 写两个query好了,第一步把NULL的都扔掉
create table temp as
sel * from table where answer_time is not null;
再计算每个州的distinct电话总数
sel state, count(distinct phone_number) from temp group by 1;
time
【在 f**********n 的大作中提到】 : 假设有这个一个table: : state | phone_number | call_time | answer_time : 一个entry可以是下面这个样子 : WA | 123-456-7890 | 2013-07-08 08:00:00 | NULL : 或者 : WA | 123-456-7890 | 2013-07-08 08:00:00 | 2013-07-08 08:13:00 : 对于同一个电话号码,可能有好几个entry,因为call好几次。。 : 要求找出对于每一个state来说,所有(distinct)电话号码的总数,所有answer_time : 至少有一个不是null的(distinct)电话号码总数。 : 好像不是一个难题。。。可是我越来越晕了。。。
|
f**********n 发帖数: 828 | 3 不是哦,要求只用一个query
然后,我可能没有表达清楚。
有些电话号码可能是没有被answer过的,这些也要count
然后,group by state
【在 a******n 的大作中提到】 : 写两个query好了,第一步把NULL的都扔掉 : create table temp as : sel * from table where answer_time is not null; : 再计算每个州的distinct电话总数 : sel state, count(distinct phone_number) from temp group by 1; : : time
|
l****u 发帖数: 529 | 4 Select distinct state, count(distinct telenum) as totnum, count( distinct (
case when answer ne 'null' then telenum else . end)) as ansnum
From your data
Group by state; |
f**********n 发帖数: 828 | 5 原来count里面可以用case when then!
谢谢啦!!
(
【在 l****u 的大作中提到】 : Select distinct state, count(distinct telenum) as totnum, count( distinct ( : case when answer ne 'null' then telenum else . end)) as ansnum : From your data : Group by state;
|