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 |
|
kshitizgp
Starting Member
31 Posts |
Posted - 2012-02-03 : 09:25:54
|
Am using start_Date and end_Date paramters to get the work_date between those ..am getting the data as 2012-01-01 , 2012-01-02 , ...i want to display the dates only 01 , 02 how should i cast my date?SELECT @col = (SELECT DISTINCT quotename(Work_date) + ',' FROM form_Details WHERE Work_Date BETWEEN @start_Date and @end_Date ORDER BY Work_Date FOR XML PATH(''))SLEECT @col = substring(@col, 1, len(@col) - 1)I've also cleaned up the kludgy way you deal with the data parameters.The big query can also be simplified:SET @sqlquery = ';with cte as ( select EM.Employee_Name as Name , BN.Block_Name as Block_Name, FD.Work_Date AS Date_Issued , CASE BN.BlockType WHEN 1 THEN ''Domestic'' ELSE ''OverSeas'' END AS BlockType, sum(FD.Work_Hours) AS HOURS from Employee_Master EM INNER JOIN Form_Details FD ON Em.Employee_Master_Id = FD.Employee_Id INNER JOIN Block_Master BN ON BN.Block_Master_Id = FD.Block_Id WHERE FD.Work_Date BETWEEN @start_Date and @end_Date AND EM.Employee_Master_Id = @Employee_Id GROUP BY BlockType,work_date, Block_Name, EM.Employee_Name) SELECT Name, BlockType, Block_Name, ' + @col + ', ' + @ColsSum+ ' As Total from CTE PIVOT (MAX(HOURS) FOR Date_Issued IN ( ' + @col + ' ) ) AS pvt UNION ALL SELECT Name, BlockType, Block_Name, '+@col+', ' + @ColsSum + ' As Total FROM (select ''Total'' as Name, BlockType, space(20) as Block_Name, Hours, Date_Issued FROM CTE) P PIVOT (SUM(hours) FOR Date_Issued IN (' + @Col + ') ) as pvt order BY blocktype 'Then you run this asEXEC sp_executesql @sqlquery, N'@start_date datetime, @end_date datetime, @Employee_id int', @start_date, @end_date, @Employee_idSimpler, cleaner and more efficient!Now that I have been able to read you question, I think the answer to you question isSELECT @headers = (SELECT DISTINCT quotename(Work_date) + ' AS ' + quotename(CONVERT(varchar(2),getdate(),103)) + ', ' FROM form_Details WHERE Work_Date BETWEEN @start_Date and @end_Date ORDER BY Work_Date FOR XML PATH(''))SLEECT @headers = substring(@headers, 1, len(@headers) - 1)ven i run this query am getting;with cte as ( select EM.Employee_Name as Name , BN.Block_Name as Block_Name, FD.Work_Date AS Date_Issued , CASE BN.BlockType WHEN 1 THEN 'Domestic' ELSE 'OverSeas' END AS BlockType, sum(FD.Work_Hours) AS HOURS from Employee_Master EM INNER JOIN Form_Details FD ON Em.Employee_Master_Id = FD.Employee_Id INNER JOIN Block_Master BN ON BN.Block_Master_Id = FD.Block_Id WHERE FD.Work_Date BETWEEN '01-01-2012' and '01-02-2012' AND EM.Employee_Master_Id = 1 GROUP BY BlockType,work_date, Block_Name, EM.Employee_Name) SELECT Name, BlockType, Block_Name, [2012-01-01] AS [03], [2012-01-02] AS [03], [2012-01-01]+[2012-01-02] As Total from CTE PIVOT (MAX(HOURS) FOR Date_Issued IN ( [2012-01-01],[2012-01-02] ) ) AS pvtUNION ALL SELECT Name, BlockType, Block_Name, [2012-01-01] AS [03], [2012-01-02] AS [03], [2012-01-01]+[2012-01-02] As Total FROM (select 'Total' as Name, BlockType, space(20) as Block_Name, Hours, Date_Issued FROM CTE) P PIVOT (SUM(hours) FOR Date_Issued IN ([2012-01-01] AS [03], [2012-01-02] AS [03]) ) as pvtorder BY blocktype incorrect index near AS ...its print 03 for all and also i cant use ordery by my print stmt any modifications? |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-03 : 09:30:12
|
| just use DATENAME(dd,datefield)for getting it in 2 digitsuse RIGHT('00' + DATENAME(dd,datefield),2)------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
kshitizgp
Starting Member
31 Posts |
Posted - 2012-02-03 : 09:58:55
|
quote: Originally posted by visakh16 just use DATENAME(dd,datefield)for getting it in 2 digitsuse RIGHT('00' + DATENAME(dd,datefield),2)------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
but how as headers .do u mean in @headers ? col? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-03 : 10:02:10
|
| hmm? are you asking dates to comes as headers? are you trying to pivot?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
kshitizgp
Starting Member
31 Posts |
Posted - 2012-02-03 : 10:57:14
|
| yeh dates as headers ......yes still not getting |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-03 : 11:07:23
|
| instead of pivoting on Date_Issued just pivot on DATENAME(dd,Date_Issued) in [1],[2],[3],...------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
kshitizgp
Starting Member
31 Posts |
Posted - 2012-02-03 : 11:46:06
|
| ;with cte as (select EM.Employee_Name as Name , BN.Block_Name as Block_Name, FD.Work_Date AS Date_Issued , CASE BN.BlockType WHEN 1 THEN 'Domestic' ELSE 'OverSeas' END AS BlockType, sum(FD.Work_Hours) AS HOURS from Employee_Master EM INNER JOIN Form_Details FD ON Em.Employee_Master_Id = FD.Employee_Id INNER JOIN Block_Master BN ON BN.Block_Master_Id = FD.Block_Id WHERE FD.Work_Date BETWEEN '01-01-2012' and '01-02-2012' AND EM.Employee_Master_Id = 1 GROUP BY BlockType,work_date, Block_Name, EM.Employee_Name ) SELECT Name ,BlockType, Block_Name ,[2012-01-01],[2012-01-02], [2012-01-01]+[2012-01-02] As Total from CTE PIVOT (MAX(HOURS) FOR DATENAME(dd,Date_Issued) IN ( [2012-01-01],[2012-01-02] ) ) AS pvt UNION ALL SELECT Name, BlockType, Block_Name,[2012-01-01],[2012-01-02], [2012-01-01]+[2012-01-02] As TotalFROM (select 'Total' as Name, BlockType, space(20) as Block_Name, Hours, Date_Issued FROM CTE) P PIVOT (SUM(hours) FOR DATENAME(dd,Date_Issued) IN ([2012-01-01],[2012-01-02]) ) as pvt order BY blocktype i tried wat u said?but errrt '('code for sp SET @sqlquery =';with cte as (select EM.Employee_Name as Name , BN.Block_Name as Block_Name, FD.Work_Date AS Date_Issued , CASE BN.BlockType WHEN 1 THEN ''Domestic'' ELSE ''OverSeas'' END AS BlockType, sum(FD.Work_Hours) AS HOURS from Employee_Master EM INNER JOIN Form_Details FD ON Em.Employee_Master_Id = FD.Employee_Id INNER JOIN Block_Master BN ON BN.Block_Master_Id = FD.Block_Id WHERE FD.Work_Date BETWEEN '''+CONVERT(nvarchar(30), @start_Date , 110) +''' and '''+CONVERT(varchar(30),@end_Date, 110) +''' AND EM.Employee_Master_Id = ' + convert(nvarchar,@Employee_Id)+' GROUP BY BlockType,work_date, Block_Name, EM.Employee_Name ) SELECT Name ,BlockType, Block_Name ,'+@col+', ' +@ColsSum+ ' As Total from CTE PIVOT (MAX(HOURS) FOR DATENAME(dd,Date_Issued) IN ( ' + @col + ' ) ) AS pvt UNION ALL SELECT Name, BlockType, Block_Name,'+@col+', ' + @ColsSum + ' As TotalFROM (select ''Total'' as Name, BlockType, space(20) as Block_Name, Hours, Date_Issued FROM CTE) P PIVOT (SUM(hours) FOR DATENAME(dd,Date_Issued) IN (' + @Col + ') ) as pvt order BY blocktype ' |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-03 : 11:54:15
|
| DATENAME(dd,Date_Issued) IN ( [2012-01-01],[2012-01-02] ) is wrongit should beDATENAME(dd,Date_Issued) IN ( [1],[2],.. )------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
kshitizgp
Starting Member
31 Posts |
Posted - 2012-02-03 : 12:24:03
|
| hhaha vishkhm i did it thnx alot ...so naive of me thnx alot kiss u :) |
 |
|
|
|
|
|
|
|