MS Access - How do I display two fields from separ

2019-05-10 08:57发布

I don't know if this is even possible or perhaps my database is designed poorly and I could have done it a better way?

Anyway here goes, I have database with two tables as follows "General Data" and "Assessment Data" - they're set up so that a patient can have a few basic details entered into "General Data" and then each time they have an assessment they have data relevant to that assessment entered into the "Assessment Data" table

Sorry about the dodgy formatting! Not enough reputation points to upload images :(

General Data

**ID --     Age (Years) --  Gender --   Town    ------ Referral Source --   Referral Doctor's Name**

1----   12----------------  Male--------    Batlow----- GP Referral----------   Smith

2----   13----------------  Male    --------Coolamon-   GP Referral ---------Bobby

Assessment Data

**ID -- Date of Assessment -----    Height (cm) ----- Weight (kg)**

1----   6/12/2012--------------------   100-------------------- 80

1----   13/12/2012------------------    100-------------------- 85

2----   25/12/2012  ------------------200--------------------   90

2----   28/12/2012------------------    200 ------------------- 90

What i would like to do is create a query that will create something like the following

Query?

**ID - - Height 1 - - Weight 1 - - Height 2 -- Weight 2 - - Height 3 - - Weight 3 etc...**

1 - - 100  - - - - - - - 80 - - - - - - - 100 - - - - - 85 - - - - - -  - - - - - - - - - - - - - - - - - 

2 - - 200 - - - - - - - 90 - - - - - - - 200 - - - -  - 90 - - - - - - - - - - - - - - - - - - - - - - -

I've had a bit of a play around with a concatenating function and query that i found on another forum but it wasn't suitable for what i wanted to do

Any help or even pointing me in the right direction would be great!

2条回答
地球回转人心会变
2楼-- · 2019-05-10 09:46

Try this:

select * from general_data 
INNER JOIN assessment_data ON general_data.id = assessment.id
查看更多
叛逆
3楼-- · 2019-05-10 09:46

You will need to use a couple of crosstab queries to acheive this.

First you need to sequence the "Assessment Data" table by using the following query and putting the results into a temporary table called "Temp_AssessmentData".

    SELECT AssData.Id, AssData.[Date of Assessment], 
        (SELECT Count(*) 
             FROM [Assessment Data] 
             WHERE [Date of Assessment] < [AssData].[Date of Assessment] 
                 AND Id = [AssData].Id
        )+1 AS Sequence 
    INTO Temp_AssessmentData FROM [Assessment Data] AS AssData GROUP BY AssData.Id,
       AssData.[Date of Assessment];

Then you can use this temporary table in two sub queries, one to get the weight values and another to get the height.

qAssessmentData_Weight

 TRANSFORM First([Assessment Data].[Weight (cm)]) AS [FirstOfWeight (cm)]
   SELECT Temp.Id
   FROM [Assessment Data] 
       INNER JOIN Temp_AssessmentData AS Temp ON (
          [Assessment Data].[Date of Assessment] = Temp.[Date of Assessment]) 
          AND ([Assessment Data].Id = Temp.Id)
   GROUP BY Temp.Id
   PIVOT "Weight " & [Sequence];

qAssessmentData_Height

 TRANSFORM First([Assessment Data].[Height (kg)]) AS [FirstOfHeight (kg)]
   SELECT Temp.Id
   FROM [Assessment Data] 
       INNER JOIN Temp_AssessmentData AS Temp ON (
          [Assessment Data].[Date of Assessment] = Temp.[Date of Assessment]) 
          AND ([Assessment Data].Id = Temp.Id)
   GROUP BY Temp.Id
   PIVOT "Height" & [Sequence];

You can then join these two sub queries together to then get your required query

SELECT Sub_Height.Id, Sub_Weight.[Weight 1], Sub_Height.[Height 1], 
    Sub_Weight.[Weight 2], Sub_Height.[Height 2]
FROM qAssessmentData_Height AS Sub_Height, qAssessmentData_Weight AS Sub_Weight;
查看更多
登录 后发表回答