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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Need help with the query.

Author  Topic 

NguyenL71
Posting Yak Master

228 Posts

Posted - 2009-12-17 : 19:08:09

SQL 2005.
How can I get desire results below. I cannot seem to figure this out.
Any help is greatly appreciate.


IF OBJECT_ID('Tempdb.dbo.#Temp', 'u') IS NOT NULL
DROP TABLE #Temp
GO
CREATE TABLE #Temp
(
UserHistoryId INT NULL,
UserId VARCHAR(6) NULL,
EventDate DATETIME NULL
)
GO


INSERT INTO dbo.#Temp([UserHistoryID],[UserId],[EventDate])VALUES (664, 'User1', CONVERT(DATETIME, 0x00008eac00000000))
INSERT INTO dbo.#Temp([UserHistoryID],[UserId],[EventDate])VALUES (1014, 'User1', CONVERT(DATETIME, 0x00009c2600ef152a))
INSERT INTO dbo.#Temp([UserHistoryID],[UserId],[EventDate])VALUES (1940, 'User1', CONVERT(DATETIME, 0x00009ce200000000))
INSERT INTO dbo.#Temp([UserHistoryID],[UserId],[EventDate])VALUES (1941, 'User1', CONVERT(DATETIME, 0x00009ce200000000))
INSERT INTO dbo.#Temp([UserHistoryID],[UserId],[EventDate])VALUES (1942, 'User1', CONVERT(DATETIME, 0x00009ce200000000))
INSERT INTO dbo.#Temp([UserHistoryID],[UserId],[EventDate])VALUES (1943, 'User1', CONVERT(DATETIME, 0x00009ce200000000))
INSERT INTO dbo.#Temp([UserHistoryID],[UserId],[EventDate])VALUES (1944, 'User1', CONVERT(DATETIME, 0x00009ce200000000))
go

SELECT *
FROM #Temp;
GO


-- Result want:
UserHistoryId UserId EventDate
------------- ------ -----------------------
664 User1 2000-01-01 00:00:00.000
1014 User1 2009-06-12 14:30:28.407
1944 User1 2009-12-17 00:00:00.000

cat_jesus
Aged Yak Warrior

547 Posts

Posted - 2009-12-17 : 19:17:57
what is the selection criteria? top 3?

Select top 3 * from #temp



An infinite universe is the ultimate cartesian product.
Go to Top of Page

NguyenL71
Posting Yak Master

228 Posts

Posted - 2009-12-17 : 19:26:06
Sorry, my bad data. Here is the new data and desire results.
Top 3 won't work for me. Thanks.

IF OBJECT_ID('Tempdb.dbo.#Temp', 'u') IS NOT NULL
DROP TABLE #Temp
GO
CREATE TABLE #Temp
(
UserHistoryId INT NULL,
UserId VARCHAR(6) NULL,
EventDate DATETIME NULL
)
GO


INSERT INTO dbo.#Temp([UserHistoryID],[UserId],[EventDate])VALUES (664, 'User1', CONVERT(DATETIME, 0x00008eac00000000))
INSERT INTO dbo.#Temp([UserHistoryID],[UserId],[EventDate])VALUES (1014, 'User1', CONVERT(DATETIME, 0x00009c2600ef152a))
INSERT INTO dbo.#Temp([UserHistoryID],[UserId],[EventDate])VALUES (1940, 'User1', '2009-12-17 16:10:09.13')
INSERT INTO dbo.#Temp([UserHistoryID],[UserId],[EventDate])VALUES (1941, 'User1', '2009-12-17 16:11:11.21')
INSERT INTO dbo.#Temp([UserHistoryID],[UserId],[EventDate])VALUES (1942, 'User1', '2009-12-17 16:16:14.21')
INSERT INTO dbo.#Temp([UserHistoryID],[UserId],[EventDate])VALUES (1943, 'User1', '2009-12-17 16:18:10.20')
INSERT INTO dbo.#Temp([UserHistoryID],[UserId],[EventDate])VALUES (1944, 'User1', '2009-12-17 16:20:47.830')
go

SELECT *
FROM #Temp;
GO

UserHistoryId UserId EventDate
------------- ------ -----------------------
664 User1 2000-01-01 00:00:00.000
1014 User1 2009-06-12 14:30:28.407
1940 User1 2009-12-17 16:10:09.130
1941 User1 2009-12-17 16:11:11.210
1942 User1 2009-12-17 16:16:14.210
1943 User1 2009-12-17 16:18:10.200
1944 User1 2009-12-17 16:20:47.830


-- Result want:
UserHistoryId UserId EventDate
------------- ------ -----------------------
664 User1 2000-01-01 00:00:00.000
1014 User1 2009-06-12 14:30:28.407
1944 User1 2009-12-17 16:20:47.830


quote:
Originally posted by cat_jesus

what is the selection criteria? top 3?

Select top 3 * from #temp



An infinite universe is the ultimate cartesian product.

Go to Top of Page

cat_jesus
Aged Yak Warrior

547 Posts

Posted - 2009-12-17 : 19:45:42
Your result set seems pretty random. Why those three records and not three others or 4 or 5?

An infinite universe is the ultimate cartesian product.
Go to Top of Page
   

- Advertisement -