1

Lets say i have a table like this:

    col1     col2     col3
row1  1       null    null

row2  null    2       null

row3  null    null    3

All data types are numbers, integers or long. And i need a table like this:

    col1     col2     col3
row1  1       2        3

How can i accomplish this with an sql statement? I assume i have to create a new table and insert a new row with a select statement, but i am more of a coder and not sure what kind of statement i should use. Thanks for the help.

5
  • Why/How would you combine row1/2/3 into a single row? How do you identify that they should be grouped together? Commented Aug 12, 2011 at 15:17
  • 1
    How to merge such rows: |1|NULL|2 and |3|NULL|4, Just sum values? Commented Aug 12, 2011 at 15:18
  • 1
    You need more information; are you trying to collapse the entire table into one row? If not, how are you defining the subset of rows that will make up one row? Either way, how do you determine what value will go into, say, col if more than one row has a value? Do you want the max? min? sum? etc. Commented Aug 12, 2011 at 15:18
  • 1
    Do you only have those three rows that needs to be joined to one row or are there more rows that needs to be joined as well? Commented Aug 12, 2011 at 15:19
  • 1
    you will always have two nulls and one value in a row? Commented Aug 12, 2011 at 15:19

2 Answers 2

3

sllev got me on the right track. In my case, because all values except for one were always going to be null in any given column, i just had to use the MAX() function. my final sql statement looked like this:

Select MAX(Merge_Table_Parcel_1.txtFrequency1) AS Frequency1,
       MAX(Merge_Table_Parcel_1.SUM_INST_N) AS SUM_INST_N1,
       MAX(Merge_Table_Parcel_1.SUM_INST_D) AS SUM_INST_D1,
       MAX(Merge_Table_Parcel_1.SUM_CTRN_D) AS SUM_CTRN_D1,
       MAX(Merge_Table_Parcel_1.SUM_CTRN_N) AS SUM_CTRN_N1,
       MAX(Merge_Table_Parcel_1.SUM_TRAN_N) AS SUM_TRAN_N1,
       MAX(Merge_Table_Parcel_1.SUM_PPU) AS SUM_PPU1,
       MAX(Merge_Table_Parcel_1.SUM_PPUJOBS) AS SUM_PPUJOBS1,
       MAX(Merge_Table_Parcel_1.SUM_DAYT) AS SUM_DAYT1,
       MAX(Merge_Table_Parcel_1.SUM_RESD_D) AS SUM_RESD_D1,
       MAX(Merge_Table_Parcel_1.SUM_RESD_N) AS SUM_RESD_N1,
       MAX(Merge_Table_Parcel_1.SUM_ON_STRN_D) AS SUM_ON_STRN_D1,
       MAX(Merge_Table_Parcel_1.SUM_ON_STRN_N) AS SUM_ON_STRN_N1
  FROM Merge_Table_Parcel_1

Simple, i know, but i dident see it right away. Thank you to everyone who contributed a response, and to sllev who pointed me in the right direction.

Sign up to request clarification or add additional context in comments.

Comments

0

SELECT MAX(col1) as Col1,
MAX(col2) as Col2,
MAX(col3) as Col3
FROM table_Name

i think it will help....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.