plotting – How can I label a table of histograms with the names of the data symbols, not the data themselves?

How can I label a table of histograms with the names of the data symbols, not the data themselves?

If I want to make a table of plots with each plot labeled with the name of the function it plots, this is simple:

f[x_] := x; g[x_] := x^2;
Table[Plot[p[x], {x, -1, 1}, PlotLabel -> p], {p, {f, g}}]

However, if I want to do the same for Histogram, I have not figured out how. I have tried various combinations using ToString, Hold, HoldForm, and Unevaluated, as well as ChartLabels. For example,

l = RandomReal[{0, 1}, 20]; m = RandomReal[{0, 1}, 20];
Table[Histogram[p, PlotLabel -> p], {p, {l, m}}]

does not work, as the labels are the values of l and m, not the strings l and m.

The same question applies to the format analogous to

Plot[#[x], {x, -1, 1}, PlotLabel -> #] & /@ {f, g}

which works for Plot but not for Histogram.

c++ – How can I clean up the code for making a 10×10 times table using two dimensional arrays

I have been on the C++ learning grind for the last 2 weeks using caveofprogramming videos so don’t judge too hard if this seems like a dumb question. My last lesson covered arrays and the assignment was to make a 10×10 times table using multidimensional arrays. I succeeded in making this times table but with my knowledge I had to type type out each integer. Is this the best way to handle this scenario or is there a way that I could calculate most of the integers without typing out 10 lines of arrays? I apologize if my terminology isn’t on point.

#include<iostream>
#include<Windows.h>
using namespace std;

int main() {
    int animals(10)(10) = {
        {1,2,3,4,5,6,7,8,9,10},
        {2,4,6,8,10,12,14,16,18,20},
        {3,6,9,12,15,18,21,24,27,30},
        {4,8,12,16,20,24,28,32,36,40},
        {5,10,16,20,25,30,35,40,45,50},
        {6,12,18,24,30,36,42,48,54,60},
        {7,14,21,28,35,42,49,56,63,70},
        {8,16,24,32,40,48,56,64,72,80},
        {9,18,27,36,45,54,63,72,81,90},
        {10,20,30,40,50,60,70,80,90,100},
    };                                          
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
            cout << animals(i)(j) << " " << flush;
        }
        cout << endl;
    }
    return 0;
}

mysql – Procedure insert into another table using cursor

I’m creating a procedure, using cursor, which checks in different tables if the current quantity of 5 products are below their minimum quantity. If the current quantity is below I create a PURCHASE ORDER (table). I’m having difficulty capturing the information from the different tables in the course to create the PURCHASE ORDER.

