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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 A subquery question

Author  Topic 

lio1972
Starting Member

19 Posts

Posted - 2014-01-30 : 18:28:09
I have a table with 3 fields : Employee Date Hours

How 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 2hours
Employee2 5hours 4 hours 7hours 5hours 2hours
Employee3 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]

Go to Top of Page

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, Hours

Emp1 2013-03-12 00:00:00.000 6
Emp2 2013-03-12 00:00:00.000 7
Emp3 2013-03-12 00:00:00.000 8
Emp3 2013-03-11 00:00:00.000 9
Emp1 2013-03-11 00:00:00.000 5
Emp2 2013-03-11 00:00:00.000 4
Emp1 2013-03-10 00:00:00.000 4
Emp2 2013-03-10 00:00:00.000 9
Emp3 2013-03-10 00:00:00.000 3
Go to Top of Page

VeeranjaneyuluAnnapureddy
Posting Yak Master

169 Posts

Posted - 2014-01-31 : 00:40:13
select * from (select employee,date,hours from @Employee)x
pivot
(
sum(hours) for date in ([2013-03-10],[2013-03-11],[2013-03-12])
)p


Veera
Go to Top of Page

lio1972
Starting Member

19 Posts

Posted - 2014-01-31 : 06:15:58
Thanks
It 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.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-01-31 : 07:59:57
quote:
Originally posted by lio1972

Thanks
It 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.



yep...see
http://beyondrelational.com/modules/2/blogs/70/posts/10840/dynamic-pivot-in-sql-server-2005.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

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 )x
pivot
(
sum(hours) for ClockInDate in (select Convert(Date, Date) from Tab2 )
)p

I also tried this, Didint work


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 )x
pivot
(
sum(hours) for Date in (select '[' + Convert(Varchar(10),Date,110) +']' from Tab2 )
)p
Go to Top of Page

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 )x
pivot
(
sum(hours) for Date in (@dates)
)p
Go to Top of Page

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 )x
pivot
(
sum(hours) for Date in (' + @dates + ')
)p'
EXEC(@SQL)



dates should be a long varchar field and not a table
see above modification

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -