2012-01-30 40 views
1

我問了這個問題,並得到了一個很好的答案爲mysql:How to assign date value to all returned mysql queries但現在我需要在MS SQL(SQL Server 2005)中做同樣的事情。警惕的是,我有一個似乎破壞事情的案例陳述。我有兩個疑問這樣:如何獲取所有類似條目的maxdate MySQL

select * from (select row_number() 
    over (order by a.device ASC) 
    AS rownum, a.device, staff, requestDate, m.maxDate, attribute = 
       REPLACE((SELECT attribute AS [data()] 
       FROM deviceList 
       WHERE device = a.device 
       ORDER BY device FOR xml PATH('')), ' ', ' ') 
    FROM deviceList a 
    INNER JOIN (SELECT device, MAX(requestDate) AS maxDate 
       FROM ASSIGNMENT GROUP BY device) m on m.device = a.device 
    RIGHT JOIN Assignment 
     ON (Assignment.device=a.device) 
    GROUP BY a.device, staff, maxDate, requestDate, contract) 
    AS B WHERE B.rownum > 0; 





select * from (select row_number() 
    over (order by a.device ASC) 
    AS rownum, a.device, staff, requestDate, CONVERT(
    varchar, dateadd(year, 
    (CASE 
     WHEN contract = '10' THEN 1 
     WHEN contract = '20' THEN 2 
     WHEN contract = '30' THEN 3 
     ELSE 3 
     END), m.maxDate) 107) as myEndDate, attribute = 
       REPLACE((SELECT attribute AS [data()] 
       FROM deviceList 
       WHERE device = a.device 
       ORDER BY device FOR xml PATH('')), ' ', ' ') 
    FROM deviceList a 
    INNER JOIN (SELECT device, MAX(requestDate) AS maxDate 
       FROM ASSIGNMENT GROUP BY device) m on m.device = a.device 
    RIGHT JOIN Assignment 
     ON (Assignment.device=a.device) 
    GROUP BY a.device, staff, myEndDate, requestDate, contract) 
    AS B WHERE B.rownum > 0;     

第一條語句的偉大工程,如條目(那些具有相同的「設備」標籤)有正確的結束日期。但是,當我根據案例聲明嘗試增加日期時,事情就會出錯。日期遞增但不相同。爲什麼案件陳述會破壞這種性質的事情?

UPDATE: 如果我有一個設備的日期的兩個值作爲這樣的:

2/1/2010 
1/1/2010 

合同= 10

查詢後的各個日期爲:

2/1/2011 
2/1/2012 

回答

0

您將不得不使用GROUP BY條款中的整個CASE聲明。在執行GROUP BY之前,SQL Server不一定會處理SELECT元素,因此定義的別名無法使用。 (Longer explanation here)。

SELECT * FROM (SELECT ROW_NUMBER() 
    OVER (ORDER BY a.device ASC) 
    AS rownum, a.device, staff, requestDate, CONVERT(
    varchar, dateadd(year, 
    (CASE 
     WHEN contract = '10' THEN 1 
     WHEN contract = '20' THEN 2 
     WHEN contract = '30' THEN 3 
     ELSE 3 
     END), m.maxDate) 107) as myEndDate, attribute = 
       REPLACE((SELECT attribute AS [data()] 
       FROM deviceList 
       WHERE device = a.device 
       ORDER BY device FOR xml PATH('')), ' ', ' ') 
    FROM deviceList a 
    INNER JOIN (SELECT device, MAX(requestDate) AS maxDate 
       FROM ASSIGNMENT GROUP BY device) m on m.device = a.device 
    RIGHT JOIN Assignment 
     ON (Assignment.device=a.device) 
    GROUP BY a.device, staff, CONVERT(
    varchar, dateadd(year, 
    (CASE 
     WHEN contract = '10' THEN 1 
     WHEN contract = '20' THEN 2 
     WHEN contract = '30' THEN 3 
     ELSE 3 
     END), m.maxDate) 107), requestDate, contract) 
    AS B WHERE B.rownum > 0;    
+0

這沒有奏效。它沒有給出錯誤,但我仍然得到相同的結果。只是好奇,你是否在你的一個數據庫上進行了驗證?看不出我做錯了什麼,因此我的問題。 – WildBill 2012-01-30 22:59:23

+0

看到上面我澄清我所看到的.... – WildBill 2012-01-30 23:05:29

相關問題