2

我正在尝试创建一个查询,以便为我提供多行测试数据的周转时间(以分钟为单位)。

该表的简洁版本是:

测试名称、开始日期时间、结束日期时间

我正在寻找一个可以给我输出的查询,例如:

Distinct TestName 
, StartDate[not time]
, Count(rows) as Total
, Count(rows where datediff(minute, StartDateTime, EndDateTime) <=60) as NonBreach
, Count(rows where datediff(minute, StartDateTime, EndDateTime) >60) as Breach
, Count(rows where datediff(minute, StartDateTime, EndDateTime) >60) / Count(rows) as BreachRate

这个原则甚至可能吗?

任何方向将不胜感激。

4

3 回答 3

4

你可以使用这样的东西:

SELECT  TestName,
        CAST(StartdateTime AS DATE) AS StartDate,
        COUNT(*) AS Total,
        COUNT(CASE WHEN DATEDIFF(MINUTE, StartDateTime, EndDateTime) <= 60 THEN 1 END) AS NonBreach,
        COUNT(CASE WHEN DATEDIFF(MINUTE, StartDateTime, EndDateTime) > 60 THEN 1 END) AS Breach,
        1.0 * COUNT(CASE WHEN DATEDIFF(MINUTE, StartDateTime, EndDateTime) > 60 THEN 1 END) / COUNT(1) AS BreachRate
FROM    YourTable
GROUP BY TestName, CAST(StartdateTime AS DATE)

尽管根据您的 DBMS,您可能需要使用不同的方法从日期中删除时间。


从日期中删除时间:


SQL-Server 2008及更高版本:

SELECT  CAST(CURRENT_TIMESTAMP AS DATE)

SQL-Server 2005及更早版本

SELECT  DATEADD(DAY, DATEDIFF(DAY, 0, CURRENT_TIMESTAMP), 0)

MySQLSQLite

SELECT  DATE(CURRENT_TIMESTAMP)

甲骨文

SELECT  TRUNC(CURRENT_TIMESTAMP)

PostgreSQL

SELECT  CURRENT_TIMESTAMP::DATE

于 2013-02-05T18:13:33.187 回答
2

使用 SQL Server 2008 语法(其他数据库对datediffand的方法略有不同cast(... as date)

select  TestName 
,       cast(StartDateTime as date) as StartDate
,       count(*) as Total
,       sum(case when datediff(minute, StartDateTime, EndDateTime) <= 60 
            then 1 end) as NonBreach
,       sum(case when datediff(minute, StartDateTime, EndDateTime) > 60 
            then 1 end) as Breach
,       1.0 * sum(case when datediff(minute, StartDateTime, EndDateTime) > 60 
            then 1 end) / count(*) as BreachRate
from    YourTable
group by
        TestName 
,       cast(StartDateTime as date)
于 2013-02-05T18:13:16.573 回答
0

据我所知,这样做是不可能的。也许你可以做这样的事情:

Select TestName 
    , StartDate[not time]
    , Count(rows) as Total
    , SUM(CASE WHEN  datediff(minute, StartDateTime, EndDateTime) <=60 THEN 1 ELSE 0 END ) as NonBreach
    , SUM(CASE WHEN datediff(minute, StartDateTime, EndDateTime) >60 THEN 1 ELSE 0 END ) as Breach
于 2013-02-05T18:15:49.603 回答