Author |
Topic |
lio1972
Starting Member
19 Posts |
Posted - 2014-01-30 : 18:28:09
|
I have a table with 3 fields : Employee Date HoursHow can I create a query to display them like that? Do I have to use sub queryes for each day?Employees Date1 Date2 Date3 Date4 Date5-----------------------------------------------------------------Employee1 8hours 6 hours 7hours 3hours 2hoursEmployee2 5hours 4 hours 7hours 5hours 2hoursEmployee3 8hours 6 hours 7hours 3hours 2hours |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2014-01-30 : 21:35:07
|
first show us how the data is like in your table KH[spoiler]Time is always against us[/spoiler] |
 |
|
lio1972
Starting Member
19 Posts |
Posted - 2014-01-30 : 23:15:02
|
Like I said in my original post I have a table with 3 fields : Employee, Date, HoursEmp1 2013-03-12 00:00:00.000 6Emp2 2013-03-12 00:00:00.000 7Emp3 2013-03-12 00:00:00.000 8Emp3 2013-03-11 00:00:00.000 9Emp1 2013-03-11 00:00:00.000 5Emp2 2013-03-11 00:00:00.000 4Emp1 2013-03-10 00:00:00.000 4Emp2 2013-03-10 00:00:00.000 9Emp3 2013-03-10 00:00:00.000 3 |
 |
|
VeeranjaneyuluAnnapureddy
Posting Yak Master
169 Posts |
Posted - 2014-01-31 : 00:40:13
|
select * from (select employee,date,hours from @Employee)xpivot(sum(hours) for date in ([2013-03-10],[2013-03-11],[2013-03-12]))pVeera |
 |
|
lio1972
Starting Member
19 Posts |
Posted - 2014-01-31 : 06:15:58
|
ThanksIt works perfect. I really appreciate your help.Is there a way to pass the date parameters?Let say that I have 100 dates and I want to pass a date from certain week and display the 7 days of that week. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
lio1972
Starting Member
19 Posts |
Posted - 2014-01-31 : 10:16:17
|
Thank you.That is my query so far and I am getting this error 'Incorrect syntax near ‘select’. Expecting ID, QUATED_ID or ‘.’ . select * from (select Tab1.Name,Tab2.Company, Tab2.employeeNum,Tab2.Date,Tab2.Hours from Tab2 inner join Tab1 ON Tab1.Company = Tab2.Company AND Tab2.Emp = Tab2.Emp )xpivot(sum(hours) for ClockInDate in (select Convert(Date, Date) from Tab2 ))p I also tried this, Didint workselect * from (select Tab1.Name,Tab2.Company, Tab2.employeeNum,Tab2.Date,Tab2.Hours from Tab2 inner join Tab1 ON Tab1.Company = Tab2.Company AND Tab2.Emp = Tab2.Emp )xpivot(sum(hours) for Date in (select '[' + Convert(Varchar(10),Date,110) +']' from Tab2 ))p |
 |
|
lio1972
Starting Member
19 Posts |
Posted - 2014-01-31 : 10:21:01
|
Reading further about Dynamic Pivot I decided to do that, but the errors were that @dates must be scalar variable:Declare @dates Table(daysInWeek nvarchar(12));SET @dates = STUFF ((select '[' + Convert(Varchar(10),Date,110) +']' from Tab2),1,1,'')select * from (select Tab1.Name,Tab2.Company, Tab2.employeeNum,Tab2.Date,Tab2.Hours from Tab2 inner join Tab1 ON Tab1.Company = Tab2.Company AND Tab2.Emp = Tab2.Emp )xpivot(sum(hours) for Date in (@dates))p |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2014-02-03 : 05:10:53
|
quote: Originally posted by lio1972 Reading further about Dynamic Pivot I decided to do that, but the errors were that @dates must be scalar variable:Declare @dates varchar(3000)Table(daysInWeek nvarchar(12));DECLARE @SQL varchar(max)SET @dates = STUFF ((select '[' + Convert(Varchar(10),Date,110) +']' from Tab2),1,1,'')SET @SQL='select * from (select Tab1.Name,Tab2.Company, Tab2.employeeNum,Tab2.Date,Tab2.Hours from Tab2 inner join Tab1 ON Tab1.Company = Tab2.Company AND Tab2.Emp = Tab2.Emp )xpivot(sum(hours) for Date in (' + @dates + '))p'EXEC(@SQL)
dates should be a long varchar field and not a tablesee above modification------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
|