3

我不知道这是否可能,或者我的数据库设计不佳,我本可以做得更好?

无论如何,我有两个表的数据库,如下“ General Data”和“ Assessment 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

评估数据

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

我想做的是创建一个查询,该查询将创建如下内容

询问?

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

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

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

我对在另一个论坛上找到的连接函数和查询进行了一些尝试,但它不适合我想做的事情

任何帮助甚至为我指明正确的方向都会很棒!

4

2 回答 2

1

您将需要使用几个交叉表查询来实现这一点。

首先,您需要使用以下查询对“评估数据”表进行排序,并将结果放入名为“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];

然后你可以在两个子查询中使用这个临时表,一个获取重量值,另一个获取高度。

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];

然后,您可以将这两个子查询连接在一起,然后获取所需的查询

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;
于 2013-06-15T07:30:25.170 回答
0

尝试这个:

select * from general_data 
INNER JOIN assessment_data ON general_data.id = assessment.id
于 2012-12-14T01:26:43.510 回答