I try to group by my purchases by hour .In this case I lave number hours that I have not any purchase in it. I want check select query and if once of hours (0 1 2 .... 22 23 ) not exist into select query : add a record with default value into @Table .I get this error :
Incorrect syntax near the keyword SELECT
How can i fix this?
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
VALUESkeyword