Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- KX Community
- :
- Discussion Forums
- :
- Developer Tools
- :
- Subtract a running value from the top of a column

Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

Subtract a running value from the top of a column

Options

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2015.06.17 10:54 AM

I have a column

x

--

5

6

10

20

and I want to subtract 14 for example

x

--

5 --> remaining is greater than row value, 14-5 leaves 9

6 -> remaining is greater than row value, 9 - 6 leaves 3

10 -> remaining is less than row value 10 - 3

20

so result would be

x

--

0

0

3

20

5 REPLIES 5

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2015.06.17 12:38 PM

select col-deltas 14&sums col from t

Or, making it a function

select {x-deltas y&sums x}[col;14] from t

Terry

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2015.06.22 05:49 AM

Works, thanks very much Terry

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2015.08.25 01:00 PM

I have two columns

t:([]price:"F"$();qty:"I"$())

price qty

10 100

20 300

30 400

22.5 1000

I want to get the sum of price*qty but only for a specific quantity off the top and just the value. So for example if I use 200 then I want (100*10 +100*20). Or if I use 500 then (100*10 + 300*20 + 100*30). Is there a simple way to do this that I am missing?

Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2015.08.25 01:11 PM

You could use something like:

update total:price*remainder from update remainder:deltas 500&sums qty from t

Have added the remainder column for clarity.

Hope this helps

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2015.08.25 04:33 PM

Thanks Kevin

Related Content

Main Office Contacts

**EMEA**

Tel: +44 (0)28 3025 2242

**AMERICAS**

Tel: +1 (212) 447 6700

**APAC**

Tel: +61 (0)2 9236 5700

Useful Information

Resources

Popular Links

Follow Us

KX. All Rights Reserved.

KX and kdb+ are registered trademarks of KX Systems, Inc., a subsidiary of FD Technologies plc.