是否可以在T-SQL中执行此操作?

时间:2017-11-19 06:43:57

标签: sql sql-server

我们有3张桌子

  • 项目
  • 零件
  • 中级Part_and_Project

Project有几个代码Parts

例如:

Id_Project: 1
   Id_Part:PART-098-876

Id_Project:1
   Id_Part:PART-055-777

Parts中,有几个代码更改日期,但代码可以重复

如何选择最高Parts代码,在Date_InitialDate_ExpectedDate_Finish中获取MAX值?

我怎么能有这样的结果?

1,PART-098-876,2018-05-14 00:00:00 PM,2018-05-03 00:00:00 PM,2018-05-03 00:00:00 PM 
1,PART-055-777,2018-05-14 00:00:00 PM,2018-05-03 00:00:00 PM,2018-05-09 00:00:00 PM

我试过这个:

SELECT 
    Project.Id_Project, Id_Part, 
    MAX(Date_Initial),
    MAX(Date_Expected),
    MAX(Date_Finish) 
FROM 
    Project
LEFT JOIN 
    Part_and_Project ON Part_and_Project.Id_Project = Project.Id_Project
INNER JOIN 
    Parts ON Parts.Id_Part = Part_and_Project.Id_Part 

但它不起作用

测试数据:

CREATE TABLE [dbo].[Parts]
(
    [Id_Part] [varchar](30) NULL,
    [Date_Initial] [datetime] NULL,
    [Date_Expected] [datetime] NULL,
    [Date_Finish] [datetime] NULL,
) ON [PRIMARY]

CREATE TABLE [dbo].[Project]
(
    [Id_Project] [int] IDENTITY(1,1) NOT NULL,
    [Status] [varchar](10) NULL,
    [Id_User] [varchar](20) NULL,
    [Value_Revenue] [varchar](20) NULL,
    [Launch] [varchar](20) NULL,

    CONSTRAINT [PK_Proyect] 
        PRIMARY KEY CLUSTERED ([Id_Project] ASC)
) ON [PRIMARY]

CREATE TABLE [dbo].[Part_and_Project]
(
    [Id_Project] [int] NULL,
    [Id_Part] [varchar](30) NULL
) ON [PRIMARY]


INSERT INTO [Parts] ([Id_Part], [Date_Initial], [Date_Expected],[Date_Finish], [Name]) 
VALUES ('PART-098-876', '2018-06-12 00:00:00 PM', '2017-05-03 00:00:00 PM', '2011-05-04 00:00:00 PM'),
       ('PART-098-876', '2014-06-03 00:00:00 PM', '2013-05-03 00:00:00 PM', '2014-09-08 00:00:00 PM'),
       ('PART-098-876', '2015-06-17 00:00:00 PM', '2013-03-02 00:00:00 PM', '2013-07-03 00:00:00 PM'),
       ('PART-098-876', '2018-05-14 00:00:00 PM', '2018-05-03 00:00:00 PM', '2018-05-09 00:00:00 PM'),
       ('PART-055-777', '2013-03-13 00:00:00 PM', '2017-05-03 00:00:00 PM', '2011-07-08 00:00:00 PM'),
       ('PART-055-777', '2011-03-07 00:00:00 PM', '2013-05-03 00:00:00 PM', '2013-07-08 00:00:00 PM'),
       ('PART-055-777', '2015-06-17 00:00:00 PM', '2013-03-02 00:00:00 PM', '2015-08-07 00:00:00 PM'),
       ('PART-055-777', '2018-05-14 00:00:00 PM', '2018-05-03 00:00:00 PM', '2018-05-09 00:00:00 PM')

INSERT INTO [Part_and_Proyect] ([Id_Project], [Id_Part]) 
VALUES ('1', 'PART-098-876'), ('1', 'PART-055-777')

2 个答案:

答案 0 :(得分:1)

试试这个:

    SELECT  Proyect.Id_Project,
        Id_Part,
        Date_Initial,
        Date_Expected,
        Date_Finish 
    FROM Proyect
    LEFT JOIN Part_and_Proyect 
        ON Part_and_Proyect.Id_Project = Proyect.Id_Project
    INNER JOIN Parts 
        ON Parts.Id_Part = Part_and_Proyect.Id_Part 
    CROSS APPLY( SELECT MAX(Date_Initial) AS Max_Date_Initial FROM Parts p 
        WHERE p.Id_Part = Parts.Id_Part) as di
    CROSS APPLY( SELECT MAX(Date_Expected) AS Max_Date_Expected FROM Parts p 
        WHERE p.Id_Part = Parts.Id_Part) as de
    CROSS APPLY( SELECT MAX(Date_Finish) AS Max_Date_Finish FROM Parts p 
        WHERE p.Id_Part = Parts.Id_Part) as df
    WHERE   di.Max_Date_Initial = Date_Initial 
        AND de.Max_Date_Expected = Date_Expected 
        AND df.Max_Date_Finish = Date_Finish

答案 1 :(得分:0)

很大程度上取决于[Part_And_Proyect]表的设置方式。如果每个部件只存在于一个项目中,并且在[Part_And_Proyect]表中的项目中没有重复,则此工作正常,并且适用于[Part_And_Proyect]表中的任意数量的部件。

代码创建一个LOOP,使用变量(@ID_Part)降低[Part_And_Proyect]表中的ID_Part值。首先,它将ID_Project和ID_Part值插入临时表(#Maxdates),然后插入该部分所需的每个最大日期。之后,它将@ID_Part变量增加到下一个ID_Part值并重复该过程。

CREATE TABLE #Maxdates
(
[Id_Project] INT,
[Id_Part] VARCHAR(30),
[MAX_Date_Initial] DATETIME,
[MAX_Date_Expected] DATETIME,
[MAX_Date_Finish] DATETIME
)
GO

DECLARE @Id_Part NCHAR(12) = (SELECT MIN(Id_Part) FROM [Part_And_Proyect])
WHILE @Id_Part <= (SELECT MAX(Id_Part) FROM [Part_And_Proyect])
BEGIN
INSERT INTO #Maxdates VALUES
(
(SELECT ID_Project FROM [Part_And_Proyect] WHERE ID_Part = @ID_Part)
,
@ID_Part
,
(SELECT MAX([Date_Initial]) FROM PARTS WHERE ID_Part = @ID_Part)
,
(SELECT MAX([Date_Expected]) FROM PARTS WHERE ID_Part = @ID_Part)
,
(SELECT MAX([Date_Finish]) FROM PARTS WHERE ID_Part = @ID_Part)
)
SET @ID_Part =  (SELECT MIN(ID_Part) FROM [Part_And_Proyect] WHERE ID_Part > @ID_Part)
END
GO

SELECT * FROM #Maxdates