CREATE TABLE IF NOT EXISTS `purchase_order`(
    `id_purchase` INT(11) NOT NULL AUTO_INCREMENT,
    `description` VARCHAR(200) NOT NULL,
    `total_consolidated` DECIMAL NOT NULL,
    `was_canceled` SET('Y','N'),
    `branch_number` INT(14) NOT NULL,
    `date_accomplished` DATE NOT NULL,
    PRIMARY KEY(`id_purchase`),
    FOREIGN KEY(`branch_number`)
    REFERENCES `branch` (`branch_number`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

CREATE TABLE IF NOT EXISTS `item_product`(
    `lot_number` INT(11) NOT NULL AUTO_INCREMENT,
    `id_purchase` INT(11) NOT NULL,
    `quantity_requested` INT(11) NOT NULL,
    PRIMARY KEY(`lot_number`),
   FOREIGN KEY(`id_purchase`)
   REFERENCES `purchase_order` (`id_purchase`)
   ON DELETE NO ACTION
   ON UPDATE NO ACTION)
ENGINE = InnoDB;

CREATE TABLE IF NOT EXISTS `product`(
    `id_product`INT(11) NOT NULL AUTO_INCREMENT,  
    `minimum_quantity` INT(11) NOT NULL,    
    `lot_number` INT(11) NOT NULL,     
    PRIMARY KEY(`id_product`),
    FOREIGN KEY(`lot_number`)
    REFERENCES `item_product` (`lot_number`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)   
ENGINE = InnoDB;

CREATE TABLE IF NOT EXISTS `stock_product`(
    `id_stock_product`INT(11) NOT NULL AUTO_INCREMENT,    
    `current_quantity`INT(11) NOT NULL,   
    `id_product`INT(11) NOT NULL,    
    PRIMARY KEY(`id_stock_product`),
    FOREIGN KEY(`id_product`)
    REFERENCES `product` (`id_product`)    
    ON UPDATE CASCADE)
ENGINE = InnoDB;

DELIMITER $$
CREATE PROCEDURE sp_generate_purchase_order()
BEGIN
    DECLARE countN INT DEFAULT 0;
    DECLARE minimumQuantity INT DEFAULT 0;
    DECLARE currentQuantity INT DEFAULT 0;
    DECLARE curs CURSOR FOR SELECT  P.minimum_quantity, SP.current_quantity  from produc P inner join stock_product as SP
on P.id_product = SP.id_product;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;  
    OPEN curs;    
    FETCH curs INTO minimumQuantity, currentQuantity;
    WHILE countN != 5
        FETCH curs INTO minimumQuantity, currentQuantity;
        IF currentQuantity < minimumQuantity
        THEN /*create the purchase order*/;
        END IF;
        set countN = countN + 1;
    END WHILE;
    CLOSE curs;
    
END$$
DELIMITER ;

python – How to create new table with first name only in table

I have some data that looks like this:

+--------------------+--------------------+--------------------+--------------------+-----------+--------------------+-------+-------------+-------------+--------------------+-----------+------------+-----+-----+
|             address|          attributes|         business_id|          categories|       city|               hours|is_open|     latitude|    longitude|                name|postal_code|review_count|stars|state|
+--------------------+--------------------+--------------------+--------------------+-----------+--------------------+-------+-------------+-------------+--------------------+-----------+------------+-----+-----+
|2818 E Camino Ace...|[,,,,,,,,,,,,,,,,...|1SWheh84yJXfytovI...|   Golf, Active Life|    Phoenix|                null|      0|   33.5221425| -112.0184807|Arizona Biltmore ...|      85016|           5|  3.0|   AZ|
|30 Eglinton Avenue W|[,, u'full_bar', ...|QXAEGFB4oINsVuTFx...|Specialty Food, R...|Mississauga|[9:0-1:0, 9:0-0:0...|      1|43.6054989743|-79.652288909|Emerald Chinese R...|    L5R 3E7|         128|  2.5|   ON|
+--------------------+--------------------+--------------------+--------------------+-----------+--------------------+-------+-------------+-------------+--------------------+-----------+------------+-----+-----+

I’d like to create a new table with the name column but with the first name only.

MySQL DB Query to find all values within a subscription table where the column is many and all values

I’m struggling to articulate what I’m trying to achieve.

I have a product which can be assigned many tags, I then have a subscription table which holds the productID and the tagID.

I’d like to search the subscription table in the most efficient way to see if a product is subscribed to ALL tags (not ANYthat I provide).

I’ve tried many things including this:

SELECT 

productID

FROM productTagSubscription

WHERE tagID IN ( 'id1', 'id2', 'id3', 'id4');

So the product has to be subscribed to all 4 ID’s as 4 rows in the subscription table

This is then pulling back any records for ANY id, which isn’t what I’m trying to achieve – any advice appreciated!!

magento2 – Magento 2 | Join a table to getLoadedProductCollection()

I’m trying to join the tier price table to the getLoadedProductCollection () collection located in the list.phtml file.

What I would like to do is filter the products in the current category by tier price like so:

$joinConditions = 'e.entity_id = tier.entity_id';
    $_productCollection = $block->getLoadedProductCollection();
    $_productCollection->getSelect()->join(
             'catalog_product_entity_tier_price as tier',
             $joinConditions
            )->where('tier.all_groups = 0')
              ->where('tier.customer_group_id = 0')
              ->where('tier.qty <= 1000')
              ->where('tier.value <= 0.70')
              ->where('tier.website_id = 0');

The following code does not generate an error but does not apply any filters and I keep seeing the same products over and over.

Where is the mistake?

mysql – Creating Tables with the same table names from other databases doesn’t have any bad effects?

I have a WordPress website and I cloned it to another website(domain) using a plugin called Duplicator. So in the backend, phpMyAdmin, I’ve two different databases but all the tables in both databases have the same names.

enter image description here

I just want to make sure that this does not have any kind of bad effects.

formulas – Google Sheets – How can i collect and add values related to a value next to it in a table and add them to another table

I’m using app sheets to see what I do in a day. I select the task i am starting on the app and app sheets dumps the data into a table in google sheets.

I have made an example of what this looks like. Sheet1 is what it looks like At the moment and sheet2 is what I would like it to look like.

https://docs.google.com/spreadsheets/d/1GF6cjlNMSTJov4G6ZMusXSWS-DMwnK4SgQqNs8d8uws/edit?usp=sharing

I can get the dates in just fine using the UNIQUE function but I’m struggling with the values.

If you have any questions or answers please tell me as I have been pulling my hair out trying to do this.

mysql – 8 vCPU, 32 GB memory, 250 GB SSD server needs 1 to 2 seconds for a query with 12 SUM functions and a JOIN on a table with 80,000 rows

I was recommended that I ask my question here at DBA. Sorry, should that be considered “double posting”. A bit of information in advance:

I have already made the following settings (my.cnf):

innodb_buffer_pool_size = 8G
innodb_file_per_table = 1
innodb_log_buffer_size = 16M
innodb_log_file_size = 1G
innodb_read_io_threads = 64
innodb_write_io_threads = 64
innodb_sort_buffer_size = 2M
join_buffer_size = 4M
read_buffer_size = 3M
read_rnd_buffer_size = 4M
sort_buffer_size = 4M

Also, I now do the calculation before the “GROUP BY”. Means, I make the calculation in a SELECT ... FROM (SELECT ... GROUP BY ...) t and then I use JOIN. Now to the actual topic…


I have the following 2 tables (browsers and metrics). browsers is a “dimensions table” which stores the name and version of a browser. metrics is a “facts table” which holds the browser_id and metrics, in conjunction with a date. According to explain select (...) no key is used on metrics and the primary key is used on browsers.

--- old query
SELECT browsers.name AS browser_name,
    SUM(visits_count) AS visits_count,
    SUM(clicks_count) AS clicks_count,
    IFNULL((100 / SUM(visits_count)) * SUM(clicks_count), 0) AS ctr,
    SUM(cost_integral) AS cost_integral,
    IFNULL((SUM(cost_integral) / SUM(visits_count)), 0) AS cpv_integral,
    IFNULL((SUM(cost_integral) / SUM(clicks_count)), 0) AS cpc_integral,
    SUM(conversions_count) AS conversions_count,
    IFNULL((100 / SUM(clicks_count)) * conversions_count, 0) AS cvr,
    SUM(revenue_integral) AS revenue_integral,
    IFNULL((SUM(revenue_integral) / SUM(clicks_count)), 0) AS epc_integral,
    (SUM(revenue_integral) - SUM(cost_integral)) AS profit_integral,
    IFNULL((SUM(revenue_integral) - SUM(cost_integral)) / SUM(cost_integral) * 100, 0) AS roi
FROM metrics
JOIN browsers ON browsers.id = browser_id
GROUP BY browsers.name
--- new query
SELECT browsers.*, `t`.*
FROM (
    SELECT browser_id,
        SUM(visits_count) AS visits_count,
        SUM(clicks_count) AS clicks_count,
        IFNULL((100 / SUM(visits_count)) * SUM(clicks_count), 0) AS ctr,
        SUM(cost_integral) AS cost_integral,
        IFNULL((SUM(cost_integral) / SUM(visits_count)), 0) AS cpv_integral,
        IFNULL((SUM(cost_integral) / SUM(clicks_count)), 0) AS cpc_integral,
        SUM(conversions_count) AS conversions_count,
        IFNULL((100 / SUM(clicks_count)) * SUM(conversions_count), 0) AS cvr,
        SUM(revenue_integral) AS revenue_integral,
        IFNULL((SUM(revenue_integral) / SUM(visits_count)), 0) AS epv_integral,
        IFNULL((SUM(revenue_integral) / SUM(clicks_count)), 0) AS epc_integral,
        (SUM(revenue_integral) - SUM(cost_integral)) AS profit_integral,
        ifnull((SUM(revenue_integral) - SUM(cost_integral)) / SUM(cost_integral) * 100, 0) AS roi
    FROM `metrics`
    GROUP BY `browser_id`
    ) AS `t`
INNER JOIN `browsers` ON `browsers`.`id` = `browser_id`

Server:

  • 8 vCPU, 32 GB Memory, 250 GB SSD
  • MySQL 8

Without all the SUM functions, the time of 900ms is reduced by about 250 to 300ms. Without the GROUP BY even down to 1 to 2 digit ms. Unfortunately I need the GROUP BY, as well as the number of SUM functions.

What can be the reason that such a server needs between 1 second and 2 seconds to execute the query on a table with only 80,000 rows? According to explain analyze the SUM functions need 96% of the time (actual time=845.038..845.052) that is needed in total.

-- browsers-Table

CREATE TABLE `browsers` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `name` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
  `version` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

ALTER TABLE `browsers`
  ADD PRIMARY KEY (`id`),
  ADD KEY `b_n` (`name`),
  ADD KEY `b_v` (`version`),
  ADD KEY `b_n_v` (`name`,`version`),
  ADD KEY `b_v_n` (`version`,`name`);

ALTER TABLE `browsers`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;
-- metrics-Table

CREATE TABLE `metrics` (
  `reference_date` date NOT NULL,
  `browser_id` bigint(20) UNSIGNED NOT NULL,
  `visits_count` bigint(20) NOT NULL DEFAULT 0,
  `cost_integral` bigint(20) NOT NULL DEFAULT 0,
  `clicks_count` bigint(20) NOT NULL DEFAULT 0,
  `conversions_count` bigint(20) NOT NULL DEFAULT 0,
  `revenue_integral` bigint(20) NOT NULL DEFAULT 0
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

ALTER TABLE `metrics`
  ADD UNIQUE KEY `mu` (`reference_date`,`browser_id`),
  ADD KEY `metrics_browser_id_foreign` (`browser_id`);

ALTER TABLE `metrics`
  ADD CONSTRAINT `metrics_browser_id_foreign` FOREIGN KEY (`browser_id`) REFERENCES `browsers` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;

Even on my localserver, with the same data, I need only ~10ms – so I suspect a faulty setting of the server (according to mysqltuner there are no remarkable suggestions).


Edit #1:

| -> Nested loop inner join  (actual time=182.931..182.937 rows=1 loops=1)
    -> Table scan on landing_pages  (cost=0.35 rows=1) (actual time=0.016..0.020 rows=1 loops=1)
    -> Index lookup on t using <auto_key0> (landing_page_id=landing_pages.id)  (actual time=0.003..0.004 rows=1 loops=1)
        -> Materialize  (actual time=182.911..182.912 rows=1 loops=1)
            -> Table scan on <temporary>  (actual time=0.001..0.002 rows=1 loops=1)
                -> Aggregate using temporary table  (actual time=182.830..182.830 rows=1 loops=1)
                    -> Index lookup on metrics using metrics_campaign_id_foreign (campaign_id=1)  (cost=2065.15 rows=18004) (actual time=0.124..44.976 rows=36266 loops=1)```

Tree-structured filter and related table columns

I’m struggling with the logic of a filtering option. In that filter I have multiple roots and nodes (see example below):

root structured filter

The user can interact with the filter and select either an entire root and all its nodes (e.g. Fruit), or just some nodes inside one root (e.g. Apple and Zucchini).

In the table where the selected options will be filtered for, I would like to display roots and nodes in two distinct columns, like so:

enter image description here

The first column (e.g. Groceries category) is the one the user will go check most of the time to have a high overview of the elements in the table, but I don’t know if it makes sense to have two columns for one filter.

Thanks!