2011-06-15 56 views
2

我想爲用戶顯示多久之前添加此記錄。 (如「0天5小時6分鐘」),所以我需要採取總分鐘數。並做數學。我在c#中做了,但現在我需要用sql語法來做,有沒有簡單的方法來做到這一點?在sql視圖中嵌入查詢和子查詢(以小時和分鐘爲單位的差異時間)

UPDATE: 由於@Andomar答案在這裏我得到了它作爲一個單獨的查詢工作,現在我需要將其添加到呼叫表的大圖..

select case when days > 0 then CAST(Days as varchar(6)) + ' Days ' else + 
     case when hours > 1 and hours < 24 then cast(hours as varchar(6)) + ' hours' 
     when hours > 1 and hours < 24 then '1 hour' 
     else '' 
    end + ' ' + 
    case when minutes > 1 and minutes < 60 then cast(minutes as varchar(6)) + ' minutes' 
     when minutes = 1 then '1 min.' 
     else '' 
    end 
    end as TimeOpen 
    From (
    select datediff(HH, dbo.Calls.CallDate, getdate()) as hours 
    ,  datediff(MI, dbo.Calls.CallDate,getdate()) % 60 as minutes 
    ,  datediff(D, dbo.Calls.CallDate, getdate()) as Days 
    from calls where Status <> 7 and Status <> 4 
    ) as SubQuery 
+0

而你的DBMS是......? – 2011-06-15 21:57:41

+0

微軟SQL Server 2008的 – Ezi 2011-06-15 21:58:23

回答

2

你可以找到像在時間的差異:

datediff(hour, startdate, enddate) 

分鐘的剩餘時間和:

datediff(minutes, startdate, enddate) % 60 

組合,它看起來像:

select cast(datediff(hour, startdate, enddate) as varchar(20)) + ' hours ' + 
     cast(datediff(minutes, startdate, enddate) % 60 as varchar(20) + ' min.' 

要做到條件格式,你可以使用子查詢:

select case when hours > 1 then cast(hours as varchar(6)) + ' hours' 
      when hours > 1 then '1 hour' 
      else '' 
     end + ' ' + 
     case when minutes > 1 then cast(minutes as varchar(6)) + ' minutes' 
      when minutes = 1 then '1 minute' 
      else '0 minutes' 
     end 
from (
     select datediff(hour, startdate, enddate) as hours 
     ,  datediff(minutes, startdate, enddate) % 60 as minutes 
     from YourTable 
     ) as SubQueryAlias 

我會離開加入天作爲練習讀者;)

+0

工作,但日子不在那裏,我在這裏有2000小時..它不能超過23.59小時。我需要一種方式來說明,如果它超過23天,那麼就應該放棄當天並顯示當天的部分。 – Ezi 2011-06-15 22:04:30

+0

@Ezi:你可以使用'case'來檢查一個值,並使用子查詢來防止一遍又一遍地重複'datediff'。正如你所看到的,格式化不是SQL的強項之一。 – Andomar 2011-06-15 22:15:02

+0

謝謝你的幫助,我使它正常工作,現在我需要在視圖中構建它..我無法使它工作。 – Ezi 2011-06-16 16:51:08

1

你需要指定你正在使用的數據庫系統,但是在sql server中(我假設有很多其他的數據庫系統),你可以使用create a function,它和你的代碼具有相同的邏輯,然後你可以c在sql查詢中的所有內容

2

將您的日期計算放入用戶定義的函數中。像這樣的東西。

create function GetDateDiffStr(@FromDate datetime, @ToDate datetime) returns varchar(50) 
as 
begin 
    declare @Ret varchar(50) 
    select @Ret = 
     cast(DayDiff.Value as varchar(10))+case DayDiff.Value when 1 then ' day ' else ' days ' end+ 
     cast(HourDiff.Value as varchar(10))+case HourDiff.Value when 1 then ' hour ' else ' hours ' end+ 
     cast(MinutDiff.Value as varchar(10))+case MinutDiff.Value when 1 then ' minute ' else ' minutes ' end 
    from  (select datediff(mi, @FromDate, @ToDate)) as TotalMinutes(Value) 
    cross apply (select TotalMinutes.Value/(24*60)) as DayDiff(Value) 
    cross apply (select (TotalMinutes.Value - DayDiff.Value*24*60)/60) as HourDiff(Value) 
    cross apply (select TotalMinutes.Value - DayDiff.Value*24*60 - HourDiff.Value*60) as MinutDiff(Value) 
    return @Ret 
end 

並在視圖的字段列表中使用該函數。

select dbo.GetDateDiffStr(YourDateColumn, getdate()) as DateDiffStr 
from YourTable 
1
CREATE VIEW your_view_name 
AS 
WITH 
    ViewWithMinutes AS (
    SELECT 
     TotalMinutes = DATEDIFF(mi, start_date_column, GETDATE()), 
     other_columns 
    FROM your_tables_and_joins 
), 
    ViewWithTimeParts AS (
    SELECT 
     Minutes = TotalMinutes % 60, 
     Hours = TotalMinutes/60 % 24, 
     Days = TotalMinutes/60/24, 
     the_other_columns 
    FROM 
) 
SELECT 
    TimeOpen = CAST(Days AS varchar) + CASE Days WHEN 1 THEN ' day ' ELSE ' days ' END 
      + CAST(Hours AS varchar) + CASE Hours WHEN 1 THEN ' hour ' ELSE ' hours ' END 
      + CAST(Minutes AS varchar) + CASE Minutes WHEN 1 THEN ' minute ' ELSE ' minutes ' END 
    the_other_columns 
FROM ViewWithTimeParts 
相關問題