0

I have a table with individual transactions. Each row represents one car wash.
I would like to know how many washes I have done in the past 365 days.

This is very simple:

Select sum(st.net_sum) as revenue
from dbo.sales_transactions as st 
where st.date BETWEEN adddate(day, -366, getdate()) AND getdate()

However, I would like to see this query for each day of the past 365 days. If I do this e.g. as an iterated query, the query takes a very long time (in my case over 30 seconds, because I also have to work with some joins).

So I would like to do this query once a day and save the output "revenue" in a new table.

How to do this so that this is updated daily? Do I have to write a PHP script that is called daily or can I do this directly in MSSQL?

The original query for number of washes is:

DECLARE @start_date DATE = '2022-03-29';
DECLARE @end_date DATE = '2023-03-29';

WITH AllDays
AS ( 
    SELECT   @start_date AS [Date]
    UNION ALL
    SELECT DATEADD(DAY, 1, [Date])
    FROM AllDays
    WHERE [Date] < @end_date
)
,   WashData as (
        SELECT
            count(st.date) as NumberOfWashes,
            cast(st.date as date) as DayOfWashes
        FROM 
            POS.dbo.sales_transaction_line_item as stli
            join POS.dbo.sales_transaction as st on st.sales_transaction_id = stli.fk_sales_transaction
            join POS.dbo.sales_item as si on si.sales_item_id = stli.fk_sales_item
        WHERE
            st.fk_sales_status <> 3
            and si.fk_sales_item_type = 1
            and st.date BETWEEN @start_date and @end_date
        Group by
            cast(st.date as date)
    )
SELECT 
    [Date]
    ,NumberOfWashes
    ,sum(NumberofWashes) over (order by [Date] rows between 365 preceding AND current row) as Window365
FROM   AllDays ad
left join WashData wd on ad.[Date] = wd.DayOfWashes
ORDER BY [Date] desc
OPTION (MAXRECURSION 0)

This takes not much time: 00:00:00.2413543

The query for Sum of Revenue however takes much longer:

DECLARE @start_date DATE = '2022-03-29';
DECLARE @end_date DATE = '2023-03-29';

WITH AllDays
AS (
    SELECT   @start_date AS [Date]
    UNION ALL
    SELECT DATEADD(DAY, 1, [Date])
    FROM AllDays
    WHERE [Date] < @end_date
)
,   WashData as (
        SELECT
            sum(st.sum_net) as NumberOfWashes,
            cast(st.date as date) as DayOfWashes
        FROM 
            POS.dbo.sales_transaction_line_item as stli
            join POS.dbo.sales_transaction as st on st.sales_transaction_id = stli.fk_sales_transaction
            join POS.dbo.sales_item as si on si.sales_item_id = stli.fk_sales_item
        WHERE
            st.fk_sales_status <> 3
            and si.fk_sales_item_type = 1
            and st.fk_payment_method <> 4
            and st.date BETWEEN @start_date and @end_date
        Group by
            cast(st.date as date)
    )
SELECT 
    [Date]
    ,NumberOfWashes
    ,sum(NumberofWashes) over (order by [Date] rows between 365 preceding AND current row) as Window365
FROM   AllDays ad
left join WashData wd on ad.[Date] = wd.DayOfWashes
ORDER BY [Date] desc
OPTION (MAXRECURSION 0)

That takes much longer: 00:01:19.7617031

siggemannen
  • 3,884
  • 2
  • 6
  • 24
  • 1
    This smells like an [XY Problem](//xyproblem.info). Why not performance tune the query if it's slow? Sure, you could store the data into a table, which would simply be an `INSERT`, but if a query such as the above is as slow as you suggest it is, then it appears that you have a poorly performing instance; does the query have any indexing to help it achieve its goal? – Thom A Mar 29 '23 at 12:44
  • The database normally has a very good performance. I just included here a very simplified query to show the basics. The hole query for my problem is much bigger and using this in iteration took exactly 37 seconds. For a query I would like to use in a dashboard 100 times a day. this is not even close to an option. Every query above 0.5 seconds is not applicable. Thats why I would like to use a new table to make this as quick as possible. – Georg Huber Mar 29 '23 at 13:00
  • 1
    Can you show us the *actual* query you have and [share the query plan](https://www.brentozar.com/pastetheplan/)? – Thom A Mar 29 '23 at 13:11

0 Answers0