SQL需要比较行计数值

发布于 2025-01-28 04:55:53 字数 3496 浏览 1 评论 0原文

我有一个查询,可以返回已输入ID的次数的ID,名称和计数。

SELECT 
    ID,
    NAME,
    COUNT(*) count
FROM 
    TABLE
GROUP BY
    NAME, ID, CASE_DETAIL_ID
HAVING
    COUNT(*) > 1;

这返回以下数据:

ID名称计数
123帽子10
12310
123毛巾10
1238
555帽子3
55510
555毛巾10
55510
3225
322毛巾20
32220

我想能够查询与其他ID相同的行的计数小于其他行。我该怎么做?因此最终结果是:

ID名称计数完整计数
123810
555帽子310
322520

我们存储的多个ID,我只希望行/名称的数量小于相同行的行ID有。

我也尝试过 -

WITH x AS
        (SELECT ID, NAME, COUNT(*) count
        FROM FRT.CASE_DETAIL_HISTORY
        GROUP BY
        NAME,
        ID,
        CASE_DETAIL_ID)
SELECT x.ID, t.NAME, X.COUNT, MIN(x.count)
FROM x
JOIN FRT.CASE_DETAIL_HISTORY t
on t.ID= x.ID
GROUP BY x.ID, t.ID, X.COUNT

但是,这并没有给我我想要的东西。我只希望在名称的计数与ID的“模式”计数匹配时返回行。 我也尝试了以下操作,但仍面临错误:

WITH COUNT_OF_ROWS AS 
    (SELECT ID, NAME, COUNT(*) count
    FROM TABLE
    GROUP BY NAME, ID, CASE_DETAIL_ID
    HAVING COUNT(*) >= 1),
    
MINIMUM AS
    (SELECT COUNT_OF_ROWS.ID, COUNT_OF_ROWS.NAME, 
MIN(COUNT_OF_ROWS.COUNT) MINI
    FROM COUNT_OF_ROWS
    JOIN TABLE CD on CD.ID = COUNT_OF_ROWS.ID
    GROUP BY COUNT_OF_ROWS.ID, COUNT_OF_ROWS.NAME
)
     
select distinct COUNT_OF_ROWS.*, MINIMUM.MINI
from minimum, count_of_rows
where minimum.mini != count_of_rows.count;

I have a query that returns the ID, Name and count of the number of times an ID has been entered to the table.

SELECT 
    ID,
    NAME,
    COUNT(*) count
FROM 
    TABLE
GROUP BY
    NAME, ID, CASE_DETAIL_ID
HAVING
    COUNT(*) > 1;

This returns the following data:

IDNAMECOUNT
123HAT10
123UMBRELLA10
123TOWEL10
123WATER8
555HAT3
555UMBRELLA10
555TOWEL10
555WATER10
322UMBRELLA5
322TOWEL20
322WATER20

I want to be able to query the row with a count of less than what the other rows with the same ID have. How can I do this? So that the end result is:

IDNAMECOUNTFULL COUNT
123WATER810
555HAT310
322UMBRELLA520

There are multiple IDs that we store and I only want the rows/names that have a count less than the rows with the same IDs have.

I have also tried -

WITH x AS
        (SELECT ID, NAME, COUNT(*) count
        FROM FRT.CASE_DETAIL_HISTORY
        GROUP BY
        NAME,
        ID,
        CASE_DETAIL_ID)
SELECT x.ID, t.NAME, X.COUNT, MIN(x.count)
FROM x
JOIN FRT.CASE_DETAIL_HISTORY t
on t.ID= x.ID
GROUP BY x.ID, t.ID, X.COUNT

However, this doesnt give me what I am looking for. I only want rows returned if the name's count doesnt match the 'mode' count of the ID.
I also have tried the below but keep facing errors:

WITH COUNT_OF_ROWS AS 
    (SELECT ID, NAME, COUNT(*) count
    FROM TABLE
    GROUP BY NAME, ID, CASE_DETAIL_ID
    HAVING COUNT(*) >= 1),
    
MINIMUM AS
    (SELECT COUNT_OF_ROWS.ID, COUNT_OF_ROWS.NAME, 
MIN(COUNT_OF_ROWS.COUNT) MINI
    FROM COUNT_OF_ROWS
    JOIN TABLE CD on CD.ID = COUNT_OF_ROWS.ID
    GROUP BY COUNT_OF_ROWS.ID, COUNT_OF_ROWS.NAME
)
     
select distinct COUNT_OF_ROWS.*, MINIMUM.MINI
from minimum, count_of_rows
where minimum.mini != count_of_rows.count;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

め可乐爱微笑 2025-02-04 04:55:54

一些示例数据会有所帮助,但您可以使用cte,选择最低的使用min()类似的事情:

WITH x AS(
    SELECT t.id, t.nametext, COUNT(*) as count
    FROM table t
    GROUP BY id, t.nametext, CASE_DETAIL_ID
), y as(
    SELECT x.id, MIN(x.[COUNT]) as mincount
    FROM x
    GROUP BY x.id
)
select y.id, x.nametext, y.mincount
from y
join x
on x.[COUNT] = y.mincount
and x.id = y.id

或可以使用top 1 < /code>和这样的订单:

SELECT TOP 1 id, name, COUNT(*) as count
FROM TABLE
WHERE ID = 123
GROUP BY NAME, ID, CASE_DETAIL_ID
ORDER BY count DESC

但是,请记住,因为这将选择仅第一行,这仅与一起使用子句,因为它总是只返回1行。

如果您使用CTE选项,则如果您需要每个ID,则可以使用,其中ID = 123

Some sample data would help but you can use a CTE, and select the lowest using min() something like this:

WITH x AS(
    SELECT t.id, t.nametext, COUNT(*) as count
    FROM table t
    GROUP BY id, t.nametext, CASE_DETAIL_ID
), y as(
    SELECT x.id, MIN(x.[COUNT]) as mincount
    FROM x
    GROUP BY x.id
)
select y.id, x.nametext, y.mincount
from y
join x
on x.[COUNT] = y.mincount
and x.id = y.id

Or it can be done using top 1 and order by like this:

SELECT TOP 1 id, name, COUNT(*) as count
FROM TABLE
WHERE ID = 123
GROUP BY NAME, ID, CASE_DETAIL_ID
ORDER BY count DESC

But bare in mind that as this would select only the first row, this would only work with the where clause because it would always only return 1 row.

While if you use the CTE option it would work also if you want per id, without where id = 123.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文