Q
Problem solve Get help with specific problems with your technologies, process and projects.

# Splitting one column into several

I have a table with one field that contains the barcode for a distinct item -- let's say they're pencils and another field with defects. Each pencil can have no defects or up to 10 defects, such that the table could contain up to 10 entries per pencil. I would like to pull the data out such that I get one row per pencil along with the defect(s) for that pencil (joining table to itself?).

```Table:

Pencil   Defect

023b5607  dented
023b5607  no graphite
023b5607  no eraser
023c5608  dented
023d5609  dented
023d5609  no eraser

Results:

Pencil    dented   no eraser  no graphite

023b5607  dented   no eraser  no graphite
023c5608  dented
023d5609  dented   no eraser```

I don't want a concatenated list because I need to analyze the data using a statistical program. Each of the possible values in column 2 of the example table must have its own column in the output table.

If you're using Microsoft Access, it has a built in crosstab capability which gives you exactly what you want. If you can't use a crosstab function, here's a brute force method you can try.

Begin by declaring the final result table you want with the columns that you need. Then declare a temporary table with the same columns, to contain partial results, obtained from the original table.

```create table finaltable
(pencil char(8)
, dented char(6), nographite char(11), noeraser char(9))

create table temptable
(pencil char(8)
, dented char(6), nographite char(11), noeraser char(9))```

Note that the columns are designed to hold the actual defect character strings; this could be made a bit more efficient by using yes/no or 1/0 datatypes (assuming your statistical software could work with that as well).

Now populate the temporary table by selecting for each of its columns.

```insert into temptable
select pencil, 'dented', ' ', ' '
from origtable
where defect = 'dented'

insert into temptable
select pencil, ' ', 'no graphite', ' '
from origtable
where defect = 'no graphite'

insert into temptable
select pencil, ' ', ' ', 'no eraser'
from origtable
where defect = 'no eraser'```

You can do the above in a single query if your database allows UNIONS in the INSERT SELECT syntax.

If the final result table must also include defect-free pencils, then just include another insert for the pencils alone --

```insert into temptable
select pencil, ' ', ' ', ' '
from origtable```

Now collapse the temptable rows into the final result table using GROUP BY on the pencil, with MAX() to capture each defect -- pencils with no defect in a particular column will have a blank in that column.

```insert into finaltable
select pencil, max(dented), max(nographite), max(noeraser)
from temptable
group by pencil```

Variations: With SUM() in the collapsing query, you can enhance this approach to do things like counting (using 0/1 as the datatype) and totals (using numeric values as the datatype).

• The Best SQL Web Links: tips, tutorials, scripts, and more.
• Have an SQL tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize. Submit your tip today!
• Ask the Experts yourself: Our SQL, database design, Oracle, SQL Server, DB2, metadata, object-oriented and data warehousing gurus are waiting to answer your toughest questions.

#### Start the conversation

Send me notifications when other members comment.

## SearchDataManagement

• ### EnterpriseDB looks to grow market for PostgreSQL

Enterprises are increasingly using the open source PostgreSQL database. Read in this Q&A where the growth is coming from and how ...

• ### 3 of the top use cases for graph databases

Graph databases establish many unique relationships between data points. These unusual relationships are beneficial in many use ...

• ### Dell EMC Streaming Data Platform integrates open source technology

Dell combines several open source streaming data technologies, including Apache Kafka, Apache Flink and Pravega, to create a new ...

• ### What Salesforce means for Tableau in the cloud

After the Salesforce acquisition, users have wondered about the future of Tableau in the cloud. Experts weigh in on how the ...

• ### MicroStrategy analytics suite fosters insurance co-op's growth

Using MicroStrategy's BI platform, a Canada-based insurance co-operative has been able to spread analytics to end users ...

• ### Yellowbrick Data, MicroStrategy partnership aims to enhance analytics

The new partnership will enable flexible scalability, support for mixed workloads and multi-cloud support. The goal is to provide...

## SearchSAP

• ### 5 tips for a successful S/4HANA Cloud implementation

Moving to S/4HANA public cloud can help you save on IT maintenance and simplify real-time analysis. Here's a look at what you can...

• ### Take care of data before SAP S/4HANA migration

In this Q&A, Rajesh Rengarethinam of ERP security vendor Appsian discusses why reviewing data security and business processes are...

Business Suite 7 will receive mainstream maintenance until 2027, a decision industry observers say reflects the difficulty of an ...

## SearchSQLServer

• ### SQL Server database design best practices and tips for DBAs

Good database design is a must to meet processing needs in SQL Server systems. In a webinar, consultant Koen Verbeeck offered ...

• ### SQL Server in Azure database choices and what they offer users

SQL Server databases can be moved to the Azure cloud in several different ways. Here's what you'll get from each of the options ...

• ### Using a LEFT OUTER JOIN vs. RIGHT OUTER JOIN in SQL

In this book excerpt, you'll learn LEFT OUTER JOIN vs. RIGHT OUTER JOIN techniques and find various examples for creating SQL ...

## TheServerSide.com

• ### Why the 8 Java primitive data types are not objects

What's the difference between Java primitive types and objects? To start, don't classify primitive types as objects. Let's ...

• ### A brief history of Java: How it forever changed programming

The Java programming language is an important tool in an application developer's quiver. But Java has also undergone many changes...

• ### How to properly perform Java String comparisons

Don't let incorrect Java String object comparisons bog down your code. Follow this tutorial to understand the differences between...

## SearchDataCenter

• ### Learn the main Linux OS components

Linux is one operating choice to run on your infrastructure. Get started with these terms to discover how the OS works and how it...

• ### New Dell EMC PowerEdge servers are built for the rugged edge

New Dell Streaming Media Platform includes two small form factor PowerEdge servers and a Modular Data Center. Dell aims the ...

• ### Initiative aims to improve data center incident reporting

The Data Center Incident Reporting Network hopes to pull back the smoke screen on software and hardware issues to improve ...

## SearchContentManagement

• ### Test yourself on the differences between SharePoint and OneDrive

Businesses use Microsoft SharePoint and OneDrive capabilities for a number of reasons. Test your knowledge on the differences ...

• ### Akeneo rolls out newest version of PIM software

Akeneo PIM version 4.0 features digital asset manager, API-integrated connection modules and AI attribute mapping to better ...

• ### Get to know 4 workflow automation tools

Workflow automation can benefit businesses by making manual processes digital, giving employees more time to work on other tasks....

## SearchHRSoftware

• ### How real-time analytics can benefit HR

For analytics to be truly useful, it must be embedded into user workflows. Learn how HR tools are incorporating such approaches ...

• ### Human vs. AI in recruiting: Why both matter

Recruiters may consider using both the human element and the data brought in by intelligent tools if it can help them make better...

• ### Kronos-Ultimate Software merger creates \$3B firm with product overlap

Kronos Inc. and Ultimate Software are being merged by their private equity owner into a new, yet unnamed, entity. How this will ...

Close