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
 sql query

Author  Topic 

kshitizgp
Starting Member

31 Posts

Posted - 2012-01-27 : 04:24:07
[code]


CREATE TABLE #cmd_result (id int,temp datetime)
INSERT #cmd_result
EXEC getPublicHolidays '2012','2012-01-01' , '2012-01-31'

DECLARE @col VARCHAR(MAX)
DECLARE @sql nvarchar(2000)

select @col = COALESCE(@col + ',','')+ QUOTENAME(Work_Date)
from form_Details WHERE Work_Date BETWEEN '2012-01-01' and '2012-01-04' AND Employee_Id=338
GROUP BY Work_Date
ORDER BY Work_Date
PRINT @col

SET @sql = 'SELECT Name , Block_Name ,Total_Dates,BlockType,'+@col+' from
(
select EM.Employee_Name as Name ,
BN.Block_Name as Block_Name,
FD.Work_Date ,
case when( (select COUNT(id) from #cmd_result where #cmd_result.temp=FD.Work_Date ) > 0 )then CONVERT(VARCHAR(20), FD.Work_Date, 103)+ ''-'' + ''PH'' else CONVERT(VARCHAR(20), FD.Work_Date, 103) end as Total_Dates ,
CASE BN.BlockType WHEN 1 THEN ''Domestic'' ELSE ''OverSeas'' END AS BlockType,
sum(FD.Work_Hours) AS HOURS

from Employee_Master EM , Block_Master BN , Form_Details FD where Em.Employee_Master_Id = FD.Employee_Id
and BN.Block_Master_Id = FD.Block_Id
and Work_Date between ''2012-01-01'' and ''2012-01-04'' AND EM.Employee_Master_Id = 35 GROUP BY BN.BlockType, work_date ,Block_Name , EM .Employee_Name
) p PIVOT (MAX(HOURS) FOR Work_Date IN ( ' + @col + ') ) AS pvt


order by BlockType '

PRINT(@sql)

EXEC(@sql)





DROP TABLE #cmd_result[/code]

AM GETTING OUTPUT AS 1/1 2/1 3/1

ANKIT ABC Ext Domestic 0 NULL NULL
ANKIT ABC Ext Domestic NULL 0 NULL
ANKIT ABC Ext DomestiC NULL NULL 0


I WANT IT LIKE THAT PLEASE HELP 1/1 2/1 3/1

ANKIT ABC Ext Domestic O O O

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-01-27 : 08:30:02
sounds like this?

CREATE TABLE #cmd_result (id int,temp datetime)
INSERT #cmd_result
EXEC getPublicHolidays '2012','2012-01-01' , '2012-01-31'

DECLARE @col VARCHAR(MAX)
DECLARE @sql nvarchar(2000)

select @col = 'MAX(' + COALESCE(@col + '),MAX(','')+ QUOTENAME(Work_Date) + ')'
from form_Details WHERE Work_Date BETWEEN '2012-01-01' and '2012-01-04' AND Employee_Id=338
GROUP BY Work_Date
ORDER BY Work_Date
PRINT @col

SET @sql = 'SELECT Name , Block_Name ,Total_Dates,BlockType,'+@col+' from
(
select EM.Employee_Name as Name ,
BN.Block_Name as Block_Name,
FD.Work_Date ,
case when( (select COUNT(id) from #cmd_result where #cmd_result.temp=FD.Work_Date ) > 0 )then CONVERT(VARCHAR(20), FD.Work_Date, 103)+ ''-'' + ''PH'' else CONVERT(VARCHAR(20), FD.Work_Date, 103) end as Total_Dates ,
CASE BN.BlockType WHEN 1 THEN ''Domestic'' ELSE ''OverSeas'' END AS BlockType,
sum(FD.Work_Hours) AS HOURS

from Employee_Master EM , Block_Master BN , Form_Details FD where Em.Employee_Master_Id = FD.Employee_Id
and BN.Block_Master_Id = FD.Block_Id
and Work_Date between ''2012-01-01'' and ''2012-01-04'' AND EM.Employee_Master_Id = 35 GROUP BY BN.BlockType, work_date ,Block_Name , EM .Employee_Name
) p PIVOT (MAX(HOURS) FOR Work_Date IN ( ' + @col + ') ) AS pvt
GROUP BY Name , Block_Name ,Total_Dates,BlockType

order by BlockType '

PRINT(@sql)

EXEC(@sql)





DROP TABLE #cmd_result


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -