How to Create a New Column in Q/KDB+ by Multiplying Quantities Based on Conditions

May 26,2025

vlogize

2016-11-23T10:23:24Z

Learn how to creatively manipulate data in Q/KDB+ . This blog covers a straightforward approach to modify the Quantity column based on specific criteria using the `update` function.
---
This video is based on the question https://stackoverflow.com/q/67519636/ asked by the user 'KieranC97' ( https://stackoverflow.com/u/12338249/ ) and on the answer https://stackoverflow.com/a/67520283/ provided by the user 'Caitlin Galway' ( https://stackoverflow.com/u/13886763/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.

Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Q/KDB+ Create new column through a query

Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/licensing
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/by-sa/4.0/ ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/by-sa/4.0/ ) license.

If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
Creating a New Column in Q/KDB+

In the world of data management, having a reliable system to manipulate and calculate data is essential. If you're working with KDB+ and Q, you may find yourself in a situation where you need to adjust values in your tables based on certain conditions. One common scenario involves dealing with financial data where you have both "Buys" and "Sells" alongside the Quantity of items.

The Problem

Imagine you have a table that lists transactions labeled as either "BUY" or "SELL" alongside their corresponding quantities. You want to create a new column that shows the quantities for "SELL" transactions as negative values. The challenge lies in efficiently applying this transformation to your data without over-complicating the process.

The Solution

Fortunately, the Q language offers powerful functionality to handle this with ease. The key tool here is the update command, which allows you to modify existing tables in place based on specific conditions.

Step-by-Step Instructions

Set Up Your Data
Start with a table that contains your transaction data. For example, you might have a table that looks like this:

[[See Video to Reveal this Text or Code Snippet]]

Using the Update Command
To modify the qty column based on the condition where side equals SELL, use the following command:

[[See Video to Reveal this Text or Code Snippet]]

What this does is create a new column where the quantities corresponding to "SELL" transactions are multiplied by -1 (negated).

Result
After executing the command, your updated table will look like this:

[[See Video to Reveal this Text or Code Snippet]]

Updating In Place
If you wish to apply the update directly to the original table without needing to specify its name afterward, you can use the backtick before the table name:

[[See Video to Reveal this Text or Code Snippet]]

This effectively modifies the original table in-place and keeps your data clean.

Conclusion

By utilizing the update functionality in Q/KDB+ , you can easily manage and manipulate your data to reflect the information you need. This method of negating quantities for "SELL" transactions is particularly useful in a trading or financial context where clarity in reporting is crucial.

Through these simple steps, you can dynamically adjust your datasets without convoluted processes. Understanding how to leverage the update command will significantly enhance your Q/KDB+ data manipulation skills!

Q/KDB+ Create new column through a querykdbqsqlquery