0

我有一个如下所示的采购表:

store_id.   industry_code    amt_age_18_24    amt_age_25-34    amt_men    amt_women
       1             1000              100               20         80           40
       2             2000              100              100        130           70

我想要做的是找到每家商店按年龄和性别购买的每一个排列。像这样,每一行都是唯一的:

store_id.   industry_code    amt_age_18_24    amt_age_25-34    amt_men    amt_women
       1             1000              100             NULL         80          NULL
       1             1000              100             NULL        NULL           40
       1             1000              NULL            20           80          NULL
       1             1000              NULL            20          NULL           80
       2             2000              100             NULL        130          NULL
       2             2000              100             NULL        NULL           70
       2             2000              NULL            100         130          NULL
       2             2000              NULL            100         NULL           70

最好的方法是什么?自加入?

4

3 回答 3

2

这看起来像union all

select store_id, instrustry_code, amt_age_18_24, null as amt_age_25_34, amt_men, null as amt_women
from t
union all
select store_id, instrustry_code, amt_age_18_24, null as amt_age_25_34, null as amt_men, amt_women
from t
union all
. . . 
于 2020-09-15T17:21:26.503 回答
1

这是一种将 across join与包含“列掩码”的派生表一起使用的方法:

select 
    t.store_id, 
    t.industry_code, 
    t.amt_age_18_24 * x.amt_age_18_24 as amt_age_18_24,
    t.amt_age_25_34 * x.amt_age_25_34 as amt_age_25_34,
    t.amnt_men      * x.amnt_men      as amnt_men,
    t.amt_women     * x.amt_women     as amt_women
from mytable t
cross join (
    select 1 as amt_age_18_24, null as amt_age_25_34, 1 as amnt_men, null as amt_women
    union all select 1, null, null, 1
    union all select null, 1, 1, null
    union all select null, 1, null, 1
) x

好处是,与方法相反,这不需要多次扫描表union all

于 2020-09-15T17:22:44.300 回答
1

您可以根据需要对每个排列使用 union:

select store_id, instrustry_code, amt_age_18_24, null as amt_age_25_34, amt_men, null as amt_women
from t
union all
select store_id, instrustry_code, amt_age_18_24, null as amt_age_25_34, null as amt_men, amt_women
from t

并为尽可能多的列执行此操作

于 2020-09-15T17:25:48.413 回答