Author |
Topic |
sureshsmanian
Starting Member
31 Posts |
Posted - 2012-11-10 : 05:00:59
|
HiI have a table TTNew(ID,Employee,WDay,Schedule1,Schedule2,Schedule3) with the following records:---------------------------------------------------------Id Employee WDay Schedule1 Schedule2 Schedule3---------------------------------------------------------2 Kiran Sun Read Watch Play3 Kiran Mon Office Travel Meet4 Kiran Tue Personal Travel Meet5 Kiran Wed Board Office Conference6 Mohd Sun Meet Market Session7 Mohd Mon Office Market Travel8 Mohd Tue Travel Market Conference9 Mohd Wed Personal Meet Watch---------------------------------------------------------I need the output in the following manner:that is, against the employee and day the duties are needed to be grouped.-----------------------------------------------------------Sno Employee Sun Mon Tue ..... -----------------------------------------------------------1 Kiran Read Watch Play | Office Travel Meet | ..... 2 Mohd Meet Market Session | Office Market Travel | ..... -- ... -- ...------------------------------------------------------------For your information, query to create table and records---CREATE TABLE [dbo].[ttnew]( [Id] [bigint] IDENTITY(1,1) NOT NULL, [Employee] [nvarchar](50) NULL, [WDay] [nvarchar](20) NULL, [Schedule1] [nvarchar](50) NULL, [Schedule2] [nvarchar](50) NULL, [Schedule3] [nvarchar](50) NULL)--insert into ttnew(Employee,Wday,Schedule1,Schedule2,Schedule3)Select 'Kiran','Sun','Read','Watch','Play'Union Select 'Kiran','Mon','Office','Travel','Meet'Union Select 'Kiran','Tue','Personal','Travel','Meet'Union Select 'Kiran','Wed','Board','Office','Conference'UnionSelect 'Mohd', 'Sun','Meet','Market','Session'UnionSelect 'Mohd', 'Mon','Office','Market','Travel'UnionSelect 'Mohd', 'Tue','Travel','Market','Conference'UnionSelect 'Mohd', 'Wed','Personal','Meet','Watch'----------------------------Thanks for your help.RegardsSSM |
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-11-10 : 07:52:17
|
You can use the PIVOT operator like shown below:SELECT * FROM(SELECT employee, wday, schedule1 + ' ' + schedule2 + ' ' + schedule3 AS schedulesFROM ttnew)sPIVOT(MAX(schedules) FOR wday IN (Sun,Mon,Tue,Wed,Thu,Fri,Sat))P |
|
|
sureshsmanian
Starting Member
31 Posts |
Posted - 2012-11-11 : 02:23:47
|
Thank you so much. |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-11-11 : 07:46:50
|
You are very welcome! |
|
|
sureshsmanian
Starting Member
31 Posts |
Posted - 2012-11-14 : 03:36:57
|
HiI have a modification in my above table, ie) one more field 'Session' has been added. CREATE TABLE [dbo].[ttnew]([Id] [bigint] IDENTITY(1,1) NOT NULL,[Employee] [nvarchar](50) NULL,[WDay] [nvarchar](20) NULL,[Schedule1] [nvarchar](50) NULL,[Schedule2] [nvarchar](50) NULL,[Schedule3] [nvarchar](50) NULL,[Session] [nvarchar] (20) NULL)for the same day, schedules will be different for each session (ie, Forenoon session, Afternoon session)insert into ttnew(Employee,Wday,Schedule1,Schedule2,Schedule3,Session)Select 'Kiran','Sun','Read','Watch','Play','FN'Union Select 'Kiran','Mon','Office','Travel','Meet','FN'Union Select 'Kiran','Tue','Personal','Travel','Meet','FN'Union Select 'Kiran','Wed','Board','Office','Conference','FN'UnionSelect 'Mohd', 'Sun','Meet','Market','Session','FN'UnionSelect 'Mohd', 'Mon','Office','Market','Travel','FN'UnionSelect 'Mohd', 'Tue','Travel','Market','Conference','FN'UnionSelect 'Mohd', 'Wed','Personal','Meet','Watch','FN'UnionSelect 'Kiran','Sun','Conference','Read','Music','AN'Union Select 'Kiran','Mon','Seminar','Play','Read','AN'Union Select 'Kiran','Tue','Read','Seminar','Travel','AN'Union Select 'Kiran','Wed','Meet','Travel','Play','AN'UnionSelect 'Mohd', 'Sun','Music','Play','Travel','AN'UnionSelect 'Mohd', 'Mon','Travel','Music','Seminar','AN'UnionSelect 'Mohd', 'Tue','Conference','Seminar','Market','FN'UnionSelect 'Mohd', 'Wed','Play','Market','Seminar','FN'==============================when I was trying with the above reply, I could able to see only ONE SESSION. whereas I want for the Particular day (from pivot values) but for both the sessions[FN and AN] as two different rows for each employee.Thanks & RegardsSSM |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-11-14 : 06:55:22
|
Add Session also into the inner querySELECT * FROM(SELECT employee, wday, Session, schedule1 + ' ' + schedule2 + ' ' + schedule3 AS schedulesFROM ttnew)sPIVOT(MAX(schedules) FOR wday IN (Sun,Mon,Tue,Wed,Thu,Fri,Sat))P |
|
|
sureshsmanian
Starting Member
31 Posts |
Posted - 2012-11-19 : 04:31:15
|
Thank you so much. Actually, in my real query i was not selecting employee column, so it was not displaying for each session. But now it is ok.Currently, i have an issue with the pivot values ie) as per the above example the wday (sun, mon, tue...) has to be selected from a table. I have replaced the real values with --- Select wday from weektable -- (a table which has all the values)but there is an error. Kindly help me in this regard.Thanks & Regards.SSM |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-11-19 : 06:57:26
|
The PIVOT operator does not allow selecting the column names from a table. If you don't know the column names in advance, the alternative is to use a dynamic query. There is code and examples on Madhivanan's blog here: http://beyondrelational.com/modules/2/blogs/70/posts/10791/dynamic-crosstab-with-multiple-pivot-columns.aspx |
|
|
|
|
|