Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
334 views
in Technique[技术] by (71.8m points)

sql - Access - retrieving status and date puzzle

In Access 2016, I have the following tables:

Table1
------
| ID | FK_ID | Status_date |Status_ID |
---------------------------------------
| 1  | 11    | d1          | 1        |
| 2  | 11    | d2          | 2        |
| 3  | 22    | d3          | 3        |
| 4  | 22    | d4          | 3        |

LookupTable1
------------
| OBJ_ID | Status |
-------------------
| 1      | A      |
| 2      | B      |
| 3      | C      |

And I would like to produce the following result. This will ultimately be exported to Excel.

xls report
==========
| FK_ID | Status_1_date | Status_2_date | Status_3_date |  <-- these will be aliased
=========================================================
| 11    | d1            | d2            |               |
| 22    |               |               | d4            |

The part of the puzzle I'm struggling with is that there seem to be at least these different ways to achieve this a) multiple Access queries b) a single Access query with in-line queries (possible?) c) VBA code d) in SQL Server itself e) other... What's the simplest way to create and maintain this, as the LookupTable1.Status values will change.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Looks like a simple CROSSTAB query:

TRANSFORM Max(Table1.Status_date) AS MaxOfStatus_date
SELECT Table1.FK_ID
FROM Table1
GROUP BY Table1.FK_ID
PIVOT Table1.Status_ID;

If you want the output to show the Status alias values, first JOIN the two tables then use the Status field as column header in CROSSTAB.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...