Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 最后一小时计数和最后一次接收_Sql_Postgresql - Fatal编程技术网

Sql 最后一小时计数和最后一次接收

Sql 最后一小时计数和最后一次接收,sql,postgresql,Sql,Postgresql,有两个表A和表B,其中包含以下列和示例数据 表A Id Code Name Active --------------------------------- 1 A1 Apple t 2 B2 Banana t 3 C1 Cherry f 4 D2 Date t 表B Aid Received ---

有两个表A和表B,其中包含以下列和示例数据

A

Id    Code      Name       Active
---------------------------------
1     A1        Apple       t
2     B2        Banana      t
3     C1        Cherry      f
4     D2        Date        t
B

Aid             Received     
-----------------------------------
1        2014-10-02 10:24:55.095714
2        2014-10-02 10:54:53.226128
3        2014-10-02 15:39:59.683531
1        2014-10-02 15:39:59.862021
4        2014-10-02 15:42:19.923144
4        2014-10-02 15:49:29.964731
1        2014-10-02 15:53:27.586373
Aid
是表A

Id    Code      Name       Active
---------------------------------
1     A1        Apple       t
2     B2        Banana      t
3     C1        Cherry      f
4     D2        Date        t
我需要显示过去一小时内收到的所有姓名、号码和上次收到的时间

示例数据的预期输出如下所示

Name     Count          LastReceived     
-------------------------------------------
Banana     0     2014-10-02 10:54:53.226128
Apple      2     2014-10-02 15:53:27.586373
Date       1     2014-10-02 15:49:29.964731
SELECT 
  A.name,
  COUNT(B.Aid) AS Count,
  MAX(B.Received) AS LastReceived
FROM 
  A
FULL OUTER JOIN
  B
ON A.id = B.Aid 
WHERE B.Received > (NOW() - INTERVAL '1 hour') AND A.Active = TRUE --approx. 3pm to 4pm 10/2/2014
GROUP BY A.name
ORDER BY Count, A.name
到目前为止,我写的查询如下

Name     Count          LastReceived     
-------------------------------------------
Banana     0     2014-10-02 10:54:53.226128
Apple      2     2014-10-02 15:53:27.586373
Date       1     2014-10-02 15:49:29.964731
SELECT 
  A.name,
  COUNT(B.Aid) AS Count,
  MAX(B.Received) AS LastReceived
FROM 
  A
FULL OUTER JOIN
  B
ON A.id = B.Aid 
WHERE B.Received > (NOW() - INTERVAL '1 hour') AND A.Active = TRUE --approx. 3pm to 4pm 10/2/2014
GROUP BY A.name
ORDER BY Count, A.name
我没有获得此查询的预期输出。我应该如何修改查询以获得预期的输出

编辑


我需要对结果进行排序,先按字母顺序显示计数为零的结果,然后按字母顺序显示其余结果。我可以在一个查询中完成此操作吗?我知道按计数添加<代码>顺序,A.name < /代码>将不起作用。

您所遇到的麻烦是WHERE子句排除了最后一次接收到的日期要考虑的行。解决此问题的一种方法是使用条件聚合:

select
  a.name,
  sum(case when b.Received > (now() - interval '1 hour') then 1 else 0 end) AS Count,
  max(b.Received) as LastReceived
from
  a
    left outer join
  b
    on a.id = b.aid 
where
  a.Active = true
group by
  a.name
order by
  case when sum(case when b.Received > (now() - interval '1 hour') then 1 else 0 end) = 0 then 0 else 1 end,
  a.name
通过拉出一个公共表表达式,可以更容易理解order by works的方式:

with x as (
  select
    a.name,
    sum(case when b.Received > '2014-10-02 15:00:00' then 1 else 0 end) AS Count,
    max(b.Received) as LastReceived
  from
    a
      left outer join
    b
      on a.id = b.aid 
  where
    a.Active = true
  group by
    a.name
) select
  x.name,
  x.count,
  x.lastreceived
from
  x
order by
  case when x.Count = 0 then 0 else 1 end,
  x.name

