[英]Changing a Select Query to a Count Distinct Query
I am using a Select query to select Members, a variable that serves as a unique identifier, and transaction date, a Date format (MM/DD/YYYY). 我正在使用Select查询来选择Member(成员)(一个用作唯一标识符的变量)和交易日期(一种Date格式(MM / DD / YYYY))。
Select Members , transaction_date,
FROM table WHERE Criteria = 'xxx'
Group by Members, transaction_date;
My ultimate aim is to count the # of unique members by month (ie, a unique member in day 3, 6, 12 of a month is only counted once). 我的最终目标是按月计算唯一身份成员的数量(即,一个月的第3天,第6天,第12天仅计算一次)。 I don't want to select any data, but rather run this calculation (count distinct by month) and output the calculation. 我不想选择任何数据,而是运行此计算(按月计数)并输出计算。
This will give distinct count per month. 这将给每个月不同的计数。
select month,count(*) as distinct_Count_month
from
(
select members,to_char(transaction_date, 'YYYY-MM') as month
from table1
/* add your where condition */
group by members,to_char(transaction_date, 'YYYY-MM')
) a
group by month
So for this input 所以对于这个输入
+---------+------------------+
| members | transaction_date |
+---------+------------------+
| 1 | 12/23/2015 |
| 1 | 11/23/2015 |
| 1 | 11/24/2015 |
| 2 | 11/24/2015 |
| 2 | 10/24/2015 |
+---------+------------------+
You will get this output 您将获得此输出
+----------+----------------------+
| month | distinct_count_month |
+----------+----------------------+
| 2015-10 | 1 |
| 2015-11 | 2 |
| 2015-12 | 1 |
+----------+----------------------+
You might want to try this. 您可能想尝试一下。 This might work. 这可能有效。
SELECT REPLACE(CONVERT(DATE,transaction_date,101),'-','/') AS [DATE], SELECT REPLACE(CONVERT(DATE,transaction_date,101),'-','/')AS [DATE],
COUNT(MEMBERS) AS [NO OF MEMBERS] COUNT(成员)为[成员数]
FROM BAR 从酒吧
WHERE REPLACE(CONVERT(DATE,transaction_date,101),'-','/') IN 在哪里替换(CONVERT(DATE,transaction_date,101),'-','/')IN
( (
SELECT REPLACE(CONVERT(DATE,transaction_date,101),'-','/') SELECT REPLACE(CONVERT(DATE,transaction_date,101),'-','/')
FROM BAR 从酒吧
) )
GROUP BY REPLACE(CONVERT(DATE,transaction_date,101),'-','/') GROUP BY REPLACE(CONVERT(DATE,transaction_date,101),'-','/')
ORDER BY REPLACE(CONVERT(DATE,transaction_date,101),'-','/') ORDER BY REPLACE(CONVERT(DATE,transaction_date,101),'-','/')
Use COUNT(DISTINCT members) and date_trunc('month', transaction_date) to retain timestamps for most calculations (and this can also help with ordering the result). 使用COUNT(DISTINCT成员)和date_trunc('month',transaction_date)可以为大多数计算保留时间戳(这也有助于对结果进行排序)。 to_char() can then be used to control the display format but it isn't required elsewhere. 然后可以使用to_char()来控制显示格式,但在其他地方不需要。
SELECT
to_char(date_trunc('month', transaction_date), 'YYYY-MM')
, COUNT(DISTINCT members) AS distinct_Count_month
FROM table1
GROUP BY
date_trunc('month', transaction_date)
;
result sample: 结果样本:
| to_char | distinct_count_month |
|---------|----------------------|
| 2015-10 | 1 |
| 2015-11 | 2 |
| 2015-12 | 1 |
see: http://sqlfiddle.com/#!15/57294/2 参见: http : //sqlfiddle.com/#!15/57294/2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.