Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Best way to flatten a table without Math functions 1

Status
Not open for further replies.

HoustonGuy

Programmer
Jun 29, 2000
165
US
I have a table that spills out this way:

SQL:
Column1    Column2   column3   Column4    Column5  etc...
John       Attended
John                           Attended
John                                       Attended
Tom                   Attended             
Tom                                        Attended
Jill       Attended            
Jill                  Attended

I would like the results this way:
SQL:
Column1    Column2   column3   Column4    Column5  etc...
John       Attended            Attended   Attended
Tom                  Attended             Attended
Jill       Attended  Attended

Currently I create a temp table and update Columns 2 thru 5 based on Column1 to flatten it out.
Question:
1) How can I flatten this table without using a mathematical function? Most Pivot/Crosstab tables I see created must use the SUM, MAX function etc.
2) Do I pivot this on MAX(Column1)?
3) Is using a temp table to flatten data acceptable?

Thank you!
:)
 
Why the restriction on avoiding MAX()? The most straightforward solution would be

Code:
SELECT Column1, MAX(Column2) Column2, ..., MAX(ColumnN) ColumnN
  FROM MyTable
 GROUP BY Column1
 
I'm afraid my example was too simple. Your example does work - If the values were the same. (And I appreciate the response.)
This is a better example of my resultset:

Code:
Column1    Column2   column3   Column4    Column5  etc...
John       Attended
John                           Unattended
John                                       Attended
Tom                   Attended             
Tom                                        Attended
Jill       Attended            
Jill                  Unttended

Results Needed:

Code:
Column1    Column2   column3   Column4    Column5  etc...
John       Attended            Unattended   Attended
Tom                  Attended             Attended
Jill       Attended  Unattended

I think this is a better example of the scenario I have.
 
It'll still work, as you don't take max of the whole column, but per person in column1, notice the group by.
You only have a problem if a person is both Attended and Unattended, but that'll never be the case (hopefully).

Bye, Olaf.
 
The hard way would be create a temporary table based on unique column1. Then push in the other columns. I have not tried but maybe use COALESCE.

djj
The Lord is my shepherd (Psalm 23) - I need someone to lead me!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top