所以我有一张下表,
product_id sender_id timestamp ...other columns...
1 2 1222
1 2 3423
1 2 1231
2 2 890
3 4 234
2 3 234234
我想获取sender_id = 2的行,但是我想对product_id进行计数和分组,并按时间戳降序进行排序。这意味着我需要以下结果
product_id sender_id timestamp count ...other columns...
1 2 3423 3
2 2 890 1
我尝试了以下查询:
SELECT product_id, sender_id, timestamp, count(product_id), ...other columns...
FROM table
WHERE sender_id = 2
GROUP BY product_id
但是我收到以下错误Error in query: ERROR: column "table.sender_id" must appear in the GROUP BY clause or be used in an aggregate function
似乎我无法选择不在GROUP BY中的列。我在网上发现的另一种方法是加入
SELECT product_id, sender_id, timestamp, count, ...other columns...
FROM table
JOIN (
SELECT product_id, COUNT(product_id) AS count
FROM table
GROUP BY (product_id)
) table1 ON table.product_id = table1.product_id
WHERE sender_id = 2
GROUP BY product_id
但是这样做只是列出所有行,而不进行分组或计数。我的猜测是ON部分只是再次扩展表。
答案 0 :(得分:1)
尝试使用product_id, sender_id
select product_id, sender_id, count(product_id), max(timestamp) maxtm
from t
where sender_id = 2
group by product_id, sender_id
order by maxtm desc
如果您还想要其他列:
select t.*, t1.product_count
from t
inner join (
select product_id, sender_id, count(product_id) product_count, max(timestamp) maxtm
from t
where sender_id = 2
group by product_id, sender_id
) t1
on t.product_id = t1.product_id and t.sender_id = t1.sender_id and t.timestamp = t1.maxtm
order by t1.maxtm desc
答案 1 :(得分:0)
只需使用数据进行锻炼即可
CREATE TABLE products (product_id INTEGER,
sender_id INTEGER,
time_stamp INTEGER)
INSERT INTO products VALUES
(1,2,1222),
(1,2,3423),
(1,2,1231),
(2,2,890),
(3,4,234),
(2,3,234234)
SELECT product_id,sender_id,string_agg(time_stamp::text,','),count(product_id)
FROM products
WHERE sender_id=2
GROUP BY product_id,sender_id
这里您具有不同的time_stamp,因此您需要应用一些汇总或仅在select语句中删除该列。
如果您在select语句中删除time_stamp,则将非常容易,如下所示:
SELECT product_id,sender_id,count(product_id)
FROM products
WHERE sender_id=2
GROUP BY product_id,sender_id