2012-10-12 33 views
1

這裏是我的選擇看起來像:麻煩與SELECT語句 - 找到最小值,最大值在不同條件下 - SQL Server 2008的

DocID DocNumber MilestoneDate MilestoneID 
30  14-1  2001-10-01 10 
30  14-1  2001-10-15 11 
30  14-1  2002-11-06 13 
30  14-1  2003-11-01 27 
30  14-1  2003-11-01 30 
30  14-1  2004-03-01 28 
30  14-1  2004-03-01 31 
31  14-3  2003-06-27 13 
31  14-3  2004-05-05 27 
31  14-3  2004-05-05 30 
31  14-3  2005-07-20 35 
31  14-3  2005-08-31 23 
36  1-2   1996-10-01 10 
36  1-2   1996-10-01 11 
36  1-2   1996-12-01 28 
36  1-2   1996-12-01 31 

我需要做一個報告了這一選擇,將有1排每個DocID。 它必須列 的DocID, DocNumber, EarliestDate =如果MilestoneID 10或11不是空的選擇更大了這兩個其他 的最早日期從其他MilestoneIDs的針對的DocID(這是我的麻煩)的 任何幫助將是讚賞。

謝謝

+0

你應該嘗試清理表(使用4位在啓動,使其保持你的格式,使用空格而不是製表符分隔),以及顯示您嘗試過的SELECT的示例。在現有代碼中顯示問題要比經常創建一個全新的查詢更容易。 – Guvante

+0

向我們展示[你嘗試過的](http://www.whathaveyoutried.com)。 – Kermit

+0

這是我試過的:select DocID,DocNumber, CASE當(MAX(MilestoneID)= 10或MAX(MilestoneID)= 11)和Not Max(MilestoneDate)爲空THEN Max(MilestoneDAte)END最早 FROM table 從 – Ddragov

回答

0

這是你在找什麼?

DECLARE @Table TABLE (DocID INT, DocNumber VARCHAR(10), MilestoneDate DATETIME, MilestoneID INT) 
INSERT INTO @Table VALUES (30, '14-1', '10-01-2001', 10) 
INSERT INTO @Table VALUES (30, '14-1', '10-15-2001', 11) 
INSERT INTO @Table VALUES (30, '14-1', '11-06-2002', 13) 
INSERT INTO @Table VALUES (30, '14-1', '11-01-2003', 27) 
INSERT INTO @Table VALUES (30, '14-1', '11-01-2003', 30) 
INSERT INTO @Table VALUES (30, '14-1', '03-01-2004', 28) 
INSERT INTO @Table VALUES (30, '14-1', '03-01-2004', 31) 
INSERT INTO @Table VALUES (31, '14-3', '06-27-2003', 13) 
INSERT INTO @Table VALUES (31, '14-3', '05-05-2004', 27) 
INSERT INTO @Table VALUES (31, '14-3', '05-05-2004', 30) 
INSERT INTO @Table VALUES (31, '14-3', '07-20-2005', 35) 
INSERT INTO @Table VALUES (31, '14-3', '08-31-2005', 23) 
INSERT INTO @Table VALUES (36, '1-2', '10-01-1996', 10) 
INSERT INTO @Table VALUES (36, '1-2', '10-01-1996', 11) 
INSERT INTO @Table VALUES (36, '1-2', '12-01-1996', 28) 
INSERT INTO @Table VALUES (36, '1-2', '12-01-1996', 31) 

-- get the items where we want the max 
SELECT DocID, DocNumber, MAX(MilestoneDate) AS MilestoneDate 
FROM @Table WHERE MilestoneID IN (10, 11) 
GROUP BY DocID, DocNumber 
UNION -- get the items where we want the min 
SELECT DocID, DocNumber, MIN(MilestoneDate) 
FROM @Table 
WHERE DocID NOT IN (SELECT DocID FROM @Table WHERE MilestoneID IN (10, 11)) 
GROUP BY DocID, DocNumber 

Result: 
DocID  DocNumber  MilestoneDate 
30   14-1    2001-10-15 00:00:00.000 
31   14-3    2003-06-27 00:00:00.000 
36   1-2    1996-10-01 00:00:00.000 
+0

感謝您的回覆。這給我想要的結果。現在我無法將這個邏輯合併到我更復雜的查詢中。這是我需要的其中一個列的基本結果。我將添加我的整個查詢來給你一個洞察。 – Ddragov

+0

您可以編輯原始帖子以包含更詳細的查詢。如果您編輯我的帖子,它會被拒絕。 – pvanhouten

+0

感謝這就是我所做的,但不知道它會被拒絕:) – Ddragov

