有没有办法将聚合SQL查询转换为MS Access SQL?
一个简单的例子:
表:
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);
我想要转换为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
答案 0 :(得分:1)
首先,我会使用条件聚合重写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
您可以在MS Access中执行几乎相同的操作。排序:
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;
我不认为MS Access会执行整数的整数平均值。如果是这样,您可能希望将words
字段转换为avg()
之前的小数值。
但是,我鼓励您继续使用Postgres。