Click here to Skip to main content
15,923,164 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,

I want to make my SQL select query display results in a very unique way that i hope somebody can help me accomplish.

Instead of having the results for 3 columns being displayed over 9 rows, i would like the data displayed in one row and have the 3 column names repeated 9 times.

I tried to Google this but i can't seem to find the right ideas anywhere.

Thanks a million to anyone that can help!
Posted
Comments
bbirajdar 25-Jan-13 6:23am    
You are doing it in the wrong place.. Fetch the data in the normal way and then display it the way you want from your 'application'
travistanley 25-Jan-13 6:33am    
Hi, ok if i were to do that how do a generate an unknown number of columns in my asp.net application(gridview for the data dump), i used 9 rows as an example to say i would need the 3 columns to be repeated 9 times. Is there any way to repeat column names to genrate only one row of data in SQL?
travistanley 25-Jan-13 6:36am    
at the moment a have an extremely long method of achieving my desired result and just wanted to know if i could simplify it in the SQL
bbirajdar 25-Jan-13 9:29am    
No.. SQL does not support the format you need..Its basically created for data fetching. So you need a language which is rich for this purpose. C# can do it
travistanley 11-Feb-13 1:05am    
Iv'e managed to do this using dynamic SQL and it works brilliantly, Nothing is impossible. In my short dev career i've learnt that if it can be done in SQL then it makes you app easier to write and less complex on the client side of things. Thanks for your input tho as it challenged me to find a way.

SQL
ALTER PROCEDURE [dbo].[Pr_TimeCardReport2]
	  @EM		  VARCHAR(MAX),
      @SD         DATETIME,
      @ED         DATETIME
   
AS
BEGIN

      DECLARE           @Sql VARCHAR(8000)
      DECLARE           @Index INT
      SET               @Index = 0

      SET @Sql =  'SELECT           TC.ID, TC.Start, TC.Finish, TC.EmpMail,
                        (SELECT OUTTime FROM TimeCardBreaks WHERE BreakType = ''Lunch''
                              AND TCID = TC.ID) [L_Out],
                        (SELECT INTime FROM TimeCardBreaks WHERE BreakType = ''Lunch''
                              AND TCID = TC.ID) [L_In], '

      WHILE (@Index < 10)
      BEGIN
            SET @Sql = @Sql + '(SELECT OUTTime FROM TimeCardBreaks TCB WHERE BreakType = ''Smoke''
                        AND TCID = TC.ID
                        AND ' + CAST(@Index AS VARCHAR) + ' = (SELECT COUNT(Id) FROM TimeCardBreaks WHERE BreakType = ''Smoke'' AND TCID = TC.ID
                        AND ID < TCB.ID)) [S' + CAST(@Index+1 AS VARCHAR) + '_Out],
                        (SELECT INTime FROM TimeCardBreaks TCB WHERE BreakType = ''Smoke''
                        AND TCID = TC.ID
                        AND ' + CAST(@Index AS VARCHAR) + ' = (SELECT COUNT(Id) FROM TimeCardBreaks WHERE BreakType = ''Smoke'' AND TCID = TC.ID
                        AND ID < TCB.ID)) [S' + CAST(@Index+1 AS VARCHAR) + '_In], '
            SET @Index = @Index + 1
      END                     

      SET @Sql = SUBSTRING(@Sql, 1, LEN(@Sql)-1)
      SET @Sql = @Sql + ' FROM      TimeCard TC WHERE EmpMail = ''' +@EM+ ''' AND Start >= ''' + CAST(@SD AS VARCHAR)+ ''' AND Finish <= ''' + CAST(@ED AS VARCHAR) + ''''

      EXEC (@Sql)
     
END
 
Share this answer
 
Hi,

use
SQL
for xml path()
,

it gives you a xml string of a perticular column,

for example,

SQL
USE AdventureWorks2008R2
SELECT      CAT.Name AS [Category],
            STUFF((    SELECT ',' + SUB.Name AS [text()]
                        – Add a comma (,) before each value
                        FROM Production.ProductSubcategory SUB
                        WHERE
                        SUB.ProductCategoryID = CAT.ProductCategoryID
                        FOR XML PATH('') – Select it as XML
                        ), 1, 1, '' )
                        – This is done to remove the first character (,)
                        – from the result
            AS [Sub Categories]
FROM  Production.ProductCategory CAT


Thanks.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900