簡體   English   中英

將查詢和子查詢嵌入到sql視圖中(以小時和分鍾為單位的時差)

[英]Embed a query and sub Query within a sql view (diff time in hours and min.)

我想為用戶顯示多久之前添加此記錄。 (例如“ 0天5小時6分”。)因此我需要計算總分。 並做數學。 我在c#中完成了此操作,但現在我需要在sql語法中執行此操作,是否有一種簡單的方法?

更新:感謝@Andomar的回答,在這里我可以將它作為單獨的查詢工作,現在我需要將其添加到calls表的大視圖中。

 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

您可以找到小時的差異,例如:

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

我會把增加的天數留給讀者練習;)

將日期計算放在用戶定義的函數中。 這樣的事情。

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

您需要指定要使用的數據庫系統,但是在sql server(我假設還有許多其他數據庫系統)中,您可以創建一個函數該函數具有與代碼中相同的邏輯,然后可以調用該函數在SQL查詢中

CREATE VIEW 
AS
WITH
  ViewWithMinutes AS (
    SELECT
      TotalMinutes = DATEDIFF(mi, , GETDATE()),
      
    FROM 
  ),
  ViewWithTimeParts AS (
    SELECT
      Minutes = TotalMinutes % 60,
      Hours   = TotalMinutes / 60 % 24,
      Days    = TotalMinutes / 60 / 24,
      
    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
  
FROM ViewWithTimeParts

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM