简体   繁体   中英

USING SQL CROSS JOIN

I have a table that holds survey data, two data points are Ethnicity and Flavor_Pref. The Flavor_Pref hold an integer 1, 2, 3,4,5. 1 = Dislike Very Much, 5 Like Very Much.

 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

I want run a query to get 4 columns, one for each Ethnic group. Each group has a different number of responses.

This is what I'm working on: I get over 1M results.... I only have 400 surveys.

  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

What I'm looking for is: In this case there are fewer Hispanic results, so nothing is reported.

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

What you are looking for is PIVOT ing 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

You want the most popular flavor for each ethnicity. You can do this in most databases using the ranking functions:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM