对于 sqlite 我想找到一种方法来删除此结果中的重复项(请参阅 this gist 或 this sqime.org demo)
有些功能适用于所有产品,有些功能仅适用于某些产品。
2024 B12XBE A1C B1 B1 hardened steal UT gehärteter Stahl
两个 feature_text 都应以 UT
但它包含重复的行。
这个sql可以作为示例
DROP TABLE IF EXISTS product_features;
DROP TABLE IF EXISTS feature_text;
CREATE TABLE product_features (
year INTEGER, -- (4)
product TEXT, -- (6)
feature TEXT -- (3)
);
CREATE TABLE feature_text (
year INTEGER, -- (4)
product_group TEXT,
-- (2) usually matches the first two digits of product
-- but is sometimes empty / NULL or blank ' '
feature TEXT, -- (3)
language INTEGER, -- (2)
description1 TEXT -- up to 40
);
插入一些记录
INSERT INTO product_features (year, product, feature) VALUES
(2024, 'B12XBE', 'A1C'), (2024, 'B12XBE', '7B0'),
(2024, 'B12XBE', 'DUL'), (2024, 'UTB2EF', 'A1C'),
(2024, 'UTB2EF', '7B0'), (2024, 'UTB2EF', 'DUL'),
(2024, 'X9Y8Z7', 'DUH'), (2024, 'X9Y8Z7', '7B0');
INSERT INTO feature_text
(year, product_group, feature, language, description1)
VALUES
(2024, 'B1', 'A1C', 1, 'B1 hardened steel'),
(2024, 'B1', '7B0', 1, 'B1 diamond tip'),
(2024, 'UT', 'A1C', 1, 'UT hardened steel'),
(2024, 'UT', '7B0', 1, 'UT diamond tip'),
(2024, ' ', 'DUL', 1, '10mm for light duty'),
(2024, 'X9', '7B0', 1, 'X9 diamond tip'),
(2024, ' ', 'DUH', 1, '13mm for heavy duty'),
(2024, 'B1', 'A1C', 2, 'B1 gehärteter Stahl'),
(2024, 'B1', '7B0', 2, 'B1 Diamant Spitze'),
(2024, 'UT', 'A1C', 2, 'UT gehärteter Stahl'),
(2024, 'UT', '7B0', 2, 'UT Diamant Spitze'),
(2024, ' ', 'DUL', 2, '10mm für leichte Aufgaben'),
(2024, 'X9', '7B0', 2, 'X9 Diamant Spitze'),
(2024, ' ', 'DUH', 2, '13mm für schwere Aufgaben');
我目前连接表格并将每种语言放在不同列中的方法
WITH prft_en as (
SELECT
pf.year, pf.product, pf.feature,
COALESCE(ft1.product_group, ft2.product_group) AS product_group,
COALESCE(ft1.description1, ft2.description1) AS feature_text_en,
COALESCE(ft1.language, ft2.language) AS language_en
FROM product_features pf
LEFT JOIN feature_text ft1
ON pf.year = ft1.year
AND pf.feature = ft1.feature
AND substr(pf.product, 1,2) = ft1.product_group
LEFT JOIN feature_text ft2
ON pf.year = ft2.year
AND pf.feature = ft2.feature
AND ft2.product_group is ' ' OR ft2.product_group is NULL
WHERE ft1.language = 1 OR ft2.language = 1
), prft_de AS (
SELECT
pf.year, pf.product, pf.feature,
COALESCE(ft1.product_group, ft2.product_group) AS product_group,
COALESCE(ft1.description1, ft2.description1) AS feature_text_de,
COALESCE(ft1.language, ft2.language) AS language_de
FROM product_features pf
LEFT JOIN feature_text ft1
ON pf.year = ft1.year
AND pf.feature = ft1.feature
AND substr(pf.product, 1,2) = ft1.product_group
LEFT JOIN feature_text ft2
ON pf.year = ft2.year
AND pf.feature = ft2.feature
AND ft2.product_group is ' ' OR ft2.product_group is NULL
WHERE ft1.language = 2 OR ft2.language = 2
)
对上面创建的 cte 的查询
SELECT prft_en.year, prft_en.product,
prft_en.feature, prft_en.product_group,
prft_en.feature_text_en, prft_de.feature_text_de
FROM prft_en
LEFT JOIN prft_de
ON prft_en.year = prft_de.year
AND prft_en.feature = prft_de.feature
AND SUBSTR(prft_en.product, 1,2) = prft_en.product_group
我认为整个 CTE 的东西确实不是你想要的。我认为您想要每个产品一行,所以从那开始
SELECT
f.*,
substring(f.product, 0, 3) as product_group
from product_features f
然后对于每个产品,您想要获取相应的描述(如果存在),这可以简单地在英语子查询中完成:
(
select description1
from feature_text t
where f.year = t.year
and (substring(f.product, 0, 3) = t.product_group or t.product_group = ' ' or t.product_group is null)
and f.feature = t.feature and language = 1
) as feature_text_en,
德语:
(
select description1
from feature_text t
where f.year = t.year
and (substring(f.product, 0, 3) = t.product_group or t.product_group = ' ' or t.product_group is null)
and f.feature = t.feature and language = 2
) as feature_text_de
这是 两者都在 sqlime 上 并生成此结果表:
年 | 产品 | 特点 | 产品组 | feature_text_en | feature_text_de |
---|---|---|---|---|---|
2024 | B12XBE | A1C | B1 | B1硬化偷窃 | B1 斯塔尔 |
2024 | B12XBE | 7B0 | B1 | B1钻石尖头 | B1 钻石斯皮策 |
2024 | B12XBE | DUL | B1 | 10mm 适用于轻型 | 10mm 轻量级 |
… |