简体   繁体   English

有什么方法可以将PostgreSQL语句(聚合)转换为MS Access SQL?

[英]Is there any way to convert a PostgreSQL statement (aggregate) into MS Access SQL?

Is there any way to convert an aggregate SQL query into MS Access SQL? 有什么方法可以将汇总SQL查询转换为MS Access SQL?

A simple example: 一个简单的例子:

Table: 表:

CREATE TABLE testing.testtable
(
  aid serial NOT NULL,
  username text,
  words integer,
  sys_time timestamp with time zone NOT NULL DEFAULT now(),
  CONSTRAINT testtable_pkey PRIMARY KEY (aid)
)

insert into testing.testtable (username, words) values
('bob', 30),
('todd', 20),
('bob', 50),
('todd', 10);

The PostgreSQL statement I want to convert to Access SQL: 我想转换为Access SQL的PostgreSQL语句:

with cur as
(
    select testtable.username, AVG(testtable.words) as avgwords
    from testing.testtable
    group by testtable.username
), 
prev as
(
    select testtable.username, AVG(testtable.words) as avgwords
    from testing.testtable
    where testtable.sys_time <  date_trunc('day', NOW() - interval '1 month')
    group by testtable.username
)
    select p.username, c.avgwords - p.avgwords as change
    from prev p, cur c
    where p.username = c.username

First, I would rewrite the Postgres query using conditional aggregation as: 首先,我将使用条件聚合将Postgres查询重写为:

select tt.username,
       avg(tt.words) - avg(case when tt.sys_time <  date_trunc('day', NOW() - interval '1 month') then tt.words end)
from testing.testtable tt
group by tt.username

You can do almost the same thing in MS Access. 您可以在MS Access中执行几乎相同的操作。 Sort of: 有点:

select tt.username,
       avg(tt.words) - avg(iif(tt.sys_time < dateadd("m", -1, date()), tt.words, NULL)
from testing.testtable as tt
group by tt.username;

I don't think that MS Access does integer averages of integers. 我认为MS Access不会执行整数的整数平均值。 If so, you might want to convert the words field to a decimal value before the avg() . 如果是这样,您可能希望将words字段转换为avg()之前的十进制值。

However, I encourage you to continue using Postgres. 但是,我建议您继续使用Postgres。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM