我需要为列的行显示值

时间:2016-04-23 20:57:43

标签: sql oracle

“显示与每个本科课程相关的专业数量。仅包括2到10个专业的课程”

SELECT prg_field AS "College", prg_name AS "Number of Majors"
FROM program 
WHERE prg_type = 'U'

我的“专业数量”栏目即将成为主要名称,如何将其更改为与学院的prg_field栏相关的值?

3 个答案:

答案 0 :(得分:0)

如果我理解正确,你想找到每个学院的不同prg_name的数量。如果这是正确的,你可以获得一套独特的大学/专业,然后计算每所大学的专业如下:

;WITH distSets as (
SELECT DISTINCT prg_field as 'College', prg_name as 'Number of Majors'
)

Select
    College
    ,COUNT(*) over (partition by College) as 'Count of Majors'
From distSets

如果您不能使用CTE,可能还有另一种格式化方法。 希望这会有所帮助。

答案 1 :(得分:0)

SELECT prg_field AS "College",
       COUNT( prg_name ) AS "Number of Majors"
FROM   program 
WHERE  prg_type = 'U'
GROUP BY prg_field
HAVING COUNT( prg_name ) BETWEEN 2 AND 10;

如果有prg_name个重复内容,请使用COUNT( DISTINCT prg_name )代替COUNT( prg_name )顶部和底部。

答案 2 :(得分:0)

考虑派生表子查询:

SELECT t1.prg_field AS "College", Count(*) As "Number Of Distinct Majors"
FROM
  (SELECT DISTINCT prg_field, prg_name
   FROM program 
   WHERE prg_type = 'U') t1
GROUP BY t1.prg_field
HAVING Count(*) BETWEEN 2 AND 10