Skip to main content
Announcements
Join us at Qlik Connect for 3 magical days of learning, networking,and inspiration! REGISTER TODAY and save!
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

adding column in table in script

Hi,

I have table created from SQL query in the script , I want to add an extra column in this table , this column is based on Previous function in Qlikview , bellow is an example

Order:

SQL

Select Col1,Col2

FROM

Order;

I want to add below third column in Order table inside script so final table should have three columns

Col3=Previous(Col1)

9 Replies
johnca
Specialist
Specialist

Maybe this?

Temp:

Select

     Col1,

     Col2

From OrderDB;

Final:

NoConcatenate

Load

     Col1,

     Col2

     Peek(Col1) as Col3

Resident Temp

Order By Col1 asc ;

Drop Table Temp;

tamilarasu
Champion
Champion

Hi Vibhu,

You can also try,

Order:
LOAD *, Previous(Col1) as Col3;
SQL
Select Col1,Col2
FROM
Order;

Anonymous
Not applicable
Author

Hi Vibhu, Previuos with order by don't works as exprected sometimes, use peek better.

LEFT JOIN (Order)

LOAD

     Col1,

     Col2,

     Peek(Col2) as Col3

RESIDENT Order

ORDER BY Col1, Col2;

Regards!

tamilarasu
Champion
Champion

Hi Manuel,

Do you have any example that shows the behaviour.? I would like to know.

Thank you.

Anonymous
Not applicable
Author

Hi Tamil,

When you load a resident table with a previous statement, and you order your table at the same load statement, previous don't works as expected, because order by is done later than previous, peek is calculated after order.

Regards!

johnca
Specialist
Specialist

As elegant as the preceding load looks, my solution seems the simplest...and works. BTW, I did forget one comma after Col2 in the resident load, so the code should look like

Temp:

Select

     Col1,

     Col2

From Order;

Order:

NoConcatenate

Load

     Col1,

     Col2,

     Peek(Col1) as Col3

Resident Temp

Order By Col1 asc ;

Drop Table Temp;

Anonymous
Not applicable
Author

Hi John,

I don't do a preceding load, I am doing a left join to join all data in the same table.

Regards!

johnca
Specialist
Specialist

Right Manuel, but Tamil's does, and is to what I was referring to. I responded to the wrong suggested solution...sorry about that. Your solution indeed looks simpler, but is a left join more efficient than my solution, especially for large data sets?

--john

tamilarasu
Champion
Champion

Thank you, Manuel. I will test this tomorrow.