Sql Ms Access多表查询

Sql Ms Access多表查询,sql,ms-access,Sql,Ms Access,欢迎!我有12张桌子,一年中每个月一张: January +----+------+ | id | venta| +----+------+ | 1 | 250 | | 3 | 500 | | 5 | 200 | | 7 | 100 | +----+------+ February +----+------+ | id | venta| +----+------+ | 1 | 350 | | 2 | 400 | | 3 |

欢迎!我有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 |
+----+-----------+-----------+
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;
其中,两个表中的匹配id不重复,其他月份缺少的id显示为0或任何其他符号,表示该id当月没有任何销售

我不得不用ASP为MySQL应用这个,一切都很酷,但对于控制台应用,我必须用ms access,不要问我为什么,我只是作为一名顾问

MySQL代码如下:

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 |
+----+-----------+-----------+
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”中,但我不知道如何解决这个问题。
提前,非常感谢

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

提出这样一个问题:

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]        

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

MS访问语法真是一团糟。试试这个:

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兼容的语法(ANSI 92),则添加的查询应该可以工作。检查这两个链接,以查看有关如何执行此操作的官方文档:


我有12张桌子,一年中每个月一张。
=>这似乎不是个好主意:)为什么你每个月都有一张单独的桌子?这是个糟糕的主意。。。但是公司就这样把DB交给了我,他们不想让我改变它。我只需要生成报告。我如何将数据库设置更改为SQL Server可比较语法?我添加了几个问题的链接:)谢谢。我换了它,它不工作了。表名周围的[]似乎与[]冲突。结构):哦,很好。表名中有空格。。。如果你引用《2013年10月文塔》中的
可能会有用(或单引号)很好!我保存了查询并与助手进行了交叉引用。非常感谢你。