Prevent HTML table to use full page width in Quarto - html

I would like to use HTML tables in Quarto, but I would like to prevent it using the full page width. Here is a reproducible example:
---
title: "HTML tables in Quarto"
format: html
---
| fruit | price |
|--------|--------|
| apple | 2.05 |
| pear | 1.37 |
| orange | 3.09 |
: Table 1
| fruit | price |
|--------|--------|
| apple | 2.05 |
| pear | 1.37 |
| orange | 3.09 |
: Table 2 {tbl-colwidths="[75,25]"}
Output:
As we can see, table 1 is normal and you can see it takes the full page width. Table 2 has tbl-colwdiths options, but this doesn't make the table smaller in width and is also the full page width. So I was wondering if anyone knows how to prevent the HTML table to use the full page width in Quarto?

Maybe use the css grid system:
---
title: "HTML tables in Quarto"
format: html
---
| fruit | price |
|--------|--------|
| apple | 2.05 |
| pear | 1.37 |
| orange | 3.09 |
: Table 1
::: {.grid}
::: {.g-col-7}
| fruit | price |
|--------|--------|
| apple | 2.05 |
| pear | 1.37 |
| orange | 3.09 |
: Table 2
:::
:::
Edit: If you want to center, you could utilize the default of twelve columns in the css grid, e.g.
::: {.grid}
::: {.g-col-2}
:::
::: {.g-col-8}
| fruit | price |
|--------|--------|
| apple | 2.05 |
| pear | 1.37 |
| orange | 3.09 |
: Table 2
:::
::: {.g-col-2}
:::
:::

One possible option to use .columns div to get column layout
---
title: "HTML tables in Quarto"
format: html
engine: knitr
---
:::: {.columns}
::: {.column width="70%"}
| fruit | price |
|--------|--------|
| apple | 2.05 |
| pear | 1.37 |
| orange | 3.09 |
: Table 1
:::
::::
```{css, echo=FALSE}
.center-table table {
width: 50%;
margin-left: auto;
margin-right: auto;
}
```
::: {.center-table}
| fruit | price |
|--------|--------|
| apple | 2.05 |
| pear | 1.37 |
| orange | 3.09 |
: Table 2
:::

Related

Count of unique characteristics in duplicate rows table

My table is having duplicate rows listed based on a duplicates ID column. The duplicate rows may have one or more Characteristic columns having unique values. I am trying to get a count of which Characteristic columns in duplicate rows have unique values.
Before:
+-----+----------+-------------+-----------+------------+
| ID | charType | charFlavour | charColor | charWeight |
+-----+----------+-------------+-----------+------------+
| 123 | gel | mint | blue | 10gms |
| 123 | liquid | mint | blue | 10gms |
| 123 | solid | mint | blue | 10gms |
| 456 | wood | orange | red | 20gms |
| 456 | wood | vanilla | red | 20gms |
| 456 | wood | raspberry | red | 20gms |
| 456 | wood | strawberry | red | 20gms |
| 789 | metal | mango | yellow | 25gms |
| 789 | metal | mango | yellow | 30gms |
| 789 | metal | mango | yellow | 22gms |
| 333 | silica | NA | magenta | 11gms |
| 333 | plastic | NA | white | 11gms |
| 333 | rubber | NA | teal | 11gms |
+-----+----------+-------------+-----------+------------+
After:
+-------------+-----+-----+-----+-----+-------+
| ID | 123 | 456 | 789 | 333 | Total |
+-------------+-----+-----+-----+-----+-------+
| charType | 1 | 0 | 0 | 1 | 2 |
| charFlavour | 0 | 1 | 0 | 0 | 1 |
| charColor | 0 | 0 | 0 | 1 | 1 |
| charWeight | 0 | 0 | 1 | 0 | 1 |
+-------------+-----+-----+-----+-----+-------+
Is this format possible using a Pivot-table or Google Query?
Perhaps this isn't the most elegant solution you were looking for — it appears that no function in Google's query language returns all the unique values of a column. But this solution should successfully count, for each attribute, how many IDs correspond to more than one value of that attribute. For example, it will count how many IDs correspond to multiple charFlavours. Here are the two steps/queries to make:
=QUERY(A1:E, "select A, max(B), min(B), max(C), min(C), max(D), min(D), max(E), min(E) group by A", 1): This will select the alphabetically/numerically maximum and minimum value for each attribute with respect to each ID. It will return one row per ID, containing the min and max attribute values.
For each attribute, use something like =QUERY(G1:O, "select count(G) where H != I", 1). If you have four attributes, you will need four of these calls; just change where H != I to be the two columns corresponding to each attribute. Each of these QUERY calls will generate a table with just one value, the number of IDs having multiple values for a certain attribute.

MYSQL multi JOIN from different tables

