2014-10-29 85 views
0

嗨,大家好,我希望你可以用我正在写的SQL查询来帮助我。我已经实现了我想要的桌子,但是我必须逐一建立桌子。这是不高效的,当我结束200行时,这会降低网站的速度。PL/SQL查询效率提高

这是我的表是什么样子:

FeedBack Table: 

RatingID | RATING| TUTORIAL_ID| TUTORIAL_NAME 

5716| 4 | 993| Test002 

5717| 3 | 993| Test002 

5777| 1 | 994| eClip3 

5886| 1 | 994| eClip3 

7127| 4 | 1235| FTIR001 

7128| 4 | 1235| FTIR001 

7169| 3 | 1235| FTIR001 

7170| 2 | 1235| FTIR001 

7131| 4 | 1235| FTIR001 

7187| 3 | 1235| FTIR001 

7132| 3 | 1235| FTIR001 

我想做的产生是所有独特的教程名称的表格,然后的次数,具体教程被评为总数;

1(not useful), 2(somewhat useful), 3(useful), 4(Very Useful) 

所以查询应:

Tutorial Name | not Useful | Somewhat Useful| Useful| Very Useful 

Test002| 0| 0|1|1 

eClip3|2|0|0|0 

FTIR001| 0| 1| 3| 3 

表正在与落后C#中的网页上显示。目前我通过表循环查找单个教程名称的列表,然后为每个剪辑名称选择Count(1)(其中rating = 1等),并逐行构建表格。

我真正想知道的是,是否有办法一次完成所有这一切。这将大大提高网站的效率,因为有200多个教程我想展示数据。

+0

看看plsql分组http://psoug.org/definition/GROUPING.htm或者甚至更好的linq分组http://msdn.microsoft.com/en-us/library/bb545971.aspx – 2014-10-29 11:31:41

回答

1
select tutorial_name, 
     sum(case when rating=1 then 1 else 0 end) "not Useful", 
     sum(case when rating=2 then 1 else 0 end) "Somewhat Useful", 
     sum(case when rating=3 then 1 else 0 end) "Useful", 
     sum(case when rating=4 then 1 else 0 end) "Very Useful" 
    from feedback 
    group by tutorial_name; 

是您需要的查询。

+0

完美这就是什么我需要。非常感谢 – Brookes 2014-10-29 11:39:34

+0

@AlexBrookes然后请接受这个答案。 – 2014-10-29 11:40:24