在SQL中创建详尽的案例列

时间:2018-08-20 00:18:21

标签: sql sql-server combinations interaction generate

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

+--------------+--------------+--------------+---------------------+
| 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           |
+--------------+--------------+--------------+---------------------+

3 个答案:

答案 0 :(得分: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

我相信您可以在此基础上进行扩展以很容易地匹配您的示例。

答案 1 :(得分: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;

答案 2 :(得分: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;

在case语句处添加了小的更改...并在表值构造函数中进行了编辑。.感谢您的出色解决方案...