大家好,并提前感谢任何帮助。这是我的情景:
我有三个链接表格,显示每种产品的生产规格和墨水类型。每个规格可以具有可变数量的墨水(从1到10),其可以与其他墨水相同或不同。我的目标是让每个规格能够显示规格名称和更频繁出现的墨水。
例如,对于规范XYZ:
|=====|=====|
¦SPEC ¦INK ¦
|=====|=====|
¦XYZ ¦blue ¦
¦XYZ ¦red ¦
¦XYZ ¦red ¦
¦XYZ ¦red ¦
¦XYZ ¦brown¦
|=====|=====|
我只想显示:
|=====|=====|
¦SPEC ¦INK ¦
|=====|=====|
¦XYZ ¦red ¦
|=====|=====|
表格链接如下:
SELECT tblStudioInput.SpecNo
, tblListInkSystem.Ink
FROM tblStudioInput
INNER JOIN tblStudioInputColour
ON tblStudioInput.stID = tblStudioInputColour.StudioInputID
INNER JOIN tblListInkSystem
ON tblStudioInputColour.InkSystem = tblListInkSystem.ID
再次感谢所有人
答案 0 :(得分:2)
SELECT SPEC, INK
FROM
(
SELECT SPEC, INK,
DENSE_RANK() OVER (ORDER BY COUNT(*) DESC) rn
FROM Table1
GROUP BY SPEC, INK
) s
WHERE rn = 1
SOURCE
答案 1 :(得分:0)
[更新]你可以试试这个:
select top 1 with ties
spec, ink
from
(
select
spec, ink, count(*) n
from
tmp
group by
spec, ink
) t
order by
n desc
[更新] SQLFiddle Demo
或没有排序
with q as (
select
spec, ink, count(*) n
from
tmp
group by
spec, ink
)
select
spec, ink
from
q
where
n = (
select
max(n)
from
q
)
或没有CTE
select
spec, ink
from
tmp
group by
spec, ink
having
count(*) = (
select
max(n)
from
(
select
count(*) n
from
tmp
group by
spec, ink
) t
)