0

我有一个包含调查数据的表,两个数据点是 Ethnicity 和 Flavor_Pref。Flavor_Pref 保存一个整数 1、2、3、4、5。1 = 非常不喜欢,5 非常喜欢。

 Ethnicity            Flavor_Pref
 African American         3
 Caucasian                2
 Asian                    4
 Hispanic                 1
 African American         3
 Caucasian                4
 Asian                    5
 Hispanic                 2
 African American         4
 Caucasian                1
 Asian                    4
 Hispanic                 2
 African American         3
 Caucasian                2
 Asian                    2
 Hispanic                 1

我想运行一个查询来获得 4 列,每个民族一个。每个组有不同数量的响应。

这就是我正在做的事情:我得到了超过 100 万个结果……我只有 400 个调查。

  select  AA.Flavor_Pref as AA,H.Flavor_Pref as H,C.Flavor_Pref AS C,
  A.Flavor_Pref AS A  from
  (SELECT ETHNICITY,Flavor_Pref FROM FLAVORS WHERE ETHNICITY = 'AFRICAN AMERICAN')AS AA
  CROSS JOIN
  (SELECT ETHNICITY,Flavor_Pref FROM FLAVORS WHERE  ETHNICITY = 'HISPANIC') AS H
  CROSS JOIN 
  (SELECT ETHNICITY,Flavor_Pref FROM FLAVORS WHERE ETHNICITY = 'CAUCASIAN') AS C
  CROSS JOIN 
  (SELECT ETHNICITY,Flavor_Pref FROM FLAVORS WHERE  ETHNICITY = 'ASIAN' ) AS A

我正在寻找的是:在这种情况下,西班牙裔结果较少,因此没有报告任何内容。

   African Americans            Hispanic        Caucasian          Asian
         3                         1                2               4
         3                         2                4               5
         4                         2                1               5
         .                         .                .               .
         .                         .                .               .
         .                         .                .               .
         3                                          2               4
         2                                          1               1  
4

2 回答 2

1

What you are looking for is PIVOTing rows in to columns. Here is the standard way to do this, it will for all the RDBMS:

SELECT 
  MAX(CASE WHEN Ethnicity = 'African American' THEN Flavor_Pref END)
     AS 'African Americans',
  MAX(CASE WHEN Ethnicity = 'Hispanic' THEN Flavor_Pref END)
     AS 'Hispanic',
  MAX(CASE WHEN Ethnicity = 'Caucasian' THEN Flavor_Pref END)
     AS 'Caucasian',
  MAX(CASE WHEN Ethnicity = 'Asian' THEN Flavor_Pref END)
     AS 'Asian'
FROM @flavors 
GROUP BY Flavor_Pref

Here is a live demo

于 2012-10-01T13:30:38.847 回答
1

您想要每个种族最受欢迎的口味。您可以使用排名函数在大多数数据库中执行此操作:

select max(case when ethnicity = 'African American' and seqnum = 1 then flavor_pref end) as AfricanAmerican,
       max(case when ethnicity = 'Hispanic' and seqnum = 1 then flavor_pref end) as Hispanic,
       max(case when ethnicity = 'Caucasian' and seqnum = 1 then flavor_pref end) as Caucasian,
       max(case when ethnicity = 'Asian' and seqnum = 1 then flavor_pref end) as Asian
from (select t.ethnicity, t.flavor_pref, cnt,
             row_number() over (partition by t.enthnicity order by cnt desc) as seqnum
      from (select t.ethnicity, t.flavor_pref, count(*) as cnt
            from t
            group by t.ethnicity, t.flavor_pref
           ) t
     ) ts
于 2012-10-01T13:43:08.903 回答