如果出现多个值,如何强制输出仅显示 1 行
How to force output to show only 1 row if multiple values appear
我有以下查询:
SELECT
item_code,
description_global,
dept_item || class1_item AS division,
view_year_code AS year,
ssn AS season
FROM
us_raw.l_ims_sum_code_master
WHERE
ssn IN (3,4)
AND view_year_code = 9
AND description_global IS NOT NULL
AND item_code = 418251
这是它在输出中的样子:
我只希望我的数据使用其中一个 description_global 值。我知道我只能做 LIMIT 1,但我需要另一种方法来做到这一点,因为这只是一个例子,我希望我的输出每次有多个 description_global 值时只强制输出一行。
谢谢,
Z
您可以使用 window 函数:
SELECT *
FROM (SELECT item_code, description_global, dept_item || class1_item AS division,
view_year_code AS year, ssn AS season,
ROW_NUMBER() OVER (PARTITION BY item_code ORDER BY item_code) as seqnum
FROM us_raw.l_ims_sum_code_master
WHERE ssn IN (3,4) AND
view_year_code = 9 AND
description_global IS NOT NULL AND
item_code = 418251
) scm
WHERE seqnum = 1;
我能够通过在 Tableau 中使用索引来解决这个问题。以下是任何使用 SQL+Tableau 的人的步骤:
1.) Create a calculated field
2.) Simply enter Index()
3.) Click OK
4.) Drag the newly created field into your rows
5.) Right-click on the Index pill (whatever you had named it) and click compute on
6.) Select whichever field that has multiple values (item description for me)
7.) Right-click again and click filter
8.) Filter to only show 1s
9.) Remove from rows (if you don't want it on your report)
10.) Done
最好的,
Z
我有以下查询:
SELECT
item_code,
description_global,
dept_item || class1_item AS division,
view_year_code AS year,
ssn AS season
FROM
us_raw.l_ims_sum_code_master
WHERE
ssn IN (3,4)
AND view_year_code = 9
AND description_global IS NOT NULL
AND item_code = 418251
这是它在输出中的样子:
我只希望我的数据使用其中一个 description_global 值。我知道我只能做 LIMIT 1,但我需要另一种方法来做到这一点,因为这只是一个例子,我希望我的输出每次有多个 description_global 值时只强制输出一行。
谢谢, Z
您可以使用 window 函数:
SELECT *
FROM (SELECT item_code, description_global, dept_item || class1_item AS division,
view_year_code AS year, ssn AS season,
ROW_NUMBER() OVER (PARTITION BY item_code ORDER BY item_code) as seqnum
FROM us_raw.l_ims_sum_code_master
WHERE ssn IN (3,4) AND
view_year_code = 9 AND
description_global IS NOT NULL AND
item_code = 418251
) scm
WHERE seqnum = 1;
我能够通过在 Tableau 中使用索引来解决这个问题。以下是任何使用 SQL+Tableau 的人的步骤:
1.) Create a calculated field
2.) Simply enter Index()
3.) Click OK
4.) Drag the newly created field into your rows
5.) Right-click on the Index pill (whatever you had named it) and click compute on
6.) Select whichever field that has multiple values (item description for me)
7.) Right-click again and click filter
8.) Filter to only show 1s
9.) Remove from rows (if you don't want it on your report)
10.) Done
最好的, Z