用于获取表中计数的SELECT语句

时间:2014-09-29 18:48:36

标签: sql select

我有一张桌子"下载"以下信息:

objKey     CustNum    UserId    DownloadDate
51048879   123        7654       2014-09-01
51048879   234        32434      2014-09-02
51048879   123        7654       2014-09-01
51047379   2132       3456       2014-08-20
51048879   2132       9786       2014-09-01
51047379   123        7654       2014-09-02

我希望通过downloadDate了解下载了多少产品,客户和用户。所以我想要:

DownloadDate      Products       Customers    Users
2014-09-01         1              2            2
2014-09-02         2              2            2
2014-08-20         1              1            1

有人可以帮我处理SELECT语句吗?我每次尝试都会得到错误的数字。我认为这应该比我尝试的更容易。

3 个答案:

答案 0 :(得分:5)

select downloaddate,
       count(distinct objkey) as products,
       count(distinct custnum) as customers,
       count(distinct userid) as users
from downloads
group by downloaddate

答案 1 :(得分:5)

你想要count(distinct)。我认为这样做你想要的:

select DownloadDate, count(distinct objKey) as Products,
       count(distinct CustNum) as Customers,
       count(distinct UserId) as Users
from table t
group by DownloadDate
order by DownloadDate;

答案 2 :(得分:0)

这样的事可能会这样做:

select DownloadDate, count(disinct objKey) as products, count(distinct CustNum) as customers, count(distinct UserId) as users
from downloads
group by DownloadDate
order by DownloadDate