根据另一个表中的计数更新表

时间:2018-08-21 08:53:10

标签: sql-server

我正在尝试使用另一个表中的日期范围对一个表进行计数。计数还必须与参考号匹配,这是数据格式:

[Rev Old New] (Table 1)
Id   Start        Finish      Amount    Calls
41   2018-01-01   2018-06-01  111.01

[Calls]  (Table 2)
Id   Date         Amount
3    2018-05-05   12.1
41   2018-01-03   11.7
41   2018-06-11   12.9

我对MS SQL还是很陌生,所以为我的基本知识道歉!

所以,我想要[Calls]中的行数,其中Date在[Rev Old New]中的开始日期和完成日期之间,并且两个表中的ID都相同(这是客户参考)

我想用[通话]中的这个值更新[Rev Old New]

到目前为止,这是我目前无法使用的语法,而且可能还远远没有达到正确的语法!

UPDATE [Insight].[dbo].[Rev Old New]. t2
SET [Calls] = 
(SELECT COUNT(CASE WHERE t1.Date BETWEEN t2.[Start] AND t2.[Finish])
FROM [Insight].[dbo].[Calls] t1
WHERE t1.[Id] = t2.[Id])

我得到的错误是:

Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 't2'.
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'WHERE'.

3 个答案:

答案 0 :(得分:1)

您不需要CASE语句,只需一个简单的WHERE就可以了:

UPDATE [Insight].[dbo].[Rev Old New]
SET [Rev Old New].[Calls] = (SELECT COUNT(*) FROM [Insight].[dbo].[Calls] t1
                  WHERE t1.Date BETWEEN [Rev Old New].[Start] AND [Rev Old New].[Finish])

答案 1 :(得分:1)

这可能会帮助

    CREATE TABLE #RevOldNew(Id BIGINT, Start DATETIME,  Finish DATETIME,  Amount BIGINT,  Calls INT)

    CREATE TABLE #Calls(Id BIGINT,[Date] DATETIME, AMOUNT BIGINT)



    INSERT INTO #RevOldNew
    SELECT 1,'2018-06-01','2018-06-15',NULL,NULL UNION ALL
    SELECT 1,'2018-07-16','2018-07-31',NULL,NULL UNION ALL
    SELECT 1,'2018-08-01','2018-08-15',NULL,NULL UNION ALL
    SELECT 1,'2018-08-16','2018-08-31',NULL,NULL UNION ALL

    SELECT 2,'2018-07-01','2018-07-15',NULL,NULL UNION ALL
    SELECT 2,'2018-08-01','2018-08-15',NULL,NULL UNION ALL
    SELECT 2,'2018-08-16','2018-08-31',NULL,NULL UNION ALL

    SELECT 3,'2018-07-16','2018-07-31',NULL,NULL UNION ALL
    SELECT 3,'2018-08-01','2018-08-15',NULL,NULL UNION ALL
    SELECT 3,'2018-08-16','2018-08-31',NULL,NULL 


    INSERT INTO #Calls
    SELECT 1,'2018-07-16',23 UNION ALL
    SELECT 1,'2018-07-21',534 UNION ALL
    SELECT 1,'2018-07-28',456 UNION ALL

    SELECT 1,'2018-08-02',43 UNION ALL
    SELECT 1,'2018-08-11',565 UNION ALL

    SELECT 1,'2018-08-20',56 UNION ALL

    SELECT 2,'2018-07-05',576 UNION ALL

    SELECT 2,'2018-08-22',54 UNION ALL
    SELECT 2,'2018-08-29',676 UNION ALL

    SELECT 3,'2018-07-17',32 UNION ALL
    SELECT 3,'2018-08-15',43


    ;with cte
    As (
        SELECT r.id,r.Start,r.Finish, SUM(c.AMOUNT) Amount, COUNT(c.id) calls
        FROM #RevOldNew r
        LEFT JOIN #Calls c on r.id=c.id and c.Date between r.Start and r.Finish
        Group by r.id,r.Start,r.Finish
    )


    UPDATE r
    SET r.Amount=c.Amount,
            r.Calls=c.calls 
    FROM #RevOldNew r
    JOIN cte c on c.id=r.id and c.Start=r.Start and c.Finish=r.Finish


    SELECT * from #RevOldNew

    DROP TABLE #RevOldNew
    DROP TABLE #Calls

答案 2 :(得分:1)

首先,您应该在表名中避免空格。这不是一个好习惯。

然后解决您的问题的查询是:

更新[Rev Old New] 设置Call​​s =(从[Rev Old New] .id = Calls.id and Calls.date> = [Rev Old New] .Start and Calls.date <= [Rev Old New] .Finish的呼叫中选择count(*)。

其中

从呼叫中选择count(*),其中[Rev Old New] .id = Calls.id and Calls.date> = [Rev Old New] .Start and Calls.date <= [Rev Old New] .Finish

计算[Rev Old New]中ID为[ids]的呼叫中的行,[Rev Old New]中Finish和Start之间的呼叫(包括)中的日期为行

我希望这会有所帮助。