0
select distinct (Member_Mailid), OrderLog_City, Member_MailId, 
       OrderLog_MovieName, OrderLog_MovieLanguage  
from tbl_OrderLog 
where 
OrderLog_Initiated between
  '2013-02-01 00:00:01.010' and '2013-02-01 23:59:59.990' 
and OrderLog_MovieLanguage in 
  ('TAMIL','ENGLISH','HINDI','MALAYALAM') 
group by Member_Mailid
having 
 count(Member_Mailid)>3  
order by member_mailid asc

当我运行上述查询时,我遇到了一些错误。

我的结果中需要以下列,这就是我之前在select语句中指定的原因:

(OrderLog_City,
Member_MailId,
OrderLog_MovieName,
OrderLog_MovieLanguage) 

其中列出了观看超过 3 次和 5 次的客户。

但它会引发错误;

消息 8120,级别 16,状态 1,第 1 行列 'tbl_OrderLog.OrderLog_City' 在选择列表中无效,因为它既不包含在聚合函数中,也不包含在 GROUP BY 子句中。

消息 209,级别 16,状态 1,第 9 行不明确的列名称“member_mailid”

4

1 回答 1

0

你不能混合DISTINCTGROUP BY

由于您使用的是COUNT,因此您需要与 聚合GROUP BY

GROUP BY您需要在子句中包含所有非聚合列。

这样的事情应该成功运行:

select Member_Mailid
  , OrderLog_City
  , OrderLog_MovieName
  , OrderLog_MovieLanguage
from tbl_OrderLog
where OrderLog_Initiated between '2013-02-01 00:00:01.010' and '2013-02-01 23:59:59.990'
  and OrderLog_MovieLanguage in ('TAMIL','ENGLISH','HINDI','MALAYALAM')
group by Member_Mailid
  , OrderLog_City
  , OrderLog_MovieName
  , OrderLog_MovieLanguage
having COUNT(Member_Mailid) > 3
order by member_mailid asc

评论后编辑

如果您想Member_Mailid根据上述查询获得不同的值,只需变成一个子查询并对其运行DISTINCT

select distinct(Member_Mailid)
from
(
  select Member_Mailid
  from tbl_OrderLog
  where OrderLog_Initiated between '2013-02-01 00:00:01.010' and '2013-02-01 23:59:59.990'
    and OrderLog_MovieLanguage in ('TAMIL','ENGLISH','HINDI','MALAYALAM')
  group by Member_Mailid
    , OrderLog_City
    , OrderLog_MovieName
    , OrderLog_MovieLanguage
  having COUNT(Member_Mailid) > 3
) m
于 2013-08-12T11:14:39.067 回答