ASP.NET SQL消除重复的ID

时间:2019-06-16 15:08:59

标签: c# sql sql-server

我有两个数据库表来记录伤口愈合的过程。那些通过以下方式在wound_id-Column上连接:

因此,对于一个伤口,我可以创造许多进步来证明其愈合。一切正常。

这是表格的代码:

表伤口细节:

CREATE TABLE [dbo].[epadoc_mod_wound_details] (
    [wound_id]          INT           IDENTITY (1, 1) NOT NULL,
    [wound_type]        VARCHAR (500) NULL,
    [wound_description] VARCHAR (500) NULL,
    [decuGrade]         INT           NULL,
    [wound_comments]    VARCHAR (500) NULL,
    [wound_timeReal]    DATETIME      NULL,
    [wound_timeGiven]   DATETIME      NULL,
    [casenumber]        INT           NULL,
    [username]          VARCHAR (50)  NULL,
    [infectionstate]    VARCHAR (50)  NULL,
    PRIMARY KEY CLUSTERED ([wound_id] ASC)
);

表伤口进展:

CREATE TABLE [dbo].[epadoc_mod_wound_progress] (
    [progress_id]       INT           IDENTITY (1, 1) NOT NULL,
    [wound_length]      INT           NULL,
    [wound_width]       INT           NULL,
    [wound_depth]       INT           NULL,
    [wound_surrounding] VARCHAR (500) NULL,
    [wound_consistence] VARCHAR (500) NULL,
    [wound_state]       VARCHAR (200) NULL,
    [wound_painscale]   INT           NULL,
    [wound_itch]        INT           NULL,
    [wound_id]          INT           NULL,
    PRIMARY KEY CLUSTERED ([progress_id] ASC),
    CONSTRAINT [FK_epadoc_mod_wound_progress_fk] FOREIGN KEY ([wound_id]) REFERENCES [dbo].[epadoc_mod_wound_details] ([wound_id]) ON DELETE CASCADE
);

然后,我编写了一个SELECT查询,以显示针对患者的特定病例编号的所有伤口:

SELECT DISTINCT
    dbo.epadoc_mod_wound_details.wound_id, dbo.epadoc_mod_wound_details.casenumber, dbo.epadoc_mod_wound_details.wound_type, dbo.epadoc_mod_wound_progress.progress_id, dbo.epadoc_mod_wound_details.wound_comments, dbo.epadoc_mod_wound_details.wound_timeReal, dbo.epadoc_mod_wound_details.username    
FROM dbo.epadoc_mod_wound_details LEFT JOIN 
     dbo.epadoc_mod_wound_progress
     ON dbo.epadoc_mod_wound_details.wound_id = dbo.epadoc_mod_wound_progress.wound_id
WHERE dbo.epadoc_mod_wound_details.casenumber = @casenr;

虽然工作正常,但问题是所有伤口进度都显示在GridView中,这是一个示例,您可以了解我的意思:

GridView Screenshot

我想做的只是显示一个伤口的最新进展,因此对于上面的示例,仅显示具有progressID 65的最后一个条目:

33     65          1111111  Dekubitus   
34         ..          .......  .........

SELECT DISTINCT方法不起作用,我也尝试使用MAX(progressID),​​但是我似乎总是会出错。我想我必须在JOIN之前用ORDER BY或第二个SELECT-Query做些事情。

谢谢您的建议!

3 个答案:

答案 0 :(得分:1)

您应在查询中结合使用GROUP BYMAX

SELECT  
   dbo.epadoc_mod_wound_details.wound_id, 
   dbo.epadoc_mod_wound_details.casenumber, 
   dbo.epadoc_mod_wound_details.wound_type, 
   MAX(dbo.epadoc_mod_wound_progress.progress_id) AS progress_id, 
   dbo.epadoc_mod_wound_details.wound_comments, 
   dbo.epadoc_mod_wound_details.wound_timeReal, 
   dbo.epadoc_mod_wound_details.username    
FROM 
  dbo.epadoc_mod_wound_details 
LEFT JOIN 
  dbo.epadoc_mod_wound_progress ON 
  dbo.epadoc_mod_wound_details.wound_id = dbo.epadoc_mod_wound_progress.wound_id 
GROUP BY
   dbo.epadoc_mod_wound_details.wound_id, 
   dbo.epadoc_mod_wound_details.casenumber, 
   dbo.epadoc_mod_wound_details.wound_type, 
   dbo.epadoc_mod_wound_details.wound_comments, 
   dbo.epadoc_mod_wound_details.wound_timeReal, 
   dbo.epadoc_mod_wound_details.username;

答案 1 :(得分:1)

由于只需要progress_id,所以简便的方法是使用相关的子查询:

SELECT  wound_id, 
        casenumber, 
        wound_type, 
        (
            SELECT TOP 1 progress_id
             FROM dbo.epadoc_mod_wound_progress AS WP
             WHERE WP.wound_id = WD.wound_id
             ORDER BY progress_id
        ) As progress_id, 
        wound_comments, 
        wound_timeReal, 
        username    
FROM dbo.epadoc_mod_wound_details As WD
WHERE casenumber = @casenr;

答案 2 :(得分:1)

我了解您需要每条“ epadoc_mod_wound_details”记录以及最新的“ epadoc_mod_wound_progress”记录。

您可以尝试以下方法:

Function TLGV(Modele As String, Pressure As Integer) 'Total Load Grand Vitesse

    If Modele = "DCHC05" Then
        If Pressure = 30 Then
            Worksheets("VC Types").Activate
            TLGV = Range("H4").Value
        End If
    Else

    End If