获取特定行的最高计数

时间:2016-06-06 12:23:49

标签: sql sql-server

我想返回属于特定kategorie,subkategorie和type的最高关键字数。

我们必须使用WHERE SubKat_ID IS NOT NULL and only WHERE Type = A

这样的事情:

  

SELECT'获得最高关键字数量'从表WHERE Kat_ID = x和   输入=' A'并且SubKat不是NULL

示例数据:

ID  Keyword_ID  Kat_ID   SubKat_ID    Type
29  1            247       NULL        A
30  2            247       NULL        A
31  3            247       NULL        A
32  3            247       96          A
33  4            247       96          A
34  2            247       96          A
35  3            247       95          A
37  4            33        NULL        B
40  6            33        44          A
41  3            33        44          A
42  4            33        66          A
43  11           33        66          A
44  7            33        66          A
45  2            33        66          A
46  8            55        NULL        A

根据帖子底部的数据结合示例:

第一次测试:

WHERE Kat_ID = 247 And Type = 'A' 

它应该说:3因为我们有:3x行96和1x95

第二次测试:

WHERE Kat_ID = 33 And Type = 'A' 

应该说:4因为我们有:2x行为44和4x66

第三次测试:

WHERE Kat_ID = 55 And Type = 'A' 

应该说:0

5 个答案:

答案 0 :(得分:2)

这也应该处理你的边缘情况:

flag2 = true;

$('#clock').countdown(dateseance, { elapse: true }).on('update.countdown', function (event) {

                if (event.offset.minutes == (4) && flag2) {
                    alert("Less than 5 minutes before the end of counter");
                    flag2 = false;
                }

答案 1 :(得分:1)

这是你想要的吗?

SELECT TOP 1 COUNT(*)
FROM T
WHERE Kat_ID = 247 And Type = 'A' 
GROUP BY SubKat_ID
ORDER BY COUNT(*) DESC;

答案 2 :(得分:0)

SELECT count(1) as [Count], Kat_ID, SubKat_ID, [Type]

from table 

WHERE Kat_ID = x And Type = 'A' And SubKat IS NOT NULL

group by Kat_ID, SubKat_ID, [Type]

您希望使用group by子句一起告诉引擎 group 这些列中的相等值。

答案 3 :(得分:0)

试试这个:

SELECT TOP 1  CASE WHEN RecType = 'NULL' THEN 0 ELSE cnt END AS RecordCount FROM--TOP 1 FROM 
(
    SELECT COUNT(*) as cnt, 'NOT NULL' as RecType
    FROM #tmp
    WHERE Kat_ID = 33 AND Type = 'A' 
        AND SubKat_ID is not null
    GROUP BY SubKat_ID
UNION
    SELECT COUNT(*) AS cnt, 'NULL' AS RecType 
    FROM #tmp WHERE Kat_ID = 33 AND Type = 'A' 
        AND SubKat_ID IS NULL
) AS T1
ORDER BY cnt desc

但我觉得我错过了一种更简单的方式。

答案 4 :(得分:0)

select  max(count(1)) from stck
WHERE Kat_ID = 247 And Type = 'A' and SubKat_ID  is not null
group by SubKat_ID 

select  max(count(1)) from stck
WHERE Kat_ID = 33 And Type = 'A' and SubKat_ID  is not null
group by SubKat_ID 

select  nvl(max(count(1)),0)  from stck
WHERE Kat_ID = 55 And Type = 'A' and SubKat_ID  is not null
group by SubKat_ID 

Have written above queries on top oracle db