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.
Author |
Topic |
leostrut
Starting Member
3 Posts |
Posted - 2013-03-29 : 02:59:02
|
I have selected serveral field from different tables and create a temp table1 like this:Number loginSystemDate loginTimeA00001 1/2/2013 12:51:57A00001 1/2/2013 12:01:00A00001 1/2/2013 17:01:13A00001 2/2/2013 18:01:13A00002 6/2/2013 7:56:59A00002 6/2/2013 12:00:44A00004 1/2/2013 12:01:01A00004 1/2/2013 17:00:44A00004 1/2/2013 17:59:31However i want to make it like the following table, what i have to do to create a second table from table 1 information?Number loginSystemDate Between12and1pm [1] [2] [3]A00001 1/2/2013 2 12:51:57 12:01:00 17:01:13A00001 2/2/2013 0 18:01:13 null nullA00002 6/2/2013 1 7:56:59 12:00:44 nullA00004 1/2/2013 1 12:01:01 17:00:44 17:59:31 |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-03-29 : 03:30:09
|
[code]SELECT *FROM (SELECT Number, loginSystemDate, logintime, COUNT(CASE WHEN loginTime BETWEEN '12:00:00' AND '13:01:00' THEN loginTime END) over() Between12and1pm, row_number() OVER(PARTITION BY Number, loginSystemDate ORDER BY (SELECT 1)) Seq FROM TempTable )pPIVOT (MAX(logintime) FOR Seq IN ([1], [2], [3]))pvt[/code] |
|
|
leostrut
Starting Member
3 Posts |
Posted - 2013-03-29 : 04:52:15
|
your answer is helpful. After checked the sql, one column between12and1pm is always 27145 in each row.anyone know why? |
|
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-03-29 : 05:02:20
|
oh am sorry.. its a typoCOUNT(CASE WHEN loginTime BETWEEN '12:00:00' AND '13:01:00' THEN loginTime END) over(PARTITION BY Number, loginSystemDate) |
|
|
leostrut
Starting Member
3 Posts |
Posted - 2013-03-31 : 22:27:13
|
wow it works perfect. Can you give me some more advice to make it dynamic because users may login more than 3 times each day. |
|
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
|
|
|
|