Ms-Access多个表查询

时间:2013-12-04 18:08:55

标签: sql ms-access

电贺!我有12张桌子,一年中每个月都有一张桌子:

January

+----+------+  
| id | venta|  
+----+------+  
|  1 |  250 |  
|  3 |  500 |
|  5 |  200 |  
|  7 |  100 |   
+----+------+

February

+----+------+  
| id | venta|  
+----+------+  
|  1 |  350 |  
|  2 |  400 |
|  3 |  500 |  
|  4 |  800 |  
+----+------+

我需要进行查询,结果如下:

Annual Sales
+----+-----------+-----------+
| id | venta_Jan | venta_Feb |
+----+-----------+-----------+
|  1 |       250 |       350 |
|  2 |         0 |       400 |
|  3 |       500 |       500 |
|  4 |         0 |       800 |
|  5 |       200 |         0 |
|  7 |       100 |         0 |
+----+-----------+-----------+

如果来自两个表的匹配ID不重复,则通过放置0或任何其他符号来显示来自其他月份的缺失ID,表明该ID没有任何销售月份。

我不得不用ASP来应用这个MySQL,一切都很酷,但对于控制台应用程序我必须使用ms-access来做,不要问我原因,我只是作为顾问。

MySQL代码是这样的:

select id,
       sum(case when month = 'Enero' then venta else 0 end) as Venta_Ene,
       sum(case when month = 'Febrero' then venta else 0 end) as Venta_Feb,
       sum(case when month = 'Marzo' then venta else 0 end) as Venta_Mar,
       sum(case when month = 'Abril' then venta else 0 end) as Venta_Abr,
       sum(case when month = 'Mayo' then venta else 0 end) as Venta_May,
       sum(case when month = 'Junio' then venta else 0 end) as Venta_Jun,
       sum(case when month = 'Julio' then venta else 0 end) as Venta_Jul,
       sum(case when month = 'Agosto' then venta else 0 end) as Venta_Ago,
       sum(case when month = 'Septiembre' then venta else 0 end) as Venta_Sep,
       sum(case when month = 'Octubre' then venta else 0 end) as Venta_Oct
from (
      (select 'Enero' as month, id, venta from ene) union all
      (select 'Febrero' as month, id, venta from febr) union all
      (select 'Marzo' as month, id, venta from marz) union all
      (select 'Abril' as month, id, venta from abri) union all
      (select 'Mayo' as month, id, venta from mayo) union all
      (select 'Junio' as month, id, venta from juni) union all
      (select 'Julio' as month, id, venta from juli) union all
      (select 'Agosto' as month, id, venta from agos) union all
      (select 'Septiembre' as month, id, venta from sept) union all     
      (select 'Octubre' as month, id, venta from octu)
     ) as t
group by id;

然后,它完美地工作,我有类似这样的ms-access:

select Cliente,
       sum(iif month = 'Enero', Venta, 0) as Venta_Ene,
       sum(iif month = 'Febrero', Venta, 0) as Venta_Feb,
       sum(iif month = 'Marzo', Venta, 0) as Venta_Mar,
       sum(iif month = 'Abril', Venta, 0) as Venta_Abr,
       sum(iif month = 'Mayo', Venta, 0) as Venta_May,
       sum(iif month = 'Junio', Venta, 0) as Venta_Jun,
       sum(iif month = 'Julio', Venta, 0) as Venta_Jul,
       sum(iif month = 'Agosto', Venta, 0) as Venta_Ago,
       sum(iif month = 'Septiembre', Venta, 0) as Venta_Sep,
       sum(iif month = 'Octubre', Venta, 0) as Venta_Oct
from (
      (select 'Enero' as month, Cliente, Venta from [Venta Ene 2013]) union all
      (select 'Febrero' as month, Cliente, Venta from [Venta Feb 2013]) union all
      (select 'Marzo' as month, Cliente, Venta from [Venta Marzo 2013]) union all
      (select 'Abril' as month, Cliente, Venta from [Venta Abril 2013]) union all
      (select 'Mayo' as month, Cliente, Venta from [Venta Mayo 2013]) union all
      (select 'Junio' as month, Cliente, Venta from [Venta Junio 2013]) union all
      (select 'Julio' as month, Cliente, Venta from [Venta Julio 2013]) union all
      (select 'Agosto' as month, Cliente, Venta from [Venta Agosto 2013]) union all
      (select 'Septiembre' as month, Cliente, Venta from [Venta Sept 2013]) union all     
      (select 'Octubre' as month, Cliente, Venta from [Venta Oct 2013])
     ) as t
group by Cliente;

但是,有一个“加入错误”。我正在读取别名不能在“from”中应用,但是,我不知道如何解决这个问题。 提前,非常感谢!

2 个答案:

答案 0 :(得分:0)

您可以保持每月一个表的结构。虽然没有人会推荐它。

进行查询:

select 'Enero' as month, Cliente, Venta from [Venta Ene 2013] union all
select 'Febrero' as month, Cliente, Venta from [Venta Feb 2013] union all
select 'Marzo' as month, Cliente, Venta from [Venta Marzo 2013] union all
    etc....
select 'Octubre' as month, Cliente, Venta from [Venta Oct 2013]        

然后使用该查询作为基础进行交叉表查询。 Access向导将为您构建此功能。

答案 1 :(得分:0)

MS-Access语法真是一团糟。试试这个:

select Cliente,
   sum(iif month = 'Enero', Venta, 0) as Venta_Ene,
   sum(iif month = 'Febrero', Venta, 0) as Venta_Feb,
   sum(iif month = 'Marzo', Venta, 0) as Venta_Mar,
   sum(iif month = 'Abril', Venta, 0) as Venta_Abr,
   sum(iif month = 'Mayo', Venta, 0) as Venta_May,
   sum(iif month = 'Junio', Venta, 0) as Venta_Jun,
   sum(iif month = 'Julio', Venta, 0) as Venta_Jul,
   sum(iif month = 'Agosto', Venta, 0) as Venta_Ago,
   sum(iif month = 'Septiembre', Venta, 0) as Venta_Sep,
   sum(iif month = 'Octubre', Venta, 0) as Venta_Oct
from [
  (select 'Enero' as month, Cliente, Venta from [Venta Ene 2013]) union all
  (select 'Febrero' as month, Cliente, Venta from [Venta Feb 2013]) union all
  (select 'Marzo' as month, Cliente, Venta from [Venta Marzo 2013]) union all
  (select 'Abril' as month, Cliente, Venta from [Venta Abril 2013]) union all
  (select 'Mayo' as month, Cliente, Venta from [Venta Mayo 2013]) union all
  (select 'Junio' as month, Cliente, Venta from [Venta Junio 2013]) union all
  (select 'Julio' as month, Cliente, Venta from [Venta Julio 2013]) union all
  (select 'Agosto' as month, Cliente, Venta from [Venta Agosto 2013]) union all
  (select 'Septiembre' as month, Cliente, Venta from [Venta Sept 2013]) union all     
  (select 'Octubre' as month, Cliente, Venta from [Venta Oct 2013])
]. as t
group by Cliente;

我用方括号更改括号,并在末尾添加了无法解释的点。如果您的数据库设置为使用SQL Server Compatible Syntax (ANSI 92),则您添加的查询应该有效。请查看这两个指向官方文档的链接: