使用 SQL Server 2005 我试图在 WHERE 子句中执行 HAVING 听起来很疯狂吗?

发布于 2024-09-10 16:50:41 字数 2112 浏览 10 评论 0原文

好的,我有超过一百万条记录,我需要从数据库中选择列数大于或等于 10 的所有信息。

我知道使用having 必须使用 group by 并在无法使用聚合函数的地方使用 using运算符,所以我想要的伪代码是

Select column1,column2,column3
From MYdatabase
Where count(column1) > 10

这就是我陷入困境的地方。我是否使用子查询并添加名为 count 的列?我不确定如何立即编写代码,而且由于我是一个 SQL 初学者用户,我可能会寻找完全错误的方向。

我的整个查询如下,我想修改是否只有 count(callid) >=10 的记录才会被选择。

Select
    FiscalMonthYear,
    'MyCenter' = Case EP.Center
        When 'Livermore Call Center' Then 'LCC'
        When 'Natomas Call Center' Then 'NCC'
        When 'Concord Call Center' Then 'CCC'
        When 'Virtual Call Center' Then 'VCC'
        When 'Morgan Hill Call Center' Then 'MHCC'
        Else Center
        End,
    ECH.segstart,
    ECH.consulttime,
    EP.Queue,
    (EP.MgrFName +' '+ EP.MgrLName)AS Manager,
    (EP.SupFName +' '+ EP.SupLName)AS Supervisor,
    (EP.RepFName +' '+ EP.RepLName)As Agent,
    EP.RepPERNR,
    LEFT(ECH.segstart, 19) as SegmentDateTime,
    ECH.origlogin,
    ECH.dialed_num, 
    ECH.segment,
    ECH.SegStart_Date,
    ECH.callid
FROM CMS_ECH.dbo.CaliforniaECH ECH 
    INNER JOIN CAPLESQL02.InfoQuest.dbo.IQ_Employee_Avaya_Id A ON ECH.origlogin = A.AvayaID
    AND getdate () BETWEEN StartDate AND EndDate
    INNER JOIN CAPLESQL02.InfoQuest.dbo.IQ_Employee_Profiles_v3 EP ON A.IQID = EP.RepID
    AND getdate () BETWEEN RepToSup_StartDate and RepToSup_EndDate
    AND getdate () BETWEEN SupToMgr_StartDate and SupToMgr_EndDate
    AND getdate () BETWEEN RepQueue_StartDate and RepQueue_EndDate 
    INNER JOIN Cal_RemReporting.dbo.udFiscalMonthTable f on ECH.SegStart_Date = f.Tdate
Where dialed_num not like '______' 
AND dialed_num not like '' 
AND dialed_num not like '_______' 
and EP.Center is NOT Null  
and EP.Center not like 'Comm Ops' 
and EP.Center not like 'Same Day Group' 
and MgrLName not like 'Hollman'
and consulttime > 0 
and ECH.SegStart_Date between getdate()-90 and getdate()-1 
and EP.Queue not IN ('BST','Collections', 'DST','DSR','Escalations','Cable Store')

我自己通过使用内部联接查询解决了这个问题,该查询选择前 10 个并联接 dialed_num 列上的所有数据。工作完美,谢谢大家的帮助!

Ok, I have over a million records and I need to select all information from the database where the count of a column is greater or equal to 10.

I know that using having I must use group by and using where I cannot use aggregate functions with operators so what I want in pseudo code is

Select column1,column2,column3
From MYdatabase
Where count(column1) > 10

This is where I get stuck. Do I use a subquery and add a column called count? I am not sure how I would code that off hand and being that I am a beginner SQL user I might be looking in the completely wrong direction.

My whole query is below, I want to modify if so only records with a count(callid) >=10 will be selected.

Select
    FiscalMonthYear,
    'MyCenter' = Case EP.Center
        When 'Livermore Call Center' Then 'LCC'
        When 'Natomas Call Center' Then 'NCC'
        When 'Concord Call Center' Then 'CCC'
        When 'Virtual Call Center' Then 'VCC'
        When 'Morgan Hill Call Center' Then 'MHCC'
        Else Center
        End,
    ECH.segstart,
    ECH.consulttime,
    EP.Queue,
    (EP.MgrFName +' '+ EP.MgrLName)AS Manager,
    (EP.SupFName +' '+ EP.SupLName)AS Supervisor,
    (EP.RepFName +' '+ EP.RepLName)As Agent,
    EP.RepPERNR,
    LEFT(ECH.segstart, 19) as SegmentDateTime,
    ECH.origlogin,
    ECH.dialed_num, 
    ECH.segment,
    ECH.SegStart_Date,
    ECH.callid
FROM CMS_ECH.dbo.CaliforniaECH ECH 
    INNER JOIN CAPLESQL02.InfoQuest.dbo.IQ_Employee_Avaya_Id A ON ECH.origlogin = A.AvayaID
    AND getdate () BETWEEN StartDate AND EndDate
    INNER JOIN CAPLESQL02.InfoQuest.dbo.IQ_Employee_Profiles_v3 EP ON A.IQID = EP.RepID
    AND getdate () BETWEEN RepToSup_StartDate and RepToSup_EndDate
    AND getdate () BETWEEN SupToMgr_StartDate and SupToMgr_EndDate
    AND getdate () BETWEEN RepQueue_StartDate and RepQueue_EndDate 
    INNER JOIN Cal_RemReporting.dbo.udFiscalMonthTable f on ECH.SegStart_Date = f.Tdate
Where dialed_num not like '______' 
AND dialed_num not like '' 
AND dialed_num not like '_______' 
and EP.Center is NOT Null  
and EP.Center not like 'Comm Ops' 
and EP.Center not like 'Same Day Group' 
and MgrLName not like 'Hollman'
and consulttime > 0 
and ECH.SegStart_Date between getdate()-90 and getdate()-1 
and EP.Queue not IN ('BST','Collections', 'DST','DSR','Escalations','Cable Store')

I have solved the problem myself by using inner joining a query which selects the top 10 and joins all the data on the dialed_num column. Works Flawlessly, thank you all for your help!

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

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

发布评论

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

评论(4

夜未央樱花落 2024-09-17 16:50:41

首先确定哪些ECH数据有超过10个callid(伪代码):

select ECH.callid
from CMS_ECH.dbo.CaliforniaECH ECH
group by ECH.callid
having count(*) > 10

然后你可以使用它作为子查询来过滤你的结果:

...
from ECH
...
where ECH.callid IN (
 select ECH.callid
 from CMS_ECH.dbo.CaliforniaECH ECH
 group by ECH.callid
 having count(*) > 10
)

First determine which ECH data have more than 10 callid (pseudo-code):

select ECH.callid
from CMS_ECH.dbo.CaliforniaECH ECH
group by ECH.callid
having count(*) > 10

Then you can use this as a subquery to filter your results:

...
from ECH
...
where ECH.callid IN (
 select ECH.callid
 from CMS_ECH.dbo.CaliforniaECH ECH
 group by ECH.callid
 having count(*) > 10
)
小苏打饼 2024-09-17 16:50:41

您可以这样做:

Select column1,column2,column3 
From MyTable
Where (Select count(*) From MyTable Group By Column1) > 10 

更具体地说,对于您的示例,添加:

AND 
 (SELECT count(*) 
  FROM CMS_ECH.dbo.CaliforniaECH iECH 
  WHERE iECH.callid = ECH.callid) > 10

You can do this:

Select column1,column2,column3 
From MyTable
Where (Select count(*) From MyTable Group By Column1) > 10 

More specifically to your example, add:

AND 
 (SELECT count(*) 
  FROM CMS_ECH.dbo.CaliforniaECH iECH 
  WHERE iECH.callid = ECH.callid) > 10
猛虎独行 2024-09-17 16:50:41

我自己通过使用内部联接查询解决了这个问题,该查询选择前 10 个并联接 dialed_num 列上的所有数据。工作完美,谢谢大家的帮助!

I have solved the problem myself by using inner joining a query which selects the top 10 and joins all the data on the dialed_num column. Works Flawlessly, thank you all for your help!

九歌凝 2024-09-17 16:50:41

更正:

SELECT * FROM (SELECT 
    FiscalMonthYear,
    'MyCenter' = CASE EP.Center
        WHEN 'Livermore Call Center' THEN 'LCC'
        WHEN 'Natomas Call Center' THEN 'NCC'
        WHEN 'Concord Call Center' THEN 'CCC'
        WHEN 'Virtual Call Center' THEN 'VCC'
        WHEN 'Morgan Hill Call Center' THEN 'MHCC'
        ELSE Center
        END,
    ECH1.segstart,
    ECH1.consulttime,
    EP.Queue,
    (EP.MgrFName +' '+ EP.MgrLName)AS Manager,
    (EP.SupFName +' '+ EP.SupLName)AS Supervisor,
    (EP.RepFName +' '+ EP.RepLName)AS Agent,
    EP.RepPERNR,
    LEFT(ECH1.segstart, 19) AS SegmentDateTime,
    ECH1.origlogin,
    ECH1.dialed_num, 
    ECH1.segment,
    ECH1.SegStart_Date,
    ECH1.callid
FROM CMS_ECH.dbo.CaliforniaECH ECH1 GROUP BY ECH1.dialed_num HAVING COUNT(ECH1.callid) >= 10) ECH 
    INNER JOIN CAPLESQL02.InfoQuest.dbo.IQ_Employee_Avaya_Id A ON ECH.origlogin = A.AvayaID
        AND getdate () BETWEEN StartDate AND EndDate
    INNER JOIN CAPLESQL02.InfoQuest.dbo.IQ_Employee_Profiles_v3 EP ON A.IQID = EP.RepID
        AND getdate () BETWEEN RepToSup_StartDate AND RepToSup_EndDate
        AND getdate () BETWEEN SupToMgr_StartDate AND SupToMgr_EndDate
        AND getdate () BETWEEN RepQueue_StartDate AND RepQueue_EndDate 
    INNER JOIN Cal_RemReporting.dbo.udFiscalMonthTable f ON ECH.SegStart_Date = f.Tdate
WHERE dialed_num NOT LIKE '______' 
    AND dialed_num NOT LIKE '' 
    AND dialed_num NOT LIKE '_______' 
    AND EP.Center IS NOT NULL  
    AND EP.Center NOT LIKE 'Comm Ops' 
    AND EP.Center NOT LIKE 'Same Day Group' 
    AND MgrLName NOT LIKE 'Hollman'
    AND consulttime > 0 
    AND ECH.SegStart_Date BETWEEN getdate()-90 AND getdate()-1 
    AND EP.Queue NOT IN ('BST','Collections', 'DST','DSR','Escalations','Cable Store')

Corrected:

SELECT * FROM (SELECT 
    FiscalMonthYear,
    'MyCenter' = CASE EP.Center
        WHEN 'Livermore Call Center' THEN 'LCC'
        WHEN 'Natomas Call Center' THEN 'NCC'
        WHEN 'Concord Call Center' THEN 'CCC'
        WHEN 'Virtual Call Center' THEN 'VCC'
        WHEN 'Morgan Hill Call Center' THEN 'MHCC'
        ELSE Center
        END,
    ECH1.segstart,
    ECH1.consulttime,
    EP.Queue,
    (EP.MgrFName +' '+ EP.MgrLName)AS Manager,
    (EP.SupFName +' '+ EP.SupLName)AS Supervisor,
    (EP.RepFName +' '+ EP.RepLName)AS Agent,
    EP.RepPERNR,
    LEFT(ECH1.segstart, 19) AS SegmentDateTime,
    ECH1.origlogin,
    ECH1.dialed_num, 
    ECH1.segment,
    ECH1.SegStart_Date,
    ECH1.callid
FROM CMS_ECH.dbo.CaliforniaECH ECH1 GROUP BY ECH1.dialed_num HAVING COUNT(ECH1.callid) >= 10) ECH 
    INNER JOIN CAPLESQL02.InfoQuest.dbo.IQ_Employee_Avaya_Id A ON ECH.origlogin = A.AvayaID
        AND getdate () BETWEEN StartDate AND EndDate
    INNER JOIN CAPLESQL02.InfoQuest.dbo.IQ_Employee_Profiles_v3 EP ON A.IQID = EP.RepID
        AND getdate () BETWEEN RepToSup_StartDate AND RepToSup_EndDate
        AND getdate () BETWEEN SupToMgr_StartDate AND SupToMgr_EndDate
        AND getdate () BETWEEN RepQueue_StartDate AND RepQueue_EndDate 
    INNER JOIN Cal_RemReporting.dbo.udFiscalMonthTable f ON ECH.SegStart_Date = f.Tdate
WHERE dialed_num NOT LIKE '______' 
    AND dialed_num NOT LIKE '' 
    AND dialed_num NOT LIKE '_______' 
    AND EP.Center IS NOT NULL  
    AND EP.Center NOT LIKE 'Comm Ops' 
    AND EP.Center NOT LIKE 'Same Day Group' 
    AND MgrLName NOT LIKE 'Hollman'
    AND consulttime > 0 
    AND ECH.SegStart_Date BETWEEN getdate()-90 AND getdate()-1 
    AND EP.Queue NOT IN ('BST','Collections', 'DST','DSR','Escalations','Cable Store')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文