Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Browse by Tags · View All
TSQL 15
BRH 13
SQL Server 13
#SQL Server 12
#TSQL 8
#SQLServer 4
SQL Server Agent 3
Stored Procedure 2
SQL server Jobs 2
Change Data Capture CDC 2

Archive · View All
November 2010 4
October 2010 3
April 2011 3
March 2011 3
January 2012 2
November 2011 2
August 2011 2
September 2010 1
October 2011 1

Grouping data from multiple rows into single row

Aug 7 2011 8:25PM by Manjunath C Bhat   

Often we come across situation where we need to group data from multiple rows into single row. E.g.: An Employee works for multiple projects, so in this case if columns are EMPLOYEE_NAME, PROJECT_DURATION, PROJECTS_WORKED and think that employee works for 10 projects. Then you would have 10 rows containing EMPLOYEE_NAME as same and then 10 different projects. If you now want to have a single row with Employee name and then 10 subsequent name delimited in single column, unfortunately we don’t have any function like GROUP_CONCAT as in MySQL for SQL server. But not to worry this can be achieved by using XML PATH. Below is the example.

INSERT INTO EMPLOYEE_PROJECT(
	EMPLOYEE_NAME, 
	PROJECT_DURATION,
	PROJECT_NAME
	) 
SELECT 
	EMPLOYEE_NAME, 
	PROJECT_DURATION, 
	PROJECT_NAME = 
		REPLACE( 
			REPLACE( 
			( SELECT REPLACE(PROJECT_NAME, ' ', '') AS [data()] 
FROM 
	EMPLOYEE_PROJECT_DETAILS 
WHERE 
	EMPLOYEE_NAME = a.EMPLOYEE_NAME 
FOR XML PATH('')),' ',' ; '),'',' ') 
FROM 
	EMPLOYEE_PROJECT_DETAILS a 
WHERE
	EMPLOYEE_NAME IS NOT NULL 
GROUP BY 
	EMPLOYEE_NAME, PROJECT_DURATION

Small query but very useful.

Happy Coding.

Technorati Tags: Group, Grouping, XML PATH

Tags: TSQL, SQL Server,


Manjunath C Bhat
102 · 2% · 511
1
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

5  Comments  

  • This is very cool! I've used PIVOT and CTE's to do string aggregation in the past. but this method is much nicer!

    A couple of issues:
    - This removes any spaces from [PROJECT NAMES]. If you have projects named "Project 1" and "Project 2", it will return "Project1 ; Project2" - Your query, as written, will still give one row per project per [EMPLOYEE NAME]. Consider removing [PROJECT DURATION] from the GROUP BY clause

    Consider something like this:

    SELECT
        EMPLOYEE_NAME, 
        LEFT(PROJECT_NAME, LEN(PROJECT_NAME) - 1) as PROJECT_NAME
    FROM
        (
        	SELECT 
        		EMPLOYEE_NAME,
        		PROJECT_NAME=
        			REPLACE(
        				REPLACE( 
        					(SELECT PROJECT_NAME FROM EMPLOYEE_PROJECT_DETAILS WHERE EMPLOYEE_NAME=a.EMPLOYEE_NAME FOR XML PATH('')), 
        					'<PROJECT_NAME>', '['), 
        				'</PROJECT_NAME>', '],')			
        	FROM 
        		EMPLOYEE_PROJECT_DETAILS a 
        	WHERE 
        		EMPLOYEE_NAME IS NOT NULL 
        	GROUP BY 
        		EMPLOYEE_NAME
        ) result
    
    commented on Aug 17 2011 6:07AM
    Marc Jellinek
    97 · 2% · 546
  • Thanks Manjunath and also Marc. Nice indeed.

    Here's one without dropping spaces AND which does not mangle ampersand and other funny characters:

    SELECT EMPLOYEE_NAME,PROJECT_DURATION, REPLACE( REPLACE( 
       (SELECT REPLACE(PROJECT_NAME, ' ', CHAR(127)) AS [data()] 
         --temporarily replace spaces in names with unprintable CHAR
        FROM EMPLOYEE_PROJECT_DETAILS b
        WHERE b.EMPLOYEE_NAME=a.EMPLOYEE_NAME
        FOR XML PATH(''), TYPE).value('.', 'varchar(8000)')
         --Looks like Al!Smith Dick!Nixon A&P, with space separating (showing CHAR(127) as !)
       ,' ',' ; ') --change space separator into ;
       ,CHAR(127),' ')  --restore spaces in names
      [Project Names]
    FROM EMPLOYEE_PROJECT_DETAILS a 
    WHERE EMPLOYEE_NAME IS NOT NULL 
    GROUP BY EMPLOYEE_NAME,PROJECT_DURATION
    

    It seems Manjunath had a special character between 'quotes' but it was lost when posting, hence the mysterious replace of empty string with space. So the CHAR(127) above is his idea really. The .value trick is thanks to KoenV on stackoverflow. Use nvarchar(max) if you need all chars or longer than 8000.

    commented on Aug 29 2011 1:25PM
    KipB7
    1720 · 0% · 11
  • Kip, can you re-post this, but select your code and click the button with the 101 label? It looks like your code go munged.

    commented on Aug 29 2011 1:39PM
    Marc Jellinek
    97 · 2% · 546
  • Thanks Manjunath and also Marc. Nice indeed.

    Here's one without dropping spaces AND which does not mangle ampersand and other funny characters:

    SELECT EMPLOYEE_NAME,PROJECT_DURATION, REPLACE( REPLACE( 
       (SELECT REPLACE(PROJECT_NAME, ' ', CHAR(127)) AS [data()] 
          --temporarily replace spaces in names with unprintable CHAR
        FROM EMPLOYEE_PROJECT_DETAILS b
        WHERE b.EMPLOYEE_NAME=a.EMPLOYEE_NAME
        FOR XML PATH(''), TYPE).value('.', 'varchar(8000)') 
          --Looks like Al!Smith Dick!Nixon A&P, with space separating (showing CHAR(127) as !)
       ,' ',' ; ') --change space separator into ;
       ,CHAR(127),' ')  --restore spaces in names
      [Project Names]
    FROM EMPLOYEE_PROJECT_DETAILS a 
    WHERE EMPLOYEE_NAME IS NOT NULL 
    GROUP BY EMPLOYEE_NAME,PROJECT_DURATION
    

    It seems Manjunath had a special character between 'quotes' but it was lost when posting, hence the mysterious replace of empty string with space. So the CHAR(127) above is his idea really. The .value trick is thanks to KoenV on stackoverflow. Use nvarchar(max) if you need all chars or longer than 8000.

    commented on Aug 29 2011 2:21PM
    KipB7
    1720 · 0% · 11
  • Thank You Guys for Improvising the query. :)

    Thanks, Manjunath C Bhat

    commented on Apr 4 2012 4:07AM
    Manjunath C Bhat
    102 · 2% · 511

Your Comment


Sign Up or Login to post a comment.

"Grouping data from multiple rows into single row" rated 5 out of 5 by 1 readers
Grouping data from multiple rows into single row , 5.0 out of 5 based on 1 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]