5

我在 MS Access 2003 中工作。

我有这种结构的记录表:

ID, Origin, Destination, Attr1, Attr2, Attr3, ... AttrX

for example:

1,  1000,   1100,        20,    M,     5 ...
2,  1000,   1105,        30,    F,     5 ...
3,  1001,   1000,        15,    M,     10 ...
...

我也有分组的出发地和目的地代码的表

Code, Country, Continent
1000, Albania, Europe
1001, Belgium, Europe
...
1100, China,   Asia
1105, Japan,   Asia
...

我需要的是获得 2 个表,这些表将根据与我指定的属性相关的标准计算记录,但按以下方式分组:
1. 起源大陆和目的地大陆
2. 起源大陆和目的地国家

例如:

情况1。

Origin, Destination, Total, Females, Males, Older than 20, Younger than 20, ...
Europe, China,       300,   100,     200,   120,           180 ...
Europe, Japan,       150,   100,     50, ...
...

案例 2。

Origin, Destination, Total, Females, Males, Older than 20, Younger than 20, ...
Europe, Asia,        1500,  700,     800 ...
Asia,   Europe,      1200, ...
...

可以这样做,以便我可以轻松地添加更多列/标准吗?

4

2 回答 2

1

情况1:

select count(1) as total ,t2.continent,t3.country,t1.attr1,t1.attr2,t1.attr3 ... t1.attrX from table1 t1
join table2 t2 on t1.origin = t2.code
join table3 t3 on t1.destination = t3.code
group by t2.continent,t3.country,t1.attr1,t1.attr2,t1.attr3 ... t1.attrX 
order by total desc

案例二:

select count(1) as total ,t2.continent,t3.continent,t1.attr1,t1.attr2,t1.attr3 ... t1.attrX from table1 t1
join table2 t2 on t1.origin = t2.code
join table3 t3 on t1.destination = t3.code
group by t2.continent,t3.continent,t1.attr1,t1.attr2,t1.attr3 ... t1.attrX 
order by total desc
于 2012-11-23T13:22:37.990 回答
0

您可以将查询与查询连接起来,因此这是男性/女性 (attr2) 的交叉表

TRANSFORM Count(Data.ID) AS CountOfID
SELECT Data.Origin, Data.Destination, Count(Data.ID) AS Total
FROM Data
GROUP BY Data.Origin, Data.Destination
PIVOT Data.Attr2;

这是年龄:

TRANSFORM Count(Data.ID) AS CountOfID
SELECT Data.Origin, Data.Destination, Count(Data.ID) AS Total
FROM Data
GROUP BY Data.Origin, Data.Destination
PIVOT Partition([Attr1],10,100,10);

这结合了两者:

SELECT Ages.Origin, Ages.Destination, Ages.Total,
       MF.F, MF.M, Ages.[10: 19], Ages.[20: 29], Ages.[30: 39]
FROM Ages, MF;

如您所见,这在 VBA 中可能更容易管理。

于 2012-11-23T22:53:30.537 回答