Solved

SQL Syntax

Posted on 2009-05-19
7
274 Views
Last Modified: 2012-05-07
I have a table with mulitple columns of doc types that either has a 0 or 1. I have created another table and I want to put all the 1s concatenated into the docname field of the new table. Example

1st table
acctID   doc1   doc2   doc3
123         0        1           1

I need to pull it like this into my 2nd table
acctID      docs
123        doc2, doc3


I know this is easy but I have spent too much time on it already.....Any help would be greatly appreciated
0
Comment
Question by:healthcheckinc
7 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24423322
what is the database/version you are using?
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 24423327
Hello healthcheckinc,

With respect, both of these are reall, really suboptimal designs :)

What db product are you using, as it makes a difference in syntax...

Regards,

Patrick
0
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 24423358
SQL Server...

SELECT acctID, SUBSTRING(
      CASE WHEN doc1 = 0 THEN '' ELSE ', doc1' END +
      CASE WHEN doc2 = 0 THEN '' ELSE ', doc2' END +
      CASE WHEN doc3 = 0 THEN '' ELSE ', doc3' END, 3, 100) AS Docs
FROM SomeTable

To create a new table...

SELECT acctID, SUBSTRING(
      CASE WHEN doc1 = 0 THEN '' ELSE ', doc1' END +
      CASE WHEN doc2 = 0 THEN '' ELSE ', doc2' END +
      CASE WHEN doc3 = 0 THEN '' ELSE ', doc3' END, 3, 100) AS Docs
INTO NewTableName
FROM SomeTable
0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 11

Expert Comment

by:bmatumbura
ID: 24423359
Try this
SELECT AcctID, 
CASE WHEN doc1 AND doc2 AND doc3 THEN 'doc1, doc2, doc3'
CASE WHEN doc1 AND doc2 THEN 'doc1, doc2'
CASE WHEN doc1 and doc3 THEN 'doc1, doc3'
CASE WHEN doc2 AND doc3 THEN 'doc2, doc3'
CASE WHEN doc1 THEN 'doc1'
CASE WHEN doc2 THEN 'doc2'
ELSE 'doc3' END AS docs
INTO Table2 from Table1

Open in new window

0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 24423374
Access...

SELECT acctID, Mid(IIf(doc1 = 0, "", ", doc1") & IIf(doc2 = 0, "", ", doc2") & IIf(doc3 = 0, "", ", doc3"), 3) AS docs
FROM SomeTable

creating new table...

SELECT acctID, Mid(IIf(doc1 = 0, "", ", doc1") & IIf(doc2 = 0, "", ", doc2") & IIf(doc3 = 0, "", ", doc3"), 3) AS docs
INTO NewTable
FROM SomeTable
0
 
LVL 11

Expert Comment

by:bmatumbura
ID: 24423378
correction
SELECT AcctID, 
CASE WHEN doc1 AND doc2 AND doc3 THEN 'doc1, doc2, doc3'
CASE WHEN doc1 AND doc2 THEN 'doc1, doc2'
CASE WHEN doc1 and doc3 THEN 'doc1, doc3'
CASE WHEN doc2 AND doc3 THEN 'doc2, doc3'
CASE WHEN doc1 THEN 'doc1'
CASE WHEN doc2 THEN 'doc2'
CASE WHEN doc3 THEN 'doc3'
ELSE '' END AS docs
INTO Table2 from Table1

Open in new window

0
 

Author Closing Comment

by:healthcheckinc
ID: 31583062
its needed for a report..I didnt design the system
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question