1

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

我知道使用必须使用 group by 并使用 where 我不能使用聚合函数和运算符,所以我想要的伪代码是

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

这就是我卡住的地方。我是否使用子查询并添加一个名为 count 的列?我不确定我将如何编写代码,而且我是一个初级 SQL 用户,我可能正在寻找完全错误的方向。

我的整个查询如下,我想修改如果只有计数(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 列上的所有数据。完美运行,谢谢大家的帮助!

4

4 回答 4

0

更正:

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')
于 2010-07-20T01:30:45.583 回答
0

我自己解决了这个问题,方法是使用内部连接一个查询,该查询选择前 10 个并连接 dialed_num 列上的所有数据。完美运行,谢谢大家的帮助!

于 2010-07-20T02:51:56.647 回答
0

你可以这样做:

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
于 2010-07-20T00:59:58.943 回答
0

首先判断哪些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
)
于 2010-07-20T01:18:44.730 回答