计数数据不同的sql

时间:2018-05-18 04:15:07

标签: sql postgresql

当我想在不同的postgresql

中计算数据时,我遇到了问题

以下是我的查询。

SELECT COUNT (distinct on (a.userid) a.userid, b.name, a.checktime
FROM checkinout a 
LEFT JOIN userinfo b
       ON b.userid = a.userid
WHERE a.checktime >= '2017-12-28 06:15:00' 
AND a.checktime <= '2017-12-28 07:45:00')

我在#34; 上或附近得到语法错误

1 个答案:

答案 0 :(得分:1)

语法为:

select COUNT(distinct co.userid)
from checkinout co left join
     userinfo ui
     on ui.userid = co.userid
where ui.checktime >= '2017-12-28 06:15:00' and
      ui.checktime <= '2017-12-28 07:45:00';

LEFT JOIN正在将WHERE转变为内部联接,所以你不妨这样做:

select COUNT(distinct co.userid)
from checkinout co join
     userinfo ui
     on ui.userid = co.userid
where ui.checktime >= '2017-12-28 06:15:00' and
      ui.checktime <= '2017-12-28 07:45:00';