解决SQL数组问题

时间:2014-02-04 20:33:00

标签: sql arrays

我知道这个问题可能已被多次询问过,但我找不到它们。我的原始表就像这样

    Location Language Component Score
    A        Eng         1        25
    A        Eng         2        30
    B        Eng         1        12
    B        Eng         2        50

预期产出:

    Location Language  Score1    Score2
    A        Eng         25       30
    B        Eng         12       50

谢谢!

2 个答案:

答案 0 :(得分:1)

SELECT 
  Location, 
  Language,
  SUM(CASE WHEN Component = 1 THEN Score ELSE 0 END) AS  Score1,
  SUM(CASE WHEN Component = 2 THEN Score ELSE 0 END) AS  Score2,
FROM MY_TABLE
GROUP BY Location, Language

答案 1 :(得分:0)

试试这个......

    SELECT DISTINCT S1.[Location], S1.[Language], S1.Score1, S2.Score2
    FROM
        (SELECT [Location], [Language], Score [Score1]
        FROM Scores WHERE Component = 1) S1
        LEFT JOIN 
        (SELECT [Location], [Language], Score [Score2]
        FROM Scores WHERE Component = 2) S2 ON S1.[Location] = S2.[Location] 
            AND S1.[Language] = S2.[Language]