< /P> < P>您的问题是WHERE子句排除了最后一次接收到的日期要考虑的行。解决此问题的一种方法是使用条件聚合:

select
  a.name,
  sum(case when b.Received > (now() - interval '1 hour') then 1 else 0 end) AS Count,
  max(b.Received) as LastReceived
from
  a
    left outer join
  b
    on a.id = b.aid 
where
  a.Active = true
group by
  a.name
order by
  case when sum(case when b.Received > (now() - interval '1 hour') then 1 else 0 end) = 0 then 0 else 1 end,
  a.name
通过拉出一个公共表表达式,可以更容易理解order by works的方式:

with x as (
  select
    a.name,
    sum(case when b.Received > '2014-10-02 15:00:00' then 1 else 0 end) AS Count,
    max(b.Received) as LastReceived
  from
    a
      left outer join
    b
      on a.id = b.aid 
  where
    a.Active = true
  group by
    a.name
) select
  x.name,
  x.count,
  x.lastreceived
from
  x
order by
  case when x.Count = 0 then 0 else 1 end,
  x.name

< /P> < P>您的问题是WHERE子句排除了最后一次接收到的日期要考虑的行。解决此问题的一种方法是使用条件聚合:

select
  a.name,
  sum(case when b.Received > (now() - interval '1 hour') then 1 else 0 end) AS Count,
  max(b.Received) as LastReceived
from
  a
    left outer join
  b
    on a.id = b.aid 
where
  a.Active = true
group by
  a.name
order by
  case when sum(case when b.Received > (now() - interval '1 hour') then 1 else 0 end) = 0 then 0 else 1 end,
  a.name
通过拉出一个公共表表达式,可以更容易理解order by works的方式:

with x as (
  select
    a.name,
    sum(case when b.Received > '2014-10-02 15:00:00' then 1 else 0 end) AS Count,
    max(b.Received) as LastReceived
  from
    a
      left outer join
    b
      on a.id = b.aid 
  where
    a.Active = true
  group by
    a.name
) select
  x.name,
  x.count,
  x.lastreceived
from
  x
order by
  case when x.Count = 0 then 0 else 1 end,
  x.name

< /P> < P>您的问题是WHERE子句排除了最后一次接收到的日期要考虑的行。解决此问题的一种方法是使用条件聚合:

select
  a.name,
  sum(case when b.Received > (now() - interval '1 hour') then 1 else 0 end) AS Count,
  max(b.Received) as LastReceived
from
  a
    left outer join
  b
    on a.id = b.aid 
where
  a.Active = true
group by
  a.name
order by
  case when sum(case when b.Received > (now() - interval '1 hour') then 1 else 0 end) = 0 then 0 else 1 end,
  a.name
通过拉出一个公共表表达式,可以更容易理解order by works的方式:

with x as (
  select
    a.name,
    sum(case when b.Received > '2014-10-02 15:00:00' then 1 else 0 end) AS Count,
    max(b.Received) as LastReceived
  from
    a
      left outer join
    b
      on a.id = b.aid 
  where
    a.Active = true
  group by
    a.name
) select
  x.name,
  x.count,
  x.lastreceived
from
  x
order by
  case when x.Count = 0 then 0 else 1 end,
  x.name

您的查询几乎是正确的,只是您需要一个
左连接
,因为表
a
是您的驾驶关系:

SELECT a.name,
       count(b.aid),
       max(b.received) AS LastReceived
  FROM a
  LEFT JOIN b ON a.id=b.aid AND b.received > now()-INTERVAL '1 hour'
 WHERE a.active
 GROUP BY a.name
 ORDER BY a.name;

此外,重要的是,您是否在
WHERE
子句中筛选出结果,在您的情况下,该子句跳过可能产生零计数的所有条目,或者您是否在联接条件中放入筛选器(如上所述)。

您的查询几乎正确,但需要
左联接,如表
A
所示,是您的驾驶关系:

