0

我有以下查询:

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 值时我的输出仅强制 1 行。

谢谢你,Z

4

2 回答 2

0

您可以使用窗口函数:

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;
于 2019-11-27T22:56:50.287 回答
0

我能够通过在 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

于 2019-12-06T15:28:16.240 回答