0
SELECT 
    t.DocID, 
    t.DocNumber, 
    EarliestDate = 
     ISNULL(MAX(CASE WHEN t.MilestoneID IN (10, 11) 
         THEN t.MilestoneDate END), 
       MIN(CASE WHEN t.MilestoneID IN (10, 11) 
         THEN NULL ELSE t.MilestoneDate END)) 
FROM [table] t 
GROUP BY 
    t.DocID, 
    t.DocNumber 
0

我帶着我的問題的解決方案。

任何有待改進的建議都值得歡迎。 因爲我需要合併建議,pvanhouten給了我到另一個查詢,我決定做這樣的事情:

Select t1.DocID, t1.DocNumber, t1.DocTitle, t1.DevStatus, t1.GrOrgType, t1.DocType, 
     t1.WebStatus, t1.RecommandType, t1.ProjectPlanApprovedBySponsor, 
     t1.ProjectPlanApprovedByWG, t1.Completed, t2.MilestoneDate as EarliestDate From 
    (Select * From 
    (Select distinct d.DocID, d.DocNumber, d.DocTitle,dm.MilestoneDate,lkpds.DevStatus, 
        lkpgt.GrOrgType, lkpdt.DocType, lkpws.WebStatus, lkprt.RecommandType, 
     CASE dm.MilestoneId 
      WHEN 10 Then 'PPAW' 
      WHEN 11 Then 'PPAS' 
      WHEN 35 Then 'Completed' 
     END as [MilestoneName], 

     (CASE WHEN not dv.DocID is Null THEN 'Yes' END) OldVersionExist 

     FROM Document d LEFT JOIN lkpDocDevStatus lkpds ON d.DocDevStatID = lkpds.DocDevStatID 
      LEFT JOIN lkpDocType lkpdt ON d.DocTypeID = lkpdt.DocTypeID 
      LEFT JOIN lkpDocRecType lkprt ON d.DocRecTypeID = lkprt.DocRecTypeID 
      LEFT JOIN lkpWebStatus lkpws ON d.WebStatID = lkpws.WebStatID 
      LEFT JOIN Doc_Group dg ON d.DocID = dg.DocID 
      LEFT JOIN GroupOrg gro ON dg.GroupID = gro.GroupID 
      LEFT JOIN lkpGroupOrgType lkpgt ON lkpgt.GrOrgTypeID = gro.GrOrgTypeID 
      LEFT JOIN Doc_Milestone dm ON d.DocID = dm.DocID 
      JOIN Milestones m ON m.MilestoneID = dm.MilestoneID 
      LEFT JOIN Doc_Version dv ON d.DocID = dv.DocID 

     Where dg.GrOrgRoleID = 5 and lkpds.DocDevStatID <> 1 and lkpds.DocDevStatID <> 9    

Group By d.DocID, d.DocNumber, d.DocTitle, lkpds.DevStatus, lkpds.DocDevStatID, 
      lkpgt.GrOrgType, lkpdt.DocType, 
      lkpws.WebStatus, lkprt.RecommandType, dv.docID, dm.MilestoneDate, dm.Milestoneid) T 

PIVOT 
(
    Max(MilestoneDate) 
    FOR [MilestoneName] IN ([PPAW],[PPAS],[Completed]) 
) AS PivotTable 
) t1 

JOIN 
    (SELECT dm.DocID , MAX(MilestoneDate) AS MilestoneDate 
    FROM document d JOIN Doc_Milestone as dm ON d.DocID = dm.DociD WHERE MilestoneID IN (10, 
     11) 
    GROUP BY dm.DocID 
    UNION -- get the items where we want the min 
    SELECT dm.DocID, MIN(MilestoneDate) 
    FROM document d JOIN Doc_Milestone as dm ON d.DocID = dm.DociD 
    WHERE dm.DocID NOT IN (SELECT dm.DocID FROM Doc_Milestone as dm WHERE dm.MilestoneID IN (10, 
    11)) 
    GROUP BY dm.DocID) as t2 

ON t1.DocID = t2.DocID 
相關問題