需要使用复合主键计算不同的值

时间:2015-04-28 08:05:40

标签: sql oracle count distinct

我必须生成一份报告,显示不同地点的名称以及在这些地点提供独特课程的次数。报告只需要指明城市和否。独特的课程产品,并按最多的独特产品排序。 我遇到的问题是产品表有一个复合主键(提供开始日期和课程代码)

我运行此SQL查询,但由于您无法计算多个列,因此会出现错误。但我不确定如何分数。

SELECT 
    offlocation AS city, COUNT(distinct offbegindate,crscode) as no. of unique course offerings
FROM
    offering
GROUP BY offlocation
ORDER BY COUNT(distinct offbegindate,crscode) as no. of unique course offerings DESC;

非常感谢任何帮助。

4 个答案:

答案 0 :(得分:4)

只需进行微调,“行类型”计数不同:

SELECT 
    offlocation AS city,
    COUNT(distinct (offbegindate,crscode)) as no_of_unique_course_offerings
FROM
    offering
GROUP BY offlocation
ORDER BY no_of_unique_course_offerings DESC;

答案 1 :(得分:1)

您可以尝试连接值:

SELECT 
    offlocation AS city, COUNT(distinct offbegindate || '-' || crscode) as no. of unique course offerings
FROM
    offering
GROUP BY offlocation
ORDER BY COUNT(distinct offbegindate,crscode) as no. of unique course offerings DESC;

答案 2 :(得分:1)

希望这会奏效:

if (number % 3 == 0)
    if (number % 5 == 0)
        console.log("FizzBuzz");
    else
        console.log("Fizz");
else
    if (number % 5 == 0)
        console.log("Buzz");
    else
        console.log(numer);

答案 3 :(得分:0)

选择计数(   不同的CONCAT(offbegindate,'-',crscode) ) 来自cgtask.tsk_proj_cost;