全部版块 我的主页
论坛 计量经济学与统计论坛 五区 计量经济学与统计软件 winbugs及其他软件专版
850 3
2015-10-08
我想select 3 个 count (based on different conditions)  in one query
下面是我的code, 每个select单独run就对,加上union就run不出来了
Select datepart(y,mydate), datepart(mon, mydate), count(distinct link) as entire  From lemon
where link like '%shop.lemon.com/checkout/spk/confirmation.jsp%'
group by datepart(y,mydate), datepart(mon,mydate)
order by datepart(y,mydate), datepart(mon,mydate)
Union
Select datepart(y,mydate), datepart(mon, mydate), count(distinct link) as Canada  From lemon
where link like '%shop.lemon.com/checkout/spk/confirmation.jsp%'and countrycode = '164'
group by datepart(y,mydate), datepart(mon,mydate)
order by datepart(y,mydate), datepart(mon,mydate)
Union
Select datepart(y,mydate), datepart(mon, mydate), count(distinct link) as USA  From lemon
where link like '%shop.lemon.com/checkout/spk/confirmation.jsp%'and countrycode = '840'
group by datepart(y,mydate), datepart(mon,mydate)
order by datepart(y,mydate), datepart(mon,mydate)
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2015-10-8 22:49:44
求助求助 求助 !
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2015-10-8 22:53:49
我还写了另一个版本,可以run,但是结果不对。求高手指点啊

SELECT datepart(y,mydate), datepart(mon,mydate),
          (select count(distinct link) From lemon where link like '%shop.lemon.com/checkout/spk/confirmation.jsp%' ) as entire ,
          (select count(distinct link) From lemon where link like '%shop.lemon.com/checkout/spk/confirmation.jsp%'  and countrycode = '124') as Canada,
          (select count(distinct link) From lemon where link like '%shop.lemon.com/checkout/spk/confirmation.jsp%'  and countrycode = '840') as USA
FROM lemon
group by datepart(y,mydate), datepart(mon,mydate)
order by datepart(y,mydate), datepart(mon,mydate)


date_part        date_part        entire        canada        usa
2014        1        10512        2065        7615
2014        2        10512        2065        7615
2014        3        10512        2065        7615
2014        4        10512        2065        7615
2014        5        10512        2065        7615
2014        6        10512        2065        7615
2014        7        10512        2065        7615
2014        8        10512        2065        7615
2014        9        10512        2065        7615
2014        10        10512        2065        7615
2014        11        10512        2065        7615
2014        12        10512        2065        7615
2015        1        10512        2065        7615
2015        2        10512        2065        7615
2015        3        10512        2065        7615
2015        4        10512        2065        7615
2015        5        10512        2065        7615
2015        6        10512        2065        7615
2015        7        10512        2065        7615
2015        8        10512        2065        7615
2015        9        10512        2065        7615
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2015-10-8 22:57:53
第三个版本 也是run不出来

select datepart(y,mydate), datepart(mon, mydate),
count(
select distinct link from lemon where link like '%shop.lemon.com/checkout/spk/confirmation.jsp%';
) as entire,
count(
select distinct link from lemon where link like '%shop.lemon.com/checkout/spk/confirmation.jsp%' and countrycode = '124';
) as CANADA,
count(
select distinct link fromlemon where link like '%shop.lemon.com/checkout/spk/confirmation.jsp%' and countrycode = '800';
) as USA
from lemon
group by datepart(y,mydate), datepart(mon,mydate)
order by datepart(y,mydate), datepart(mon,mydate)
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群