每个客户的总持续时间

时间:2017-10-13 05:44:55

标签: sql sql-server tsql reporting-services

我试图以hh:mm获得每个客户的总持续时间。 这是我的疑问:

SELECT 
    CONVERT(VARCHAR(10), 
            SUM(DATEDIFF(Minute, startdate,EndDate) / 60) 
                OVER (PARTITION BY clientId ORDER BY clientId)) 
       + '.' 
       + RIGHT('00' + CONVERT(VARCHAR(2), 
            SUM(DATEDIFF(Minute, startdate, EndDate)) % 60), 2) 
                (OVER(PARTITION BY clientId ORDER BY clientId))                        
FROM tableX

3 个答案:

答案 0 :(得分:0)

你可以使用它。

SELECT 
    clientId,
    CONVERT(varchar(5), DATEADD(MINUTE, SUM(DATEDIFF(Minute, startdate,EndDate)) OVER(PARTITION BY clientId), '00:00') ,108) 
FROM tableX

答案 1 :(得分:0)

您需要将时间的第一部分转换为字符串:

int

否则,你得到:

  

消息245,级别16,状态1,行6转换时转换失败   varchar值'。'数据类型int。

那是因为.有更大的date type precedence且引擎是。{ 尝试将int转换为SUM,以便+RIGHT)值。第三个参数已经是字符串,因为您正在使用SELECT CONVERT(VARCHAR(10), SUM(DATEDIFF(Minute, startdate,EndDate)) OVER (PARTITION BY clientId ORDER BY clientId) / 60 ) + '.' + RIGHT('00' + CONVERT(VARCHAR(2), SUM(DATEDIFF(Minute, startdate, EndDate)) OVER(PARTITION BY clientId ORDER BY clientId) % 60), 2) FROM tableX 函数,因此无需强制转换。

你可以试试这个:

<xsl:template match="subfigure">
    <xsl:copy-of select="legend"/>
    <xsl:copy>
        <xsl:apply-templates select="node() except legend"/>
    </xsl:copy>
</xsl:template>

答案 2 :(得分:0)

请试试这个:

DECLARE @ClientDuration TABLE (clientId INT NOT NULL, StartDate DATETIME, EndDate DATETIME)

INSERT INTO @ClientDuration VALUES (1, '2017-10-13 09:21:53', '2017-10-13 10:22:53')
INSERT INTO @ClientDuration VALUES (1, '2017-10-14 09:21:53', '2017-10-14 10:22:53')
INSERT INTO @ClientDuration VALUES (1, '2017-10-15 09:21:53', '2017-10-15 10:22:53')

INSERT INTO @ClientDuration VALUES (2, '2017-10-13 08:21:53', '2017-10-13 10:22:53')
INSERT INTO @ClientDuration VALUES (2, '2017-10-14 09:21:53', '2017-10-16 10:22:53')
INSERT INTO @ClientDuration VALUES (2, '2017-10-15 10:21:53', '2017-10-15 10:22:53')

;WITH cte AS (
    SELECT 
        clientId,
        CAST((SUM(DATEDIFF(minute, startdate, enddate)) % 60) AS VARCHAR(3)) AS mins, 
        CAST((SUM(DATEDIFF(minute, startdate, enddate)) - (SUM(DATEDIFF(minute, startdate, enddate)) % 60))/60 AS VARCHAR(3)) AS hrs
    FROM @ClientDuration
    GROUP BY clientId
)

SELECT  clientId, 
    IIF(LEN(c.hrs) = 1, '0' + c.hrs, c.hrs)  + ' H :' + IIF(LEN(c.hrs) = 1, '0' + c.hrs, c.hrs) + ' M'  AS TotalDuration  

FROM cte AS c

输出

clientId    TotalDuration
1           03 H :03 M
2           51 H :51 M