SQL Select - Grouping Rows

I need help with a SQL Select Script that will remove duplicates and combine values.  I have a table with two columns.  Right now each ID is duplicated by the validity.  I just need need to combine all validity values and separate by a ";".

ID                Validity        
------             -----------
12A             Product
12A             Family
12A             FO
31C             Product
87A             FO
87A             Product

Expected Output:

ID                Validity
------             -----------
12A             Product; Family;FO
31C             Product
87A             FO; Product
tmajor99Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dirk StraussSenior Full Stack DeveloperCommented:
Have a look at this thread: GROUP BY to combine/concat a column.
0
Michael CrippsMicrosoft Access & Excel  Advanced /SQL Syntax ExpertCommented:
What database system are you using ? Every system has its' own flavor of SQL. I can cook something for you quick, but I am not sure we are using the same system.
0
PortletPaulEE Topic AdvisorCommented:
tmajor99

I notice you have asked questions on Oracle and SQL Server in the past, and the way those 2 products do what you want are radically different. PLEASE always indicate what database you want the query for.

ORACLE
select id, listagg(validity,';') within group (order by validity) as validity
from table1
group by id
order by id

Open in new window


MS SQL Server
select
    id, ca.validity
from table1 t1
cross apply (
        SELECT
        STUFF((
              SELECT
                    ';' + validity
              FROM table1 t2
              WHERE t1.id = t2.id
              ORDER BY validity
              FOR XML PATH ('')
              )
             , 1, 1, '')
       ) ca (validity)
group by 
      id, ca.validity

Open in new window


MySQL
select id, group_concat(validity ORDER BY validity SEPARATOR ';') as validity
from table1
group by id
order by id

Open in new window

0
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

Pawan KumarDatabase ExpertCommented:
Please try this for SQL Server. Full Tested solution.

Do not use Order BY Before XML Path. The ordering is changing the output you need.

CREATE TABLE Combiner
(
	 ID VARCHAR(5)                
	,Validity VARCHAR(20)
)
GO

INSERT INTO Combiner VALUES
('12A','Product'),
('12A','Family'),
('12A','FO'),
('31C','Product'),
('87A','FO'),
('87A','Product')
GO

Open in new window


Solution - Do not use Order BY Before XML Path.

SELECT DISTINCT b.Id 
			, STUFF 
				((
					SELECT '; ' + a.validity 
					FROM Combiner a
					WHERE ( a.ID = b.ID )					
					FOR XML PATH('')
				) ,1,2,'') 
				AS validity
FROM Combiner b

Open in new window


Output

/*------------------------
OUTPUT
------------------------*/
Id    validity
----- ----------------------------------
12A   Product; Family; FO
31C   Product
87A   FO; Product

(3 row(s) affected)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PortletPaulEE Topic AdvisorCommented:
why not ordered? please provide a reason i you making such a statement.
0
Pawan KumarDatabase ExpertCommented:
If Order BY is added you will get

12A   Family; FO; Product

But requirement is this

12A   Product; Family; FO
0
PortletPaulEE Topic AdvisorCommented:
then, it would have been more correct to say: "if you wish to have an un-ordered list do not use order by"

(it reads like an instruction that should always be obeyed, and of course that isn't true)

all 3 of my examples assume the opposite, that tmajor99 would wish to have an ordered list.
0
Pawan KumarDatabase ExpertCommented:
Yes the ideal way is to use ORDER BY ( ASC/DESC ) but this is a different case.
0
tmajor99Author Commented:
Really nice!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.