简体   繁体   中英

Ms-Access multiple tables query

Greets! I have 12 tables, one for each month of the year:

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.

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.

MySQL code is like this:

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:

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". I was reading that the aliases cannot be applied within the "from", but then, i do not know how to fix this. 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.

MS-Access syntax is a real mess. 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) . Check this two links to the official documentation on how to do this:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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