SQL Server SUM问题

时间:2012-09-21 06:34:32

标签: sql sql-server tsql

我正在尝试浏览一个表并获取mins列的总和,但它总是告诉我mins不是有效的列名

SELECT 
    acs.cid, 
    DATEDIFF (n, acs.StartTime, acs.EndTime ) as prepromomins,
    CASE 
        WHEN (p.multiplier is null) THEN DATEDIFF (n , acs.StartTime, acs.EndTime)
        ELSE (DATEDIFF ( n , acs.StartTime , acs.EndTime ) * p.multiplier) 
    END AS mins 
FROM 
    activecards as acs
    LEFT JOIN Promotions as p 
    ON acs.StartTime > p.StartTime and acs.EndTime < p.EndTime

1 个答案:

答案 0 :(得分:0)

试试这个:

SELECT SUM(DATEDIFF(n, acs.StartTime, acs.EndTime) * ISNULL(p.multiplier,1)) AS TotalMins
FROM activecards as acs
LEFT JOIN Promotions as p 
ON acs.StartTime > p.StartTime and acs.EndTime < p.EndTime