| Author |
Topic |
|
smithersgs
Starting Member
17 Posts |
Posted - 2008-08-26 : 16:25:28
|
| A table has two fields named Types and Time. There are two different types, Catalog and Application. I want to group them by time (date), but one more. If they have same date I want to have them in the same row such as:row1: Catalog 8/8/08 10:00:01 Application 8/8/08 9:10:12row2: Catalog 8/8/08 11:12:32 Application 8/8/08 12:34:44row3: Catalog 8/8/08 11:13:11row4: Catalog 8/10/08 07:11:32 Application 8/10/08 09:09:22Can anyone help me to write SQL for this purpose?Thanks much, |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-26 : 23:48:42
|
| [code]SELECT MAX(CASE WHEN Types='Category' THEN Types ELSE NULL END), MAX(CASE WHEN Types='Category' THEN Time ELSE NULL END), MAX(CASE WHEN Types='Application' THEN Types ELSE NULL END), MAX(CASE WHEN Types='Application' THEN TimeELSE NULL END) FROM (SELECT ROW_NUMBER() OVER(PARTITION BY Type ORDER BY Time),Types,TimeFROM YourTable)tGROUP BY t.Seq[/code] |
 |
|
|
smithersgs
Starting Member
17 Posts |
Posted - 2008-08-27 : 10:07:32
|
| visakh16,Thanks for the reply. I tried after changing the actual field names and the table, then I got the following error. Can you tell me what they are?Msg 8155, Level 16, State 2, Line 1No column was specified for column 1 of 't'.Msg 207, Level 16, State 1, Line 9Invalid column name 'Seq'.Thanks, |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-27 : 10:09:40
|
quote: Originally posted by smithersgs visakh16,Thanks for the reply. I tried after changing the actual field names and the table, then I got the following error. Can you tell me what they are?Msg 8155, Level 16, State 2, Line 1No column was specified for column 1 of 't'.Msg 207, Level 16, State 1, Line 9Invalid column name 'Seq'.Thanks,
ah sorry missed that partSELECT MAX(CASE WHEN Types='Category' THEN Types ELSE NULL END), MAX(CASE WHEN Types='Category' THEN Time ELSE NULL END), MAX(CASE WHEN Types='Application' THEN Types ELSE NULL END), MAX(CASE WHEN Types='Application' THEN TimeELSE NULL END) FROM (SELECT ROW_NUMBER() OVER(PARTITION BY Type ORDER BY Time) AS Seq,Types,TimeFROM YourTable)tGROUP BY t.Seq |
 |
|
|
smithersgs
Starting Member
17 Posts |
Posted - 2008-08-27 : 12:22:30
|
| Good, thanks again. I could run it. What does the MAX funtion do in your query? Because that is not exactly what I wanted. Please take a look at the result below. There are 4 Catalogs on 5/12 and 12 Applications on the same day. I want to have 8 NULL in the Catalog columns. That is, the Catalog record of 5/13 started from the same row of Application reacord of 5/13. Result for your query:Catalog 5/12/2008 1:34:47 PM Application 5/12/2008 1:47:44 PMCatalog 5/12/2008 2:22:05 PM Application 5/12/2008 10:15:04 PMCatalog 5/12/2008 4:02:24 PM Application 5/12/2008 10:57:41 PMCatalog 5/12/2008 6:39:08 PM Application 5/12/2008 11:08:23 PMCatalog 5/13/2008 11:07:21 AM Application 5/12/2008 3:29:23 PMCatalog 5/13/2008 12:34:02 PM Application 5/12/2008 3:45:31 PMCatalog 5/13/2008 2:33:34 PM Application 5/12/2008 5:56:58 PMCatalog 5/13/2008 3:15:25 PM Application 5/12/2008 7:08:09 PMCatalog 5/13/2008 8:20:10 PM Application 5/12/2008 7:33:39 PMCatalog 5/13/2008 9:14:45 PM Application 5/12/2008 8:46:53 PMCatalog 5/14/2008 10:36:28 AM Application 5/12/2008 9:08:01 PMCatalog 5/14/2008 4:13:31 PM Application 5/12/2008 9:19:13 PMCatalog 5/14/2008 4:32:28 PM Application 5/13/2008 1:24:31 PMCatalog 5/15/2008 10:55:50 AM Application 5/13/2008 10:48:11 PMCatalog 5/15/2008 2:15:14 AM Application 5/13/2008 11:25:19 AMWhat I need:Catalog 5/12/2008 1:34:47 PM Application 5/12/2008 1:47:44 PMCatalog 5/12/2008 2:22:05 PM Application 5/12/2008 10:15:04 PMCatalog 5/12/2008 4:02:24 PM Application 5/12/2008 10:57:41 PMCatalog 5/12/2008 6:39:08 PM Application 5/12/2008 11:08:23 PMNULL NULL Application 5/12/2008 3:29:23 PMNULL NULL Application 5/12/2008 3:45:31 PMNULL NULL Application 5/12/2008 5:56:58 PM... Application 5/12/2008 7:08:09 PM... Application 5/12/2008 7:33:39 PM... Application 5/12/2008 8:46:53 PM... Application 5/12/2008 9:08:01 PM... Application 5/12/2008 9:19:13 PMCatalog 5/13/2008 11:07:21 AM Application 5/13/2008 1:24:31 PMCatalog 5/13/2008 12:34:02 PM Application 5/13/2008 10:48:11 PMCatalog 5/13/2008 2:33:34 PM Application 5/13/2008 11:25:19 AMThanks, |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-27 : 12:49:55
|
| How will the NULLs be coming ? can you explain that with some table data? |
 |
|
|
smithersgs
Starting Member
17 Posts |
Posted - 2008-08-27 : 13:39:58
|
| What I meant is that is what I wanted. I need your more help. How can I get those NULL values there like that? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-27 : 13:48:42
|
quote: Originally posted by smithersgs What I meant is that is what I wanted. I need your more help. How can I get those NULL values there like that?
Ok. But first show the table data and explain how you think NULL values will come in output. |
 |
|
|
smithersgs
Starting Member
17 Posts |
Posted - 2008-08-28 : 11:36:06
|
| Data looks like this:Catalog 5/12/2008 1:34:47 PMApplication 5/12/2008 1:47:44 PMCatalog 5/12/2008 2:22:05 PMApplication 5/12/2008 3:29:23 PMApplication 5/12/2008 3:45:31 PMCatalog 5/12/2008 4:02:24 PMCatalog 5/12/2008 6:39:08 PMApplication 5/12/2008 5:56:58 PMApplication 5/12/2008 7:33:39 PMApplication 5/12/2008 7:08:09 PMApplication 5/12/2008 8:46:53 PMApplication 5/12/2008 9:08:01 PMApplication 5/12/2008 9:19:13 PMApplication 5/12/2008 10:15:04 PMApplication 5/12/2008 10:57:41 PMApplication 5/12/2008 11:08:23 PMApplication 5/13/2008 5:09:58 AMApplication 5/13/2008 7:39:41 AMCatalog 5/13/2008 11:07:21 AMApplication 5/13/2008 11:25:19 AMCatalog 5/13/2008 12:34:02 PMThere is no NULL. I am not sure what you meant by NULL, but hope this helps.Thanks, |
 |
|
|
|