I have multiple MYSQLI tables for multiple details and options about a touristic package.
I have package containing the main information, then I have package_option which contains unknown number of options added to the package, and there's package_images which contains the images.
I want to get all the information in one query, but it's prooving a difficult task. Let me explain.
I've used JOIN with but the problem was if the package_option had 3 options and the package_images had 6 images, the result was a 6*4 plus 1 (package) result table.
I would like a result containing all the fields, as many as they originaly are in the tables, not multiplied for each match in every table.
For example, the table I aim for looks like that:
-------------------------------------------------------------------------
| ID | name | description | price | option | image | size | explanation |
-------------------------------------------------------------------------
| 1 | test | some desc | 50 $ | opt. 1 | img1 | 30 | |
| | | | | opt. 2 | img2 | | |
| | | | | opt. 3 | img3 | | |
| | | | | | img4 | | |
| | | | | | img5 | | |
| | | | | | img6 | | |
Right now, the above table gets populated in every field, but it's multiplied, so I don't get only 6 rows, but 24.
I did a lot of JOINS, I'm not a beginer, which is even more frustrating, but it's the first time I try to do that, a select from multiple tables with different columns and unknown number of rows.
package_option and package_images looks like that:
-------------------------------------
| ID | package_id | option / image |
-------------------------------------
| 1 | 1 | opt. 1 / img 1 |
| 2 | 2 | opt. 2 / img 2 |
..... etc
so I don't know how many rows I'll have.
Thank you in advance for t

How to select a maximum value row in 2 mysql table?

I have 2 tables as follows:
Table: Visitor
---------------------
| Code |Contr|
|-------------------|
|225919528553 | 1003|
|-------------------|
|130324727131 | 1004|
|-------------------|
|353952972425 | 1010|
|-------------------|
|997498622785 | 1014|
|-------------------|
|
|
|
Table: Products
-----------------------------
| Code | Name| Color |
|----------------------------
|225919528553 | Pen | Balck |
|----------------------------
|130324727131 | Book| White |
|----------------------------
|353952972425 | Fan | Black |
|----------------------------
|997498622785 | DVD | Red |
|----------------------------
|
|
|
.
I want to more value of the "Contr" column following is displayed:
| Code | Name| Color | Contr |
|------------------------------------
|997498622785 | DVD | Red | 1014 |
|------------------------------------
|353952972425 | Fan | Black | 1010 |
|------------------------------------
|130324727131 | Book| White | 1004 |
|------------------------------------
|225919528553 | Pen | Balck | 1003 |
|------------------------------------
|
|
https://stackoverflow.com/revisions/f6f23a85-d8b8-4900-b72a-8ccb7b8abf25/view-source
try with
SELECT p.*, v.Contr
FROM Visitor AS v
JOIN Products AS p ON p.Code = v.Code
I would find more value in the table!?
select v.Code,
p.Name,
p.Color,
p.Contr
from
Visitor as v,
Products p
where
v.code=p.code order by contr

multilevel/nested headers in CSV files

How can I "make" multilevel/nested headers in CSV files please?
Example:
-----------------------------------------
| Big 1 | Big 2 |
-----------------------------------------
| Small A | Small B | Small C | Small D |
-----------------------------------------
| ... |

What is the benefit of using int instead of bigint in this case?

(MYSQL n00b)
I have 3 tables:
id = int(10), photo_id = bigint(20)
PHOTO records limited to 3 million
PHOTO:
+-------+-----------------+
| id | photo_num |
+-------+-----------------+
| 1 | 123456789123 |
| 2 | 987654321987 |
| 3 | 5432167894321 |
+-------+-----------------+
COLOR:
+-------+-----------------+---------+
| id | photo_num | color |
+-------+-----------------+---------+
| 1 | 123456789123 | red |
| 2 | 987654321987 | blue |
| 3 | 5432167894321 | green |
+-------+-----------------+---------+
SIZE:
+-------+-----------------+---------+
| id | photo_num | size |
+-------+-----------------+---------+
| 1 | 123456789123 | large |
| 2 | 987654321987 | small |
| 3 | 5432167894321 | medium |
+-------+-----------------+---------+
Both COLOR and SIZE tables will have several million records.
Q1: Is it better to change photo_num on COLOR and SIZE to int(10) and point it to PHOTO's id?
Right now I use these: (PHOTO is no where in the picture)
SELECT * from COLOR WHERE photo_num='xxx';
SELECT * from SIZE WHERE photo_num='xxx';
Q2: How will the SELECT query look if PHOTO id was used in COLOR, SIZE?
Q1: It is better to use an int as a foreign key, but far more important is to index the table correctly. If you have the correct indexes it probably will be good enough either way. For your query you need to make sure that photo_num is indexed on all tables.
I also wonder why you decided to split it up the table like this. Could a photo have more than one size or color? What is the purposes of the separate tables?
Q2: It will use a JOIN:
SELECT *
FROM color
JOIN photo
ON photo.id = color.photo_id
WHERE photo_num='xxx'
I'd go with:
PHOTO:
+-------+-----------------+----------+---------+
| id | photo_num | color_id | size_id |
+-------+-----------------+----------+---------+
| 1 | 123456789123 | 1 | 3 |
| 2 | 987654321987 | 1 | 2 |
| 3 | 5432167894321 | 2 | 2 |
+-------+-----------------+----------+---------+
COLOR:
+-------+---------+
| id | color |
+-------+---------+
| 1 | red |
| 2 | blue |
| 3 | green |
+-------+---------+
SIZE:
+-------+---------+
| id | size |
+-------+---------+
| 1 | large |
| 2 | small |
| 3 | medium |
+-------+---------+
And:
SELECT <Columns> FROM PHOTO JOIN COLOR ON PHOTO.color_id = COLOR.id JOIN SIZE ON PHOTO.size_id = SIZE.id
<Columns> perhaps being PHOTO.photo_num, COLOR.color, SIZE.size or what you need for the purpose.