Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to Select INTO

Author  Topic 

tendulkar
Starting Member

9 Posts

Posted - 2007-04-20 : 14:24:51
[code]WITH Partitioned AS
(
SELECT *, ROW_NUMBER() OVER (PARTITION BY cu_id, date ORDER BY cu_id, date) AS RowNumber
FROM @Table
)
SELECT
*,
CASE
WHEN RowNumber > 1 THEN 0
ELSE COALESCE(DATEDIFF(DAY, (SELECT MAX(date) FROM @Table WHERE date < a.date AND cu_id = a.cu_id), a.date), 0)
END AS Days_between
FROM Partitioned a[/code]

Please let me know how I can write the output of this statement into a table.

Thanks.

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2007-04-20 : 14:52:43
Refer to Books Online, add INTO <table name>:
WITH Partitioned AS
(
SELECT *, ROW_NUMBER() OVER (PARTITION BY cu_id, date ORDER BY cu_id, date) AS RowNumber
FROM @Table
)
SELECT
*,
CASE
WHEN RowNumber > 1 THEN 0
ELSE COALESCE(DATEDIFF(DAY, (SELECT MAX(date) FROM @Table WHERE date < a.date AND cu_id = a.cu_id), a.date), 0)
END AS Days_between
INTO
#Temp

FROM Partitioned a
Go to Top of Page
   

- Advertisement -