2014-01-31 69 views
0

我想我的觀點,選擇最近的「手工處理」續約日期,但無法找到如何把MAX功能在查詢可以一MAX在case語句也有CONVERT被合併(VARCHAR

ALTER VIEW [dbo].[cb_TrainingFar] AS 
      SELECT 
       ISNULL(cs_facilities.guid, '00000000-0000-0000-0000-000000000000') AS [FacilityGuid], 
       '' as DART_ALL, 
       ISNULL(cs_facilities.name,'') as [Facility name], 
       ISNULL (ct_workers.forenames,'') + ',' + ISNULL (ct_workers.surname,'') AS 'Name of worker/volunteer', 
       ISNULL (ct_workers.startDate, 0) As 'Start Date', 
        CASE when ct_qualificationTypes.type = 
         'Manual Handling' 
         THEN CONVERT (varchar, ct_qualifications.renewalDate) 
        ELSE 'Not completed' 
       END 'Manual Handling', 
+0

我認爲你真的應該也加上你的'FROM'-和'WHERE'-子句 – DrCopyPaste

+0

max是一個聚合函數。你有沒有跟隨最大值? – xQbert

+0

ALTER VIEW [DBO]。[cb_TrainingFar] AS SELECT ISNULL (cs_facilities.guid, '00000000-0000-0000-0000-000000000000')AS [FacilityGuid], '' 作爲DART_ALL, ISNULL(cs_facilities.name, '')as [工具名稱], ISNULL(ct_workers.forenames,'')+','+ ISNULL(ct_workers.surname,'')AS'工人/志願者姓名', ISNULL(ct_workers.startDate,0 ) '開始日期', CASE ct_qualificationTypes.type WHEN '手工處理' 然後將其轉換(VARCHAR,ct_qualifications.renewalDate) ELSE '未完成' END '手工處理',請從添加*組ct_workers – user3257545

回答

0

這裏的總體思路是你應該能夠制定出具體細節:根據您我

0

select case when maxdate > getdate() then 'Fred' else 'Wilma' end Flinstone 
from 
(select id, max(datefield) maxdate 
from sometable 
group by id) temp 

GETDATE()是SQL Server的功能,您沒有指定數據庫。 nputs

ALTER VIEW [dbo].[cb_TrainingFar] AS 
SELECT 
ISNULL(cs_facilities.guid, '00000000-0000-0000-0000-000000000000') AS [FacilityGuid], 
'' as DART_ALL, 
ISNULL(cs_facilities.name,'') as [Facility name], 
ISNULL (ct_workers.forenames,'') + ',' + ISNULL (ct_workers.surname,'') AS 'Name of worker/volunteer', 
ISNULL (ct_workers.startDate, 0) As 'Start Date', 
CASE 
    ct_qualificationTypes.type 
    WHEN 'Manual Handling' 
    THEN CONVERT (varchar, MAX(ct_qualifications.renewalDate)) 
    ELSE 'Not completed' END AS 'Manual Handling' 
    FROM ct_workers 


    ... 
    GROUP BY ct_qualificationstype 
+0

關閉,但仍然獲得兩個'手動處理「一個比另一個更老。想要嘗試讓系統有效地忽略舊記錄並給我最近的'更新日期'的記錄 – user3257545

+0

在這種情況下,您需要放入一個內部查詢,該查詢按'ct_qualificationstype'分組並獲取'MAX( ct_qualifications.renewalDate)' – DhruvJoshi

0

下面是一個使用窗口函數的一種方法:

ALTER VIEW [dbo].[cb_TrainingFar] AS 
     SELECT 
      ISNULL(cs_facilities.guid, '00000000-0000-0000-0000-000000000000') AS [FacilityGuid], 
      '' as DART_ALL, 
      ISNULL(cs_facilities.name,'') as [Facility name], 
      ISNULL (ct_workers.forenames,'') + ',' + ISNULL (ct_workers.surname,'') AS 'Name of worker/volunteer', 
      ISNULL (ct_workers.startDate, 0) As 'Start Date', 
       CASE when ct_qualificationTypes.type = 
        'Manual Handling' 
        THEN CONVERT(varchar(255), max(ct_qualifications.renewalDate) over()) 
       ELSE 'Not completed' 
      END 'Manual Handling', 

不過,我懷疑你的問題,需要更好地理解查詢。查詢的其餘部分是什麼樣的?順便說一句,請不要使用varchar而不是長度。默認長度根據上下文而變化,並且依賴於默認長度足夠長是相當危險的。

+0

查詢正在查看員工記錄以瞭解員工是否已完成各種培訓課程,如果有的話,他們最近的更新日期是什麼,因爲每次他們重新參加課程時都會有新記錄 – user3257545

+0

@ user3257545。 。 。如圖所示,您將爲每個續訂日期分別獲取一行,而不是每個員工一行。你不能在'select'子句中解決這個問題。 –

+0

我確實擔心可能是這樣,感謝您對此的幫助,幫助我更多瞭解了更多的東西 – user3257545