简体   繁体   English

MS-Access多表查询

[英]Ms-Access multiple tables query

Greets! 问候! I have 12 tables, one for each month of the year: 我有12张桌子,一年中的每个月一张:

January

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

February

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

etc. 等等

I need to do a query where the result is something like this: 我需要做一个查询,结果是这样的:

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 |
+----+-----------+-----------+

Where the matching ids from both tables do not duplicate and the missing ids from other months are shown by putting a 0 or any other symbol indicating that there was not any sales that month from that id. 如果两个表中的匹配ID都不重复,而其他月份缺少的ID则以0或任何其他符号表示,表示该月该月份没有任何销售。

I had to apply to this for MySQL with ASP and everything was cool, but for the console application I have to do it with ms-access, do not ask me why, I'm there just as a consultant. 我必须使用ASP将它应用于MySQL,并且一切都很棒,但是对于控制台应用程序,我必须使用ms-access来做到这一点,不要问我为什么,我只是作为一名顾问。

MySQL code is like this: 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;

And it works perfectly, then, and I have something like this for ms-access: 然后,它运行完美,对于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;

But, there's a "JOIN error". 但是,存在“ JOIN错误”。 I was reading that the aliases cannot be applied within the "from", but then, i do not know how to fix this. 我读到的别名不能在“ from”中应用,但是,我不知道如何解决此问题。 In advance, thank you very much! 提前,非常感谢!

You can keep your one-table-per-month structure. 您可以保持每月一张表的结构。 Although no one would recommend it. 尽管没有人会推荐它。

Make a query thus: 进行查询:

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]        

Then make a crosstab query using that query as the basis. 然后以该查询为基础进行交叉表查询。 The Access wizard will build this for you. Access向导将为您构建它。

MS-Access syntax is a real mess. MS-Access语法实在是一团糟。 Try this out: 试试看:

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;

I changed the parentheses with square brackets and added the unexplainable dot at the end. 我用方括号更改了括号,并在末尾添加了无法解释的点。 The query you've added should work if your database is set to use SQL Server Compatible Syntax (ANSI 92) . 如果数据库设置为使用SQL Server Compatible Syntax (ANSI 92)则添加的查询应该可以使用。 Check this two links to the official documentation on how to do this: 检查这两个指向官方文档的链接,以了解如何执行此操作:

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

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