关键字 SELECT 附近的语法不正确

Incorrect syntax near the keyword SELECT

我尝试按 小时 对我的购买进行分组。在这种情况下,我有几个小时,我没有购买任何东西。我想检查 select 查询,如果每小时一次 (0 1 2 .... 22 23 ) 不存在于 select 查询中:添加默认记录价值进入 @Table 。我得到这个错误:

Incorrect syntax near the keyword SELECT

我该如何解决这个问题?

    DECLARE @Table AS TABLE (Price DECIMAL, NumberOfPhurchase INT, Hour INT);
    DECLARE @i AS INT = 0;
    DECLARE @j AS INT = 0;

    INSERT INTO @Table (Price,NumberOfPhurchase,Hour)
    VALUES
      (
        SELECT SUM(p.Price)    ,
           COUNT(p.Price)  ,
           DATEPART(HOUR, p.IssueDate)
    FROM   dbo.Payments    AS p
    WHERE  p.[state] = 6
           AND p.Transactionsuccess = 1
           AND (p.ReserveType = @ReserveType OR @ReserveType = 0)
           AND p.IssueDate >= @StartDate
           AND p.IssueDate <= @EndDate
    GROUP BY
           DATEPART(HOUR, p.IssueDate)

      )


    WHILE @i <= 23
    BEGIN
        SET @j =  
        SELECT COUNT(*) FROM @Table 
        WHERE  @Table.Hour = @i;
        IF @j = 0
        BEGIN
            INSERT INTO @Table
              (
                Price,
                NumberOfPhurchase,
                Hour
              )
            VALUES
              (
                0,
                0,
                @j
              )
        END
        SET @i=@i+1;
    END
    SELECT
        @Table.Price,
        @Table.NumberOfPhurchase,
        @Table.Hour
    FROM
        @Table

只需删除 VALUES 这个词

INSERT INTO @Table (Price,NumberOfPhurchase,Hour)
    SELECT SUM(p.Price)    ,
       COUNT(p.Price)  ,
       DATEPART(HOUR, p.IssueDate)
    FROM   dbo.Payments    AS p
    WHERE  p.[state] = 6
       AND p.Transactionsuccess = 1
       AND (p.ReserveType = @ReserveType OR @ReserveType = 0)
       AND p.IssueDate >= @StartDate
       AND p.IssueDate <= @EndDate
    GROUP BY
       DATEPART(HOUR, p.IssueDate)

您可以使用另一个 INSERT .. SELECT 语句填充零值:

  WHILE @i <= 23
  BEGIN
       INSERT INTO @Table(Price,NumberOfPhurchase,Hour)
       SELECT  0,0,@i
       WHERE @i NOT IN (SELECT Hour FROM @Table)
       SET @i=@i+1
  END

如果这是 TSQL,那么

SET @j =  
SELECT COUNT(*) FROM @Table

尝试:

SELECT @j = COUNT(*) FROM @Table 

我能够使用下面的代码 (SQL Server 2016) 实现此功能:

    DECLARE @Table AS TABLE (Price DECIMAL, NumberOfPhurchase INT, Hour INT);
    DECLARE @i AS INT = 0;
    DECLARE @j AS INT = 0; 

    INSERT INTO @Table (Price,NumberOfPhurchase,Hour)
    SELECT SUM(p.Price)    ,
           COUNT(p.Price)  ,
           DATEPART(HOUR, p.IssueDate)
    FROM   dbo.Payments    AS p
    WHERE  p.[state] = 6
           AND p.Transactionsuccess = 1
           AND (p.ReserveType = @ReserveType OR @ReserveType = 0)
           AND p.IssueDate >= @StartDate
           AND p.IssueDate <= @EndDate
    GROUP BY
           DATEPART(HOUR, p.IssueDate)

    WHILE @i <= 23
    BEGIN
        SELECT @j =  
         COUNT(*) FROM @Table 
        WHERE  Hour = @i;
        IF @j = 0
        BEGIN
            INSERT INTO @Table
              (
                Price,
                NumberOfPhurchase,
                Hour
              )
            VALUES
              (
                0,
                0,
                @j
              )
        END
        SET @i=@i+1;
    END
    SELECT
       Price,
       NumberOfPhurchase,
       Hour
    FROM
        @Table

为了完整起见,这里是一个非程序性的解决方案。如果你打算使用数据库,你至少应该明白我在这里做什么:

INSERT INTO @Table (Price,NumberOfPurchase,Hour)
SELECT ISNULL(SUM(p.Price),0)    ,
       ISNULL(COUNT(p.Price),0)  ,
       H.HourNumber
FROM   
(
VALUES
(0),(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),
(13),(14),(15),(16),(17),(18),(19),(20),(21),(22),(23)
) H (HourNumber)
LEFT OUTER JOIN dbo.Payments    AS p
ON H.HourNumber = DATEPART(HOUR, p.IssueDate)
AND p.[state] = 6
AND p.Transactionsuccess = 1
AND (p.ReserveType = @ReserveType OR @ReserveType = 0)
AND p.IssueDate >= @StartDate
AND p.IssueDate <= @EndDate
GROUP BY
DATEPART(HOUR, p.IssueDate)