0

我正在尝试生成一个列“性别组合”,它创建了详尽的交互术语类别,如下表所示。有没有一种简单的方法可以在 SQL(微软服务器)中做到这一点?

+--------------+--------------+--------------+---------------------+
| EMP 1 Gender | EMP 2 Gender | Emp 3 Gender | Gender Combinations |
+--------------+--------------+--------------+---------------------+
| Male         |              |              | 1 Male              |
| Female       |              |              | 1 Female            |
|              | Male         |              | 1 Male              |
|              | Female       |              | 1 Female            |
|              |              | Male         | 1 Male              |
|              |              | Female       | 1 Female            |
| Male         | Female       |              | 1 Male, 1 Female    |
|              | Male         | Female       | 1 Male, 1 Female    |
| Male         |              | Female       | 1 Male, 1 Female    |
|              | Female       | Male         | 1 Male, 1 Female    |
| Female       |              | Male         | 1 Male, 1 Female    |
| Female       | Male         |              | 1 Male, 1 Female    |
|              | Female       | Male         | 1 Male, 1 Female    |
| Male         |              | Female       | 1 Male, 1 Female    |
|              | Male         | Female       | 1 Male, 1 Female    |
| Male         | Male         | Female       | 1 Male, 1 Female    |
| Male         | Female       | Male         | 2 Males, 1 Female   |
| Female       | Male         | Male         | 1 Female, 2 Males   |
| Female       | Female       | Male         | 2 Females, 1 Male   |
| Female       | Male         | Female       | 2 Females, 1 Male   |
| Male         | Female       | Female       | 2 Females, 1 Male   |
| Male         | Male         | Male         | 3 Males             |
| Female       | Female       | Female       | 3 Females           |
+--------------+--------------+--------------+---------------------+
4

3 回答 3

1

我会使用apply

select t.*, g.gender_combination
from t cross apply
     (select ((case when num_males = 1 then '1 Male; '
                    when num_males > 1 then cast(varchar(255), num_males) + ' Males; '
                    else ''
              ) +
              (case when num_females = 1 then '1 Female; '
                    when num_females > 1 then cast(varchar(255), num_females) + ' Females; '
                    else ''
              )
             ) as gender_combination
      from (select sum(case when gender = 'Male' then 1 else 0 end) as num_males,
                   sum(case when gender = 'Female' then 1 else 0 end) as num_females                   
            from values (t.emp1_gender), (t.emp2_gender), (t.emp3_gender) as v(gender)
           ) v
     ) g;
于 2018-08-20T02:37:50.703 回答
1

像下面这样的东西可能会让你继续前进。您正在寻找 3 组数据的交叉连接/笛卡尔积。

 ;WITH emp1 AS
 (
   SELECT 'male' AS gender
   UNION 
   SELECT 'female'
   UNION 
   SELECT ''
  ),
  emp2 AS 
  (
   SELECT 'male' AS gender
   UNION 
   SELECT 'female'
    UNION 
   SELECT ''
  ),
  emp3 AS 
  (
   SELECT 'male' AS gender
   UNION 
   SELECT 'female'
   UNION 
   SELECT ''
  )
  SELECT *, emp1.gender+','+emp2.gender+','+emp3.gender
  FROM emp1
  CROSS JOIN emp2
  CROSS JOIN emp3
  ORDER BY emp1.gender DESC,emp2.gender DESC,emp3.gender DESC

我相信您可以对此进行扩展以非常轻松地匹配您的示例。

于 2018-08-20T01:07:05.313 回答
0
select t.*, g.gender_combination
from EMPGENDER t cross apply
     (select ((case when num_males = 1 then '1 Male; '
                    when num_males > 1 then cast(num_males AS varchar(255)) + ' Males; '
                    else ''
                END
              ) +
              (case when num_females = 1 then '1 Female; '
                    when num_females > 1 then cast(num_females AS varchar(255)) + ' Females; '
                    else ''
               END
              )
             ) as gender_combination
      from (select sum(case when gender = 'Male' then 1 else 0 end) as num_males,
                   sum(case when gender = 'Female' then 1 else 0 end) as num_females                   
            from (values (t.[EMP 1 Gender]), (t.[EMP 2 Gender]), 
            (t.[EMP 3 Gender])) as v(gender)
           ) v
     ) g;

small changes added at the places of case statements... and edited at table valued constructor.. Thanks for awesome solution...

于 2018-08-20T05:16:41.167 回答