0

我有一张名为“学生列”的表格:学生姓名、大学和技能。Student Name 是 varchar 类型,Skills 以 CSV 格式包含学生的技能。此外,性别列有“M”或“F”,例如:

College Skills                  Student Name
ABC     C,Python,Python,JAVA    S1
ABC     C,Python,JAVA,CSS       S2
ABC     C,Python,JAVA,JS,C#     S3
ABC     C,Python,JAVA           S4
XYZ     C,Python,C#             S5
XYZ     C,Python,C#             S6
XYZ     C,Python,C#             S7
UT      C,SQL,JS,CSS            S8

我想显示这样的表格

College   Men Women  C     C++     C#      Python      JAVA      SQL        JS        CSS
ABC       2     2    4     0       1         4         4        0            1         1
XYZ       1     2    3     0       3         3         0        0            0         0
UT        1     0    1     0       0         0         0        1            1         1

我该怎么做呢?

我做了一个查询,将所有拥有 SKILLS 的学生显示为 1 或 0。但这会导致很多重复。

我在 select 中使用了 IIF 语句来获取列 Skills Columns 并且每一行都是一个学生。但我希望每一行都是学院而不是学生。

4

2 回答 2

2

基于您之前问题的答案,修改后的查询

SELECT
    [Student Name],
    College,
    IIf(Skillz LIKE "*,C,*",1,0) AS _C,
    IIf(Skillz LIKE "*,Python,*",1,0) AS _Python,
    IIf(Skillz LIKE "*,JAVA,*",1,0) AS _JAVA,
    IIf(Skillz LIKE "*,CSS,*",1,0) AS _CSS,
    IIf(Skillz LIKE "*,JS,*",1,0) AS _JS,
    IIf(Skillz LIKE "*,C#,*",1,0) AS _CSharp,
    IIf(Skillz LIKE "*,SQL,*",1,0) AS _SQL
FROM
    (
        SELECT 
            [Student Name],
            College,
            "," & Skills & "," AS Skillz
        FROM Students
    )

返回

Student Name  College  _C  _Python  _JAVA  _CSS  _JS  _CSharp  _SQL
------------  -------  --  -------  -----  ----  ---  -------  ----
S1            ABC       1        1      1     0    0        0     0
S2            ABC       1        1      1     1    0        0     0
S3            ABC       1        1      1     0    1        0     0
S4            ABC       1        1      1     0    0        0     0
S5            XYZ       1        1      0     0    0        0     0
S6            XYZ       1        1      0     0    0        0     0
S7            XYZ       1        1      0     0    0        0     0
S8            UT        1        0      0     1    1        0     1

所以,现在我们需要做的就是添加一个外部查询来将值相加

SELECT 
    College, 
    SUM([_C]) AS C,
    SUM([_Python]) AS Python,
    SUM([_JAVA]) AS JAVA,
    SUM([_CSS]) AS CSS,
    SUM([_JS]) AS JS,
    SUM([_CSharp]) AS CSharp,
    SUM([_SQL]) AS SQL
FROM
    (
        SELECT
            [Student Name],
            College,
            IIf(Skillz LIKE "*,C,*",1,0) AS _C,
            IIf(Skillz LIKE "*,Python,*",1,0) AS _Python,
            IIf(Skillz LIKE "*,JAVA,*",1,0) AS _JAVA,
            IIf(Skillz LIKE "*,CSS,*",1,0) AS _CSS,
            IIf(Skillz LIKE "*,JS,*",1,0) AS _JS,
            IIf(Skillz LIKE "*,C#,*",1,0) AS _CSharp,
            IIf(Skillz LIKE "*,SQL,*",1,0) AS _SQL
        FROM
            (
                SELECT 
                    [Student Name],
                    College,
                    "," & Skills & "," AS Skillz
                FROM Students
            )
    )
GROUP BY College

...返回:

College  C  Python  JAVA  CSS  JS  CSharp  SQL
-------  -  ------  ----  ---  --  ------  ---
ABC      4       4     4    1   1       0    0
UT       1       0     0    1   1       0    1
XYZ      3       3     0    0   0       0    0

注意:如您所见,使用当前形式的数据将继续使您的生活变得比实际需要的更难。也许你会认为这是另一个强烈的暗示,你应该修复你的数据模型!

于 2013-10-17T21:03:30.927 回答
0

在单个字段中存储逗号分隔值绝不是一个好主意。这有时被称为 SQL 反模式。您正在使用关系数据库,因此使其具有关系。

创建一个名为 Skills 的单独表,并在其中放置一个与您的 studentId 字段匹配的外键字段。

对于最终结果,要获得您想要的那种视图,我认为您需要使用交叉表查询。

于 2013-10-17T20:56:22.827 回答