我有下表 id | signed_up_at | product_id
-------+---------------------------------
20030 | 2017-09-15 12:51:45 | 2
20122 | 2017-09-15 12:51:45 | 2
21461 | 2017-09-15 12:51:45 | 2
20150 | 2017-09-13 10:10:10 | 2
19858 | 2017-09-10 23:00:54 | 2
20126 | 2017-09-10 23:00:28 | 2
20888 | 2017-09-10 23:00:28 | 2
20143 | 2017-09-10 23:00:28 | 2
21369 | 2017-09-10 23:00:02 | 2
SELECT DATE_TRUNC('day', signed_up_at::timestamptz) AS date,
SUM(COUNT(*)) OVER (ORDER BY DATE_TRUNC('day', signed_up_at::timestamptz)) AS sum
FROM "data_users"
WHERE product_id = 2
GROUP BY date ORDER BY date
我正在使用查询从该表中查找每天的累计总和:
date | sum
------------------------+------
2017-09-15 00:00:00+02 | 1693
2017-09-13 00:00:00+02 | 1690
2017-09-10 00:00:00+02 | 1689
这给出了类似的东西
signed_up_at
正如您所看到的那样,我的日期与没有 date | sum
------------------------+------
2017-09-15 00:00:00+02 | 1693
2017-09-14 00:00:00+02 | 1690
2017-09-13 00:00:00+02 | 1690
2017-09-12 00:00:00+02 | 1689
2017-09-11 00:00:00+02 | 1689
2017-09-10 00:00:00+02 | 1689
的用户的日期存在差距。
而不是这个,我希望得到以下结果
signed_up_at
所以我希望每天都能获得总和,而不仅仅是那些拥有GENERATE_SERIES
用户的日子。
我正试图用SELECT DATE_TRUNC('day', signed_up_at::timestamptz) AS date,
SUM(COUNT(*)) OVER (ORDER BY DATE_TRUNC('day', signed_up_at::timestamptz)) AS sum
FROM (SELECT GENERATE_SERIES(MIN(signed_up_at)::DATE, MAX(signed_up_at)::DATE, '1 DAY'::INTERVAL) AS date
FROM "data_users") AS d
LEFT OUTER JOIN "data_users" u ON u.signed_up_at::DATE = d.date::DATE
WHERE product_id = 2
GROUP BY signed_up_at ORDER BY date
实现这一点,但我没有得到理想的结果:
WHERE
我应该如何修改我的查询以支持它? Turo的回答有助于我指出正确的方向,但我现在遇到了import java.net.*;
import java.io.*;
class ScoketQuestion {
public static void main(String[] args) {
try {
final String URL = "";
Socket s = new Socket(URL, 80);
DataInputStream dIn = new DataInputStream(s.getInputStream());
PrintWriter wtr = new PrintWriter(s.getOutputStream());
wtr.println("GET / HTTP/1.1");
wtr.println("Host: " + URL);
wtr.println("");
wtr.flush();
byte[] data = new byte[1024];
int pt = 0;
String str;
while (true) {
pt = dIn.read(data);
if (pt == -1) break;
str = new String(data, 0, pt);
System.out.println(str);
}
} catch (IOException e) {
System.out.println(e);
}
}
}
条款的问题。
答案 0 :(得分:2)
我手边没有任何违规行为,我看到计数和小组中的问题,请尝试
SELECT DATE_TRUNC('day', date::timestamptz) AS date,
SUM(COUNT(signed_up_at)) OVER (ORDER BY DATE_TRUNC('day',
date::timestamptz)) AS sum
FROM (SELECT GENERATE_SERIES(MIN(signed_up_at)::DATE, MAX(signed_up_at)::DATE,
'1 DAY'::INTERVAL) AS date
FROM "data_users") AS d
LEFT OUTER JOIN "data_users" u ON u.signed_up_at::DATE = d.date::DATE
GROUP BY date ORDER BY date