用于查找计数大于2的所有单个记录的SQL查询
所以我找不到任何解决办法。我已经看到许多订购和解决方案,这些都很接近,但并没有完全交付我所寻找的 我有这个:用于查找计数大于2的所有单个记录的SQL查询,sql,datetime,count,having-clause,Sql,Datetime,Count,Having Clause,所以我找不到任何解决办法。我已经看到许多订购和解决方案,这些都很接近,但并没有完全交付我所寻找的 我有这个: SELECT * FROM t1 WHERE t1.placed > '2020-12-01' AND t1.placed < '2020-12-02' GROUP BY t1.account HAVING count(t1.account) > 2; 现在,这正好反映了我希望看到的行,但我不希望它们组合在一起。我希望数据库给我所有单独的行。现在我试着通过删除组
SELECT *
FROM t1
WHERE t1.placed > '2020-12-01' AND t1.placed < '2020-12-02'
GROUP BY t1.account
HAVING count(t1.account) > 2;
现在,这正好反映了我希望看到的行,但我不希望它们组合在一起。我希望数据库给我所有单独的行。现在我试着通过删除组,但这根本不起作用。那么,我如何才能获得与上面相同的信息,但显示为单独的行而不是分组的行呢
我希望数据库给我所有单独的行
这听起来像是存在而不是伤害:
select *
from mytable t
where
placed > '2020-12-01'
and placed < '2020-12-02'
and exists (
select 1
from mytable t1
where
t1.account = t.account
and t1.placed > '2020-12-01'
and t1.placed < '2020-12-02'
and t1.placed <> t.placed
)
我希望数据库给我所有单独的行
这听起来像是存在而不是伤害:
select *
from mytable t
where
placed > '2020-12-01'
and placed < '2020-12-02'
and exists (
select 1
from mytable t1
where
t1.account = t.account
and t1.placed > '2020-12-01'
and t1.placed < '2020-12-02'
and t1.placed <> t.placed
)
您可以使用公共表表达式检索所需的帐户,然后将其再次与accounts表联接
with accountids(account) as
(select account
from accounttable
where placed > '2020-12-01' and placed < '2020-12-02'
group by account
having count(account) > 2)
select a.*
from accountids ids inner join accounttable a on ids.account = a.account
您可以使用公共表表达式检索所需的帐户,然后将其再次与accounts表联接
with accountids(account) as
(select account
from accounttable
where placed > '2020-12-01' and placed < '2020-12-02'
group by account
having count(account) > 2)
select a.*
from accountids ids inner join accounttable a on ids.account = a.account
也许通过join的子查询,您可以轻松地解决它
select a.*
from table a
inner join (
select itemID, Count(*)
from table
where placed > '2020-12-01' and placed < '2020-12-02'
group by itemID
having Count(*) > 2
) b on a.ItemID = b.itemID
也许通过join的子查询,您可以轻松地解决它
select a.*
from table a
inner join (
select itemID, Count(*)
from table
where placed > '2020-12-01' and placed < '2020-12-02'
group by itemID
having Count(*) > 2
) b on a.ItemID = b.itemID
样本数据和期望的结果会有所帮助。样本数据和期望的结果会有所帮助。