SELECT a.name,
       count(b.aid),
       max(b.received) AS LastReceived
  FROM a
  LEFT JOIN b ON a.id=b.aid AND b.received > now()-INTERVAL '1 hour'
 WHERE a.active
 GROUP BY a.name
 ORDER BY a.name;

此外,重要的是,您是否在
WHERE
子句中筛选出结果,在您的情况下,该子句跳过可能产生零计数的所有条目,或者您是否在联接条件中放入筛选器(如上所述)。

您的查询几乎正确,但需要
左联接,如表
A
所示,是您的驾驶关系:

SELECT a.name,
       count(b.aid),
       max(b.received) AS LastReceived
  FROM a
  LEFT JOIN b ON a.id=b.aid AND b.received > now()-INTERVAL '1 hour'
 WHERE a.active
 GROUP BY a.name
 ORDER BY a.name;

此外,重要的是,您是否在
WHERE
子句中筛选出结果,在您的情况下,该子句跳过可能产生零计数的所有条目,或者您是否在联接条件中放入筛选器(如上所述)。

您的查询几乎正确,但需要
左联接,如表
A
所示,是您的驾驶关系:

SELECT a.name,
       count(b.aid),
       max(b.received) AS LastReceived
  FROM a
  LEFT JOIN b ON a.id=b.aid AND b.received > now()-INTERVAL '1 hour'
 WHERE a.active
 GROUP BY a.name
 ORDER BY a.name;


另外,重要的是,您是否在
WHERE
子句中筛选出结果,在您的情况下,该子句跳过可能产生零计数的所有条目,或者您是否将筛选器放入联接条件中(如上所述)。

谢谢。我无法获取带有零的项目的上次接收时间戳count@Srikanth:Hm,如果在规定的时间间隔内没有收到任何东西,则上次收到的
的含义是什么?在表B中,在最后一个小时内没有收到香蕉(约2014年2月10日下午3点至4点)因此,计数为零,但最后一次收到香蕉是在2014年10月2日上午11点左右,因此时间应为displayed@Srikanth,我明白了。劳伦斯的回答是,你应该跟着去。谢谢。我无法获取带有零的项目的上次接收时间戳count@Srikanth:Hm,如果在规定的时间间隔内没有收到任何东西,则上次收到的
的含义是什么?在表B中,在最后一个小时内没有收到香蕉(约2014年2月10日下午3点至4点)因此,计数为零,但最后一次收到香蕉是在2014年10月2日上午11点左右,因此时间应为displayed@Srikanth,我明白了。劳伦斯的回答是,你应该跟着去。谢谢。我无法获取带有零的项目的上次接收时间戳count@Srikanth:Hm,如果在规定的时间间隔内没有收到任何东西,则上次收到的
的含义是什么?在表B中,在最后一个小时内没有收到香蕉(约2014年2月10日下午3点至4点)因此,计数为零,但最后一次收到香蕉是在2014年10月2日上午11点左右,因此时间应为displayed@Srikanth,我明白了。劳伦斯的回答是,你应该跟着去。谢谢。我无法获取带有零的项目的上次接收时间戳count@Srikanth:Hm,如果在规定的时间间隔内没有收到任何东西,则上次收到的
的含义是什么?在表B中,在最后一个小时内没有收到香蕉(约2014年2月10日下午3点至4点)因此,计数为零,但最后一次收到香蕉是在2014年10月2日上午11点左右,因此时间应为displayed@Srikanth,我明白了。劳伦斯的答案是你应该接受的。我编辑了这个问题。你能修改答案以合并上面提到的顺序吗?@Srikanth我已经添加了,谢谢。它起作用了。你能解释一下你在
酷的时候对外套做了什么吗。谢谢你的例子。现在有道理了。我编辑了这个问题。你能修改答案以合并上面提到的顺序吗?@Srikanth我已经添加了,谢谢。它起作用了。你能解释一下你在
酷的时候对外套做了什么吗。谢谢你的例子。现在有道理了。我编辑了这个问题。你能修改答案以合并上面提到的顺序吗?@Srikanth我已经添加了,谢谢。它起作用了。你能解释一下你对外套做了什么吗