0
我需要从上周找到顶级申请人,但我在我的SQL查询中遇到错误。SQL聚合列问题
var queryString = "select id, created_at, user_id, count(id) as cnt from "+
"applications where id in (select id from applications where "+
"created_at > current_date - interval '1 week') group by user_id";
data.sql
insert into listings (id, created_at, created_by, name, description) values
(1, '2015-01-15 11:00', 1, 'Join us conquering the world!', 'This is your best chance to be on the right side of the equation...')
;
insert into listings (id, created_at, created_by, name, description) values
(1, '2015-01-15 11:00', 1, 'Join us conquering the world!', 'This is your best chance to be on the right side of the equation...')
;
insert into listings (id, created_at, created_by, name, description) values
(2, '2017-01-29 11:00', 1, 'Join us conquering the world!', 'Holla ho')
;
insert into listings (id, created_at, created_by, name, description) values
(3, '2017-01-15 11:00', 1, 'Join us conquering the world!', 'Hey ya')
;
insert into applications (created_at, user_id, listing_id, cover_letter) values
('2017-02-23 12:00', 2, 1, 'Hello, ...')
;
INSERT INTO APPLICATIONS (CREATED_AT, USER_ID, LISTING_ID, COVER_LETTER) VALUES
('2017-02-24 12:00', 2, 2, 'HELLO, ...')
;
INSERT INTO APPLICATIONS (CREATED_AT, USER_ID, LISTING_ID, COVER_LETTER) VALUES
('2017-02-22 12:00', 2, 2, 'HELLO, ...')
;
INSERT INTO APPLICATIONS (CREATED_AT, USER_ID, LISTING_ID, COVER_LETTER) VALUES
('2017-02-25 12:00', 3, 1, 'HELLO, ...')
;
这里是错误:
column "applications.id" must appear in the GROUP BY clause or be used in an aggregate function
我做错了吗?
所以,基本上我想看到用户ID 2
与3
应用程序和用户ID 3
与1
应用。
请你进行标记正在使用的DBMS。 –
错误非常明显。您需要将'applications.Id'添加到group by子句中。 – Sparrow
@FeryalBadili不那么简单。在OP的查询中,他选择了id,created_at,...以便至少传递错误,他需要GROUP BY中的id和create_at列。但我认为OP首先是在处理错误的查询,但他并没有完全理解他对“COUNT”函数的意图。 –