npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

tabulation-query-builder

v0.0.16

Published

Easy tabulation with generated SQL.

Readme

Easy tabulation with generated SQL.

BREAKING CHANGES

0.0.13 to 0.0.14

  • If you set interval or categoryRange option, you set with setAggregating until now. Please write in setIndexing like example.

Introduction

When I come up with a hypothesis, I want to confirm it soon. And based on that, if you come up with the next hypothesis, I'd like to confirm it soon too. I think that what is important is the speed of this cycle. Responsive analytical methods lead to efficient learning on your marketing.

How to know your service?

In order to know the user, I think that it is necessary to analyze the user's behavior history. At that time, you will prepare two axes and analyze user's behavior from different indicaters. Due to this operation called Tabulation, we used Excel, introduced BI tools, or wrote SQL every time. Since I was troublesome to do whichever, I created a module that generates SQL from several input items.

Usage

Generate SQL for analyzing with some inputs.

User count by total payment within term.

const tqb = new TabulationQueryBuilder();

tqb.setTable('payment_logs');

/**
*
* Matching
*   We narrow down the logs matching the conditions
*   by specifying the period and user ID.
*
*   e.g) Term range
*
*/

tqb.setMatching({
  field: 'timestamp',
  range: ['2017-01-01 00:00:00', '2017-01-03 23:59:59']
});

/**
*
* Indexing
*   At first, indexing for aggregating
*
*   e.g) Calculate total payment for each target field
*
*/

tqb.setIndexing({
  field: 'price',
  method: 'sum',
  interval: 300
});

/**
*
* Aggregating
*   Aggregate using by indexing values
*
*   e.g) Count users by total payment ranges
*
*/

tqb.setAggregating({
  field: 'user_id',
  method: 'count'
});

const query = tqb.build();

Generated SQL

SELECT COUNT(`user_id`) AS "value", FLOOR(`indexed_value_0` / 300) AS "category" FROM (SELECT SUM(`indexed_value_0`) AS "indexed_value_0", `user_id` FROM (SELECT price AS "indexed_value_0", user_id FROM payment_logs WHERE ("2017-01-01 00:00:00" <= timestamp AND timestamp <= "2017-01-03 23:59:59")) `matching_table` GROUP BY `user_id`) `indexing_table` GROUP BY FLOOR(`indexed_value_0` / 300)

Query result

mysql> select * from payment_logs;
+----+---------+-------+---------------------+
| id | user_id | price | timestamp           |
+----+---------+-------+---------------------+
|  1 |       1 |   100 | 2017-01-01 12:00:00 |
|  2 |       2 |   200 | 2017-01-01 12:00:00 |
|  3 |       1 |   300 | 2017-01-02 12:00:00 |
|  4 |       4 |    10 | 2017-01-02 15:00:00 |
|  5 |       4 |    50 | 2017-01-02 16:00:00 |
|  6 |       4 |   100 | 2017-01-03 18:00:00 |
|  7 |       5 |  1000 | 2017-01-04 19:00:00 |
|  8 |       1 |   500 | 2017-01-04 10:00:00 |
|  9 |       1 |   600 | 2017-01-05 11:00:00 |
| 10 |       3 |   800 | 2017-01-06 12:00:00 |
| 11 |       1 |   100 | 2017-01-06 13:00:00 |
+----+---------+-------+---------------------+
11 rows in set (0.06 sec)

mysql> SELECT COUNT(user_id) AS "value", FLOOR(indexed_value / 300) AS "category" FROM (SELECT SUM(price) AS "indexed_value", user_id FROM (SELECT price, user_id FROM payment_logs WHERE ("2017-01-01 00:00:00" <= timestamp AND timestamp <= "2017-01-03 23:59:59")) `matching_table` GROUP BY user_id) `indexing_table` GROUP BY FLOOR(indexed_value / 300);
+-------+----------+
| value | category |
+-------+----------+
|     2 |        0 |
|     1 |        1 |
+-------+----------+
2 rows in set (0.01 sec)

In this case

  • There are two users in range of 0-299 payment group.
  • There is one user in range of 300- payment group.