with a as (
select '1' fxh,'c' fa, 'e' fb from dual
union
select '2' fxh,'c' fa, 'e' fb from dual
union
select '3' fxh,'c' fa, 'e' fb from dual
union
select '4' fxh,'c' fa, 'f' fb from dual
union
select '5' fxh,'c' fa, 'f' fb from dual
union
select '6' fxh,'d' fa, 'g' fb from dual
union
select '7' fxh,'d' fa, 'g' fb from dual
union
select '8' fxh,'d' fa, 'g' fb from dual
union
select '9' fxh,'d' fa, 'h' fb from dual
)
SELECT b.fa as "第一列",b.fb as "出現最多的",ct as "出現的次數" FROM (
SELECT max(ct)over(partition by fa ) maxct ,a.* FROM (
SELECT count(fb)over(partition by fa ,fb ) ct,fa,fb,row_number()over(partition by fa ,fb order by fa )rn ,fxh
FROM a
) a where a.rn=1
) b where maxct = ct
最近腦子有點抽,寫的可能有些復雜,但是可以完成你的需求,也希望有大腿寫出簡單的sql,學習一下
uj5u.com熱心網友回復:
看樓主要求了,如果有并列最多是都顯示還是只顯示一個,如果都顯示2樓的就沒問題
uj5u.com熱心網友回復:
with test as
(select 'c' a,'e' b from dual
union all select 'c' a,'e' b from dual
union all select 'c' a,'e' b from dual
union all select 'c' a,'f' b from dual
union all select 'c' a,'f' b from dual
union all select 'd' a,'g' b from dual
union all select 'd' a,'g' b from dual
union all select 'd' a,'g' b from dual
union all select 'd' a,'h' b from dual)
select a,b,c
from
(select a,b,c,rank() over(partition by a order by c desc ) rn
from
(select a,b,count(b) c from test group by a,b))
where rn=1;
uj5u.com熱心網友回復:
表名為X,SQL如下:
SELECT a, b, cnt
FROM (SELECT a, b, cnt, row_number() over(PARTITION BY a ORDER BY cnt DESC) rn
FROM (SELECT a, b, COUNT(b) over(PARTITION BY a, b) cnt FROM x))
WHERE rn = 1;
uj5u.com熱心網友回復:
select A,B,count(1) from table
group by A,B
uj5u.com熱心網友回復:
剛沒理解全,改了下
select A, B, d
from (select A,
B,
d,
dense_rank() over(partition by A order by d desc) rnn
from (select A, B, count(1) d from table t group by A, B))
where rnn = 1;
uj5u.com熱心網友回復:
大神,我不是很理解你的演算法,怎么體現圖二的B列他C列呢?
uj5u.com熱心網友回復:
剛沒理解全,改了下
select A, B, d
from (select A,
B,
d,
dense_rank() over(partition by A order by d desc) rnn
from (select A, B, count(1) d from table t group by A, B))
where rnn = 1;
**桔妹導讀:**深耕人工智能領域,致力于探索AI讓出行更美好的滴滴AI Labs再次斬獲國際大獎,這次獲獎的專案是什么呢?一起來看看詳細報道吧! 近日,由國際計算語言學協會ACL(The Association for Computational Linguistics)舉辦的世界最具影響力的機器 ......
我們經常在資料庫中使用 LIKE 運算子來完成對資料的模糊搜索,LIKE 運算子用于在 WHERE 子句中搜索列中的指定模式。 如果需要查找客戶表中所有姓氏是“張”的資料,可以使用下面的 SQL 陳述句: SELECT * FROM Customer WHERE Name LIKE '張%' 如果需要 ......
關于MySQL的二進制日志(binlog),我們都知道二進制日志(binlog)非常重要,尤其當你需要point to point災難恢復的時侯,所以我們要對其進行備份。關于二進制日志(binlog)的備份,可以基于flush logs方式先切換binlog,然后拷貝&壓縮到到遠程服務器或本地服務器 ......