SQL Query中的小变化

时间:2013-11-22 09:27:48

标签: sql sql-server sql-server-2008

这是我的表

CREATE TABLE [dbo].[marks_581](
[Name] [varchar](30) NOT NULL,
[Subject] [varchar](30) NOT NULL,
[Marks] [int] NOT NULL
)
INSERT INTO marks_581 
select 'Dishant','English',40 union all
select 'Dishant','Maths',45 union all
select 'Dishant','Hindi',49 union all
select 'Pranay','English',41 union all
select 'Pranay','Maths',45 union all
select 'Pranay','Hindi',50 union all
select 'Gautham','English',41 union all
select 'Gautham','Maths',45 union all
select 'Gautham','Hindi',49 

select * from marks_581

And my output should be like the below one

Name |Subject1|Marks1|Subject2|marks2|Subject3|marks3
Dishant|English |40 |Maths |45 |hindi |49
Gautham|English |40 |Maths |45 |hindi |49
Pranay |English |40 |Maths |45 |hindi |49

这是我的查询,我无法在主题栏中获取主题名称。它说像无法将varchar转换为数据类型int。有人帮我吗?

SELECT Name,       
    SUM(CASE WHEN Subject= 'English' THEN Subject ELSE 0 END) AS Subject1,
    SUM(CASE WHEN Subject= 'English' THEN Marks ELSE 0 END) AS Marks1,        
    SUM(CASE WHEN Subject= 'Maths' THEN Subject ELSE 0 END) AS Subject2,
    SUM(CASE WHEN Subject= 'Maths' THEN Marks ELSE 0 END) AS Marks2,    
    SUM(CASE WHEN Subject= 'Hindi' THEN Subject ELSE 0 END) AS Subject3,
    SUM(CASE WHEN Subject= 'Hindi' THEN Marks ELSE 0 END) AS Marks3 
FROM marks_581
GROUP BY Name
ORDER BY Name

5 个答案:

答案 0 :(得分:2)

对非数字数据使用不同的聚合函数,例如MAX()

SELECT Name,       
    MAX(CASE WHEN Subject= 'English' THEN Subject END) AS Subject1,
    SUM(CASE WHEN Subject= 'English' THEN Marks ELSE 0 END) AS Marks1,        
    MAX(CASE WHEN Subject= 'Maths' THEN Subject END) AS Subject2,
    SUM(CASE WHEN Subject= 'Maths' THEN Marks ELSE 0 END) AS Marks2,    
    MAX(CASE WHEN Subject= 'Hindi' THEN Subject END) AS Subject3,
    SUM(CASE WHEN Subject= 'Hindi' THEN Marks ELSE 0 END) AS Marks3 
FROM marks_581
GROUP BY Name
ORDER BY Name

虽然鉴于此查询的性质,我们知道我们可以进一步简化它:

SELECT Name,       
    'English' AS Subject1,
    SUM(CASE WHEN Subject= 'English' THEN Marks ELSE 0 END) AS Marks1,        
    'Maths' AS Subject2,
    SUM(CASE WHEN Subject= 'Maths' THEN Marks ELSE 0 END) AS Marks2,    
    'Hindi' AS Subject3,
    SUM(CASE WHEN Subject= 'Hindi' THEN Marks ELSE 0 END) AS Marks3 
FROM marks_581
GROUP BY Name
ORDER BY Name

答案 1 :(得分:1)

SELECT Name,       
    MAX(CASE WHEN Subject= 'English' THEN Subject ELSE Subject  END) AS Subject1,
    SUM(CASE WHEN Subject= 'English' THEN Marks ELSE 0 END) AS Marks1,        
    MAX(CASE WHEN Subject= 'Maths' THEN Subject ELSE Subject  END) AS Subject2,
    SUM(CASE WHEN Subject= 'Maths' THEN Marks ELSE 0 END) AS Marks2,    
    MAX(CASE WHEN Subject= 'Hindi' THEN Subject ELSE Subject  END) AS Subject3,
    SUM(CASE WHEN Subject= 'Hindi' THEN Marks ELSE 0 END) AS Marks3 
FROM marks_581
GROUP BY Name
ORDER BY Name

答案 2 :(得分:0)

这是因为......

 THEN Subject ELSE 0 END

... subject是varchar not int

答案 3 :(得分:0)

嘿,我觉得错误就在这里

CREATE TABLE [dbo].[marks_581](
[Name] [varchar](30) NOT NULL,
[Subject] [varchar](30) NOT NULL,
[Marks] [int] NOT NULL
)

使用上述命令创建的表将没有sub1 sub2 sub3和mark1 mark2和mark3

答案 4 :(得分:0)

SELECT Name,       
'English' AS Subject1,
SUM(CASE WHEN Subject= 'English' THEN Marks ELSE 0 END) AS Marks1,        
'Maths' AS Subject2,
SUM(CASE WHEN Subject= 'Maths' THEN Marks ELSE 0 END) AS Marks2,    
'Hindi' AS Subject3,
SUM(CASE WHEN Subject= 'Hindi' THEN Marks ELSE 0 END) AS Marks3 
FROM marks_581
GROUP BY Name
ORDER BY Name