I have the following SQL which works in SQL Server 2005, but I need to replicate the functionality for SQL Server 2000. Unfortunately, the "row_number()" and "with partitioned as" syntaxes don't exist in the older version of SQL Server, so I can't build my tables or run my reports. I tried using the identity function in the first step (building the avg_days table) with the order by clause, but the row numbers are out of order (I want to order by customer_key asc, calendar_date asc, transaction_id asc). Any ideas would be welcomed as I am out of them and need an answer asap.
Thanks!
-------------------------------------------------
-------------------- AVG_DAYS -------------------
-------------------------------------------------
Select distinct A.customer_key, b.transaction_id, c.calendar_dt
into avg_days
From crdw_customer A, crdw_txn_header B, crdw_date_time C
Where A.customer_key = B.customer_key
And B.time_key = C.time_key
-------------------------------------------------
-------------------- AVG_DAYS_1 -----------------
-------------------------------------------------
WITH Partitioned AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY customer_key, calendar_dt ORDER BY customer_key, calendar_Dt) AS RowNumber
FROM avg_days)
SELECT *,
CASE
WHEN RowNumber > 1 THEN 0
ELSE COALESCE(DATEDIFF(DAY, (SELECT MAX(calendar_dt) FROM avg_days WHERE calendar_dt < a.calendar_dt AND customer_key = a.customer_key), a.calendar_dt), 0)
END AS Days_between
into avg_days_1
FROM Partitioned a
Thanks!
-------------------------------------------------
-------------------- AVG_DAYS -------------------
-------------------------------------------------
Select distinct A.customer_key, b.transaction_id, c.calendar_dt
into avg_days
From crdw_customer A, crdw_txn_header B, crdw_date_time C
Where A.customer_key = B.customer_key
And B.time_key = C.time_key
-------------------------------------------------
-------------------- AVG_DAYS_1 -----------------
-------------------------------------------------
WITH Partitioned AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY customer_key, calendar_dt ORDER BY customer_key, calendar_Dt) AS RowNumber
FROM avg_days)
SELECT *,
CASE
WHEN RowNumber > 1 THEN 0
ELSE COALESCE(DATEDIFF(DAY, (SELECT MAX(calendar_dt) FROM avg_days WHERE calendar_dt < a.calendar_dt AND customer_key = a.customer_key), a.calendar_dt), 0)
END AS Days_between
into avg_days_1
FROM Partitioned a