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 |
|
thomson
Starting Member
9 Posts |
Posted - 2009-03-05 : 16:09:22
|
| I am looking for a solution. convert one column to row's title,please see 2 tables belowTable 1userid name001 Geoge 002 John....Table 2calendar2009-02-032009-02-04....The final table I want to looks likeuserid name 2009-02-03 2009-02-04001 Geoge 002 John...Thanks |
|
|
Skorch
Constraint Violating Yak Guru
300 Posts |
Posted - 2009-03-05 : 16:21:08
|
| Research the PIVOT functionSome days you're the dog, and some days you're the fire hydrant. |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-03-05 : 16:25:36
|
| [code]Declare @T table(userid char(4),names varchar(20))Insert @Tselect '001', 'Geoge' union all Select '002', 'John'Declare @M table(calendar datetime)Insert @MSelect '2009-02-03' union allSelect '2009-02-04'Select Z.Userid,Z.names,r.[2009-02-03],r.[2009-02-04]from(Select Userid,names from @T)Zouter apply(Select Max(Case when Calendar = '2009-02-03' then '' Else Null end)as [2009-02-03],Max(Case when Calendar = '2009-02-04' then '' Else Null end)as [2009-02-04]from @M)r[/code]If you want dynamic,you have to search for dynamic cross tab |
 |
|
|
thomson
Starting Member
9 Posts |
Posted - 2009-03-05 : 17:13:22
|
| I have a SQL SERVER 2005 table with 3 columns: Operator, Shift, and Date. Sample rows are : Operator Shift Date John Early Oct. 1, 2008 Mary Late Oct. 1, 2008 John Late Oct. 2, 2008 Mary Early Oct. 2, 2008 How can i use PIVOT or Crosstab to create a SQL SERVER 2005 report that lists ALL the days of a month as columns and the operators as rows - one row for each operator. More importantly in the row-column intersection, I will like to see Shifts instead of aggregate data (as the shift field is non numeric). I want something like this: Oct 1 Oct 2 Oct3 ..........John Early Late Mary Late EarlyThanks all |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-03-05 : 23:56:38
|
| try this oneusing pivot select operator, [2008-oct-1],[2008-oct-2],..........from tablenamepivot (max(shift) for date in ([2008-oct-1],[2008-oct-2],.......))pvt |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-03-05 : 23:57:38
|
| by dynamic cross tabdeclare @tbl table(Operator varchar(32),Shift varchar(32),Date datetime)insert into @tbl select 'John', 'Early','2008-oct-1' insert into @tbl select 'Mary', 'Late', '2008-oct-1' insert into @tbl select 'John', 'Late','2008-oct-2'insert into @tbl select 'Mary', 'Early', '2008-oct-2' select operator,max(case when date = '2008-oct-1' then shift end) as '2008-oct-1',max(case when date = '2008-oct-2' then shift end) as '2008-oct-2'from @tblgroup by operatorsee this linkhttp://sqlblogcasts.com/blogs/madhivanan/archive/2007/08/27/dynamic-crosstab-with-multiple-pivot-columns.aspx |
 |
|
|
|
|
|
|
|