SQL需要比较行计数值
我有一个查询,可以返回已输入ID的次数的ID,名称和计数。
SELECT
ID,
NAME,
COUNT(*) count
FROM
TABLE
GROUP BY
NAME, ID, CASE_DETAIL_ID
HAVING
COUNT(*) > 1;
这返回以下数据:
ID | 名称 | 计数 |
---|---|---|
123 | 帽子 | 10 |
123 | 伞 | 10 |
123 | 毛巾 | 10 |
123 | 水 | 8 |
555 | 帽子 | 3 |
555 | 伞 | 10 |
555 | 毛巾 | 10 |
555 | 水 | 10 |
322 | 伞 | 5 |
322 | 毛巾 | 20 |
322 | 水 | 20 |
我想能够查询与其他ID相同的行的计数小于其他行。我该怎么做?因此最终结果是:
ID | 名称 | 计数 | 完整计数 |
---|---|---|---|
123 | 水 | 8 | 10 |
555 | 帽子 | 3 | 10 |
322 | 伞 | 5 | 20 |
我们存储的多个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:
ID | NAME | COUNT |
---|---|---|
123 | HAT | 10 |
123 | UMBRELLA | 10 |
123 | TOWEL | 10 |
123 | WATER | 8 |
555 | HAT | 3 |
555 | UMBRELLA | 10 |
555 | TOWEL | 10 |
555 | WATER | 10 |
322 | UMBRELLA | 5 |
322 | TOWEL | 20 |
322 | WATER | 20 |
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:
ID | NAME | COUNT | FULL COUNT |
---|---|---|---|
123 | WATER | 8 | 10 |
555 | HAT | 3 | 10 |
322 | UMBRELLA | 5 | 20 |
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一些示例数据会有所帮助,但您可以使用cte,
选择
最低的使用min()
类似的事情:或可以使用
top 1 < /code>和
按
这样的订单:但是,请记住,因为这将
选择
仅第一行,这仅与一起使用
子句,因为它总是只返回1行。如果您使用CTE选项,则如果您需要每个ID,则可以使用
,其中ID = 123
。Some sample data would help but you can use a CTE, and
select
the lowest usingmin()
something like this:Or it can be done using
top 1
andorder by
like this:But bare in mind that as this would
select
only the first row, this would only work with thewhere
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
.