Our Client want to get a list for customer orders, but the order count need sort by time, if customer change the order name, it will count separately. The sample data as below:
CREATE TABLE #D(ID INT, Name char(2),adddate datetime) INSERT into #D SELECT 1,'AA','1-1-2000' UNION SELECT 1,'AA','1-2-2000' UNION SELECT 1,'CC','1-3-2000' UNION SELECT 1,'AA','1-4-2000' UNION SELECT 1,'AA','1-5-2000' UNION SELECT 1,'AA','1-6-2000' UNION SELECT 1,'BB','1-7-2000' UNION SELECT 2,'AA','1-8-2000' UNION SELECT 2,'CC','1-9-2000' UNION SELECT 2,'AA','1-10-2000' UNION SELECT 2,'AA','1-11-2000' UNION SELECT 2,'BB','1-12-2000' UNION SELECT 2,'AA','1-13-2000' SELECT * FROM #D ORDER BY 3 --DROP TABLE #D
the results will like this:
SELECT 1 AS ID,'AA' AS Name,2 AS Count,'2000-01-01 00:00:00.000' AS StartDate UNION SELECT 1,'CC',1,'2000-01-03 00:00:00.000' UNION SELECT 1,'AA',3,'2000-01-04 00:00:00.000' UNION SELECT 1,'BB',1,'2000-01-07 00:00:00.000' UNION SELECT 2,'AA',1,'2000-01-08 00:00:00.000' UNION SELECT 2,'CC',1,'2000-01-09 00:00:00.000' UNION SELECT 2,'AA',2,'2000-01-10 00:00:00.000' UNION SELECT 2,'BB',1,'2000-01-12 00:00:00.000' UNION SELECT 2,'AA',1,'2000-01-13 00:00:00.000'
Could you please help how to query it to get results? Thanks!