sql - How can I create a Date Range from a single column? -
i trying query database has these params:
transaction date, user email address
the resultant table want these params:
email address, datediff
for datediff, want find difference between earliest transaction date , latest transaction date.
how go doing this?
my code (clearly not correct)
select [email address], datediff(day, [transaction date],[transaction date]) 'datediff' [db].[table] order [datediff] desc
you need aggregation minimum , maximum values each email, before doing datediff()
:
select [email address], datediff(day, min([transaction date]), max([transaction date])) 'datediff' [db].[table] group [email address] order 2 desc;
Comments
Post a Comment