Sales Per Hour Report

Sorry to be bringing this thread back up, however I was trying to implement the Sales Per hour Script.
But ended up with

In the script I got

declare @StartDate datetime = convert(varchar(25),'{Start}',126)
declare @EndDate datetime = convert(varchar(25),'{End}',126)
declare @StartHour int = @1 -- set the First Hour you want to track, or supply via report parm
declare @EndHour int = @2  -- set the Last Hour you want to track, or supply via report parm

/************************************/

declare @Hour int = 0
declare @TotalSales money = 0.00

declare @tbl_Sales table
(
[ID]  INT IDENTITY(1,1) NOT NULL 
,[Hour] varchar(5)
,[Amount] decimal(9,2)
,[Tickets] int
,[AvgTx] decimal(5,2)
,[Percent] decimal(5,2)
)

set @Hour = @StartHour

WHILE @Hour < @EndHour+1
BEGIN
INSERT INTO @tbl_Sales
SELECT
  @Hour as [Hour]
, SUM(CASE WHEN (DATEPART(hour,[Date])>=@Hour AND DATEPART(hour,[Date])<@Hour+1)
           THEN [TotalAmount]
           ELSE 0 END) as [Amount]
, COUNT(CASE WHEN (DATEPART(hour,[Date])>=@Hour AND DATEPART(hour,[Date])<@Hour+1)
           THEN 1
           ELSE null END) as [Tickets]
,0,0
FROM [dbo].[Tickets]
WHERE [Date]>=@StartDate AND [Date]<=@EndDate
SET @Hour = @Hour + 1
END

INSERT INTO @tbl_Sales
SELECT 'TTL', sum([Amount]), sum([Tickets]), 0, 0 FROM @tbl_Sales

set @TotalSales = (SELECT [Amount] FROM @tbl_Sales WHERE [Hour]='TTL')

UPDATE @tbl_Sales SET
 [AvgTx] = (CASE WHEN [Tickets]=0 THEN 0 ELSE [Amount]/[Tickets] END)
,[Percent] = [Amount]/@TotalSales * 100

SELECT CASE
WHEN isnumeric([Hour])=1
THEN (CASE WHEN [Hour]<10 THEN '0'+[Hour] ELSE [Hour] END)+':00'
ELSE [Hour]
END as [Hour]
, [Amount], [Tickets], [AvgTx], [Percent]
 FROM @tbl_Sales

any idea where im going wrong?

Restart SambaPOS.

This happens sometimes when you create new Reports that contain @@SQL.

Ahh yep, I thought so, it started working as soon as I restarted. All good :smiley:


I Try What u show Above It Show This Message before Hour is Invalid Column so i change HH also same error that why can u Help Me Please @QMcKay

Youll need to show you report settings

i Try To Fix whole night now show this message again


another one

We wont be able to help you unless you show us your report syntax.

image
image
image
image
that 2 are script i copy above show
i want my report like this5f211a120699075f827a4d9248d4f426673e9d2b

please help me

Hi there,

I was trying

  • Report per Day
  • Report per Hour

In both case I have an error (translated):

Varchar data type conversion to datetime gives a value outside of the interval

It rings a bell but not sure on how to correct it. I think I saw that error when playing around with date time in a WHERE clause. I had to write datetime like that

  • 2020-03-15T14:40:00
    instead of
  • 2020-03-15 14:40:00

So I guess, that error is from this instruction:

(DATEPART(hour,[Date])>=@Hour

But I don’t know how to solve it.