使用 SQL Server 2005 我试图在 WHERE 子句中执行 HAVING 听起来很疯狂吗?
好的,我有超过一百万条记录,我需要从数据库中选择列数大于或等于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先确定哪些ECH数据有超过10个callid(伪代码):
然后你可以使用它作为子查询来过滤你的结果:
First determine which ECH data have more than 10 callid (pseudo-code):
Then you can use this as a subquery to filter your results:
您可以这样做:
更具体地说,对于您的示例,添加:
You can do this:
More specifically to your example, add:
我自己通过使用内部联接查询解决了这个问题,该查询选择前 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!
更正:
